Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFD-200: Persist diagram display #127

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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