From 6dfd46197d2fc65b2e69823a6aebc50ee19c990b Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Sun, 17 Jul 2022 13:42:36 -0600 Subject: [PATCH] handle case where selected union enum is invalid after migration (#1658) * handle case where selected union enum is invalid after migration * revert necessary ternary and fix types Co-authored-by: Lucy Cifferello <12953208+elvece@users.noreply.github.com> --- frontend/projects/ui/src/app/services/form.service.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/frontend/projects/ui/src/app/services/form.service.ts b/frontend/projects/ui/src/app/services/form.service.ts index b5f3affaa..80435e2d9 100644 --- a/frontend/projects/ui/src/app/services/form.service.ts +++ b/frontend/projects/ui/src/app/services/form.service.ts @@ -43,7 +43,7 @@ export class FormService { getUnionObject( spec: ValueSpecUnion | ListValueSpecUnion, selection: string, - current?: { [key: string]: any }, + current?: { [key: string]: any } | null, ): FormGroup { const { variants, tag } = spec const { name, description, warning } = isFullUnion(spec) @@ -84,7 +84,7 @@ export class FormService { private getFormGroup( config: ConfigSpec, validators: ValidatorFn[] = [], - current: { [key: string]: any } = {}, + current?: { [key: string]: any } | null, ): FormGroup { let group: Record = {} Object.entries(config).map(([key, spec]) => { @@ -128,10 +128,13 @@ export class FormService { }) return this.formBuilder.array(mapped, validators) case 'union': + const currentSelection = currentValue?.[spec.tag.id] + const isValid = !!spec.variants[currentSelection] + return this.getUnionObject( spec, - currentValue?.[spec.tag.id] || spec.default, - currentValue, + isValid ? currentSelection : spec.default, + isValid ? currentValue : undefined, ) case 'boolean': case 'enum':