From 7bc360f125df5f929d898ac1ff9636b97042e355 Mon Sep 17 00:00:00 2001 From: Sebastian Leidig Date: Tue, 2 Jul 2024 10:26:25 +0200 Subject: [PATCH] fix(public forms): show info to user submitting invalid form instead of throwing error in background fixes #2445 --- .../public-form/public-form.component.spec.ts | 19 +++++++++++++++++ .../public-form/public-form.component.ts | 21 ++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/app/features/public-form/public-form.component.spec.ts b/src/app/features/public-form/public-form.component.spec.ts index 9f7996a71f..d82ada44a9 100644 --- a/src/app/features/public-form/public-form.component.spec.ts +++ b/src/app/features/public-form/public-form.component.spec.ts @@ -17,6 +17,7 @@ import { MatSnackBar } from "@angular/material/snack-bar"; import { EntityFormService } from "../../core/common-components/entity-form/entity-form.service"; import { ConfigService } from "../../core/config/config.service"; import { EntityMapperService } from "../../core/entity/entity-mapper/entity-mapper.service"; +import { InvalidFormFieldError } from "../../core/common-components/entity-form/invalid-form-field.error"; describe("PublicFormComponent", () => { let component: PublicFormComponent; @@ -97,6 +98,24 @@ describe("PublicFormComponent", () => { expect(component.form.get("name")).toHaveValue(null); })); + it("should show a snackbar error and not reset when trying to submit invalid form", fakeAsync(() => { + initComponent(); + tick(); + const openSnackbarSpy = spyOn(TestBed.inject(MatSnackBar), "open"); + const saveSpy = spyOn(TestBed.inject(EntityFormService), "saveChanges"); + saveSpy.and.throwError(new InvalidFormFieldError()); + component.form.get("name").setValue("some name"); + + component.submit(); + + expect(saveSpy).toHaveBeenCalledWith(component.form, component.entity); + tick(); + expect(openSnackbarSpy).toHaveBeenCalledWith( + jasmine.stringContaining("invalid"), + ); + expect(component.form.get("name")).toHaveValue("some name"); + })); + it("should reset the form when clicking reset", fakeAsync(() => { initComponent(); tick(); diff --git a/src/app/features/public-form/public-form.component.ts b/src/app/features/public-form/public-form.component.ts index 8c5ba131a9..c796658c65 100644 --- a/src/app/features/public-form/public-form.component.ts +++ b/src/app/features/public-form/public-form.component.ts @@ -17,6 +17,7 @@ import { MatSnackBar } from "@angular/material/snack-bar"; import { MatCardModule } from "@angular/material/card"; import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy"; import { FieldGroup } from "../../core/entity-details/form/field-group"; +import { InvalidFormFieldError } from "../../core/common-components/entity-form/invalid-form-field.error"; @UntilDestroy() @Component({ @@ -55,11 +56,21 @@ export class PublicFormComponent implements OnInit { .subscribe(() => this.loadFormConfig()); } - submit() { - this.entityFormService - .saveChanges(this.form, this.entity) - .then(() => this.snackbar.open($localize`Successfully submitted form`)) - .then(() => this.initForm()); + async submit() { + try { + await this.entityFormService.saveChanges(this.form, this.entity); + this.snackbar.open($localize`Successfully submitted form`); + } catch (e) { + if (e instanceof InvalidFormFieldError) { + this.snackbar.open( + $localize`Some fields are invalid, please check the form and submit again.`, + ); + return; + } + throw e; + } + + this.initForm(); } reset() {