Skip to content

Commit

Permalink
Restore stored control states on init
Browse files Browse the repository at this point in the history
  • Loading branch information
jantoun-scottlogic committed Aug 2, 2024
1 parent dbd8d29 commit 72f6622
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/app/carbon-estimator-form/carbon-estimator-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ export class CarbonEstimatorFormComponent implements OnInit, OnDestroy {
if (storedFormData) {
this.estimatorForm.setValue(storedFormData);
}

const storedControlStates = this.getSavedControlStates();

if (storedControlStates) {
for (const [controlKey, controlState] of Object.entries(storedControlStates)) {
const control = this.estimatorForm.get(controlKey)!;
if (controlState.dirty) {
control.markAsDirty();
}
if (controlState.touched) {
control.markAsTouched();
}
}
}
}

ngOnDestroy(): void {
Expand Down Expand Up @@ -255,6 +269,11 @@ export class CarbonEstimatorFormComponent implements OnInit, OnDestroy {
this.storageService.set('controlStates', JSON.stringify(controlStates));
}

private getSavedControlStates() {
const storedControlStates = this.storageService.get('controlStates');
return storedControlStates ? (JSON.parse(storedControlStates) as Record<string, ControlState>) : null;
}

private saveFormState() {
this.saveFormData();
this.saveControlStates();
Expand Down

0 comments on commit 72f6622

Please sign in to comment.