Skip to content

Commit

Permalink
Store whether the form has been submitted
Browse files Browse the repository at this point in the history
Don't store the error state as it should be re-generated
  • Loading branch information
mgriffin-scottlogic committed Sep 10, 2024
1 parent 157bfbf commit ee9443b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class CarbonEstimatorFormComponent implements OnInit, OnDestroy {
showErrorSummary: false,
validationErrors: [],
};
public submitted = false;

public compareCostRanges = compareCostRanges;

Expand Down Expand Up @@ -193,7 +194,9 @@ export class CarbonEstimatorFormComponent implements OnInit, OnDestroy {
if (storedFormState) {
this.estimatorForm.setValue(storedFormState.formValue);
this.setControlStates(storedFormState.controlStates);
this.errorSummaryState = storedFormState.errorSummaryState;
if (storedFormState.submitted) {
this.handleSubmit();
}
}
}

Expand All @@ -202,6 +205,7 @@ export class CarbonEstimatorFormComponent implements OnInit, OnDestroy {
}

public handleSubmit() {
this.submitted = true;
if (this.estimatorForm.invalid) {
this.errorSummaryState = {
showErrorSummary: true,
Expand All @@ -227,6 +231,7 @@ export class CarbonEstimatorFormComponent implements OnInit, OnDestroy {

public resetForm() {
this.estimatorForm.reset();
this.submitted = false;
this.resetValidationErrors();
this.clearStoredFormState();
this.formReset.emit();
Expand Down Expand Up @@ -279,7 +284,7 @@ export class CarbonEstimatorFormComponent implements OnInit, OnDestroy {
}

private storeFormState() {
this.formStateService.storeFormState(this.estimatorForm, this.errorSummaryState);
this.formStateService.storeFormState(this.estimatorForm, this.submitted);
}

private getStoredFormState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,5 @@ export type ErrorSummaryState = {
export type FormState = {
formValue: EstimatorFormRawValue;
controlStates: Record<string, ControlState>;
errorSummaryState: ErrorSummaryState;
submitted: boolean;
};
6 changes: 3 additions & 3 deletions src/app/services/form-state.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { StorageService } from './storage.service';
import { FormGroup } from '@angular/forms';
import { ControlState, ErrorSummaryState, FormState } from '../carbon-estimator-form/carbon-estimator-form.constants';
import { ControlState, FormState } from '../carbon-estimator-form/carbon-estimator-form.constants';
import { EstimatorFormValues } from '../types/carbon-estimator';

@Injectable({
Expand Down Expand Up @@ -39,11 +39,11 @@ export class FormStateService {
}
}

storeFormState(estimatorForm: FormGroup<EstimatorFormValues>, errorSummaryState: ErrorSummaryState) {
storeFormState(estimatorForm: FormGroup<EstimatorFormValues>, submitted: boolean) {
const formState: FormState = {
formValue: estimatorForm.getRawValue(),
controlStates: this.getControlStates(estimatorForm),
errorSummaryState,
submitted,
};
this.storageService.set('formState', JSON.stringify(formState));
}
Expand Down

0 comments on commit ee9443b

Please sign in to comment.