From 8a6358ef44ef22d1308228a6768a4067f3b6ae90 Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Wed, 12 Aug 2020 22:34:16 +0100 Subject: [PATCH] [AAE-2984] fix save not modified Form with dropdown (#5976) * fix save not modified Form with dropdown * Update form-field.model.ts * Update form-field.model.ts * fix empty value scenario * fix unit --- .../widgets/core/form-field.model.ts | 17 ++++++--- lib/core/mock/form/form.component.mock.ts | 2 +- .../pagination/pagination.component.spec.ts | 3 -- .../widgets/dropdown/dropdown-cloud.widget.ts | 38 +++++++++---------- .../src/lib/form/form.component.spec.ts | 12 +++--- 5 files changed, 38 insertions(+), 34 deletions(-) diff --git a/lib/core/form/components/widgets/core/form-field.model.ts b/lib/core/form/components/widgets/core/form-field.model.ts index 83d21eaf74..dd65b609be 100644 --- a/lib/core/form/components/widgets/core/form-field.model.ts +++ b/lib/core/form/components/widgets/core/form-field.model.ts @@ -274,14 +274,19 @@ export class FormFieldModel extends FormWidgetModel { but saving back as object: { id: , name: } */ if (json.type === FormFieldTypes.DROPDOWN) { - if (json.hasEmptyValue && json.options) { + + if (json.options) { const options = json.options || []; if (options.length > 0) { - const emptyOption = json.options[0]; - if (value === '' || value === emptyOption.id || value === emptyOption.name) { - value = emptyOption.id; - } else if (value.id && value.name) { - value = value.id; + if (json.hasEmptyValue) { + const emptyOption = json.options[0]; + if (value === '' || value === emptyOption.id || value === emptyOption.name) { + value = emptyOption.id; + } + } else { + if (value?.id && value?.name) { + value = value.id; + } } } } diff --git a/lib/core/mock/form/form.component.mock.ts b/lib/core/mock/form/form.component.mock.ts index 86906022ff..ef590cc029 100644 --- a/lib/core/mock/form/form.component.mock.ts +++ b/lib/core/mock/form/form.component.mock.ts @@ -62,7 +62,7 @@ export const fakeForm: any = { 1: [ { fieldType: 'RestFieldRepresentation', - id: 'label', + id: 'dropdownId', name: 'Label', type: 'dropdown', value: 'Choose one...', diff --git a/lib/core/pagination/pagination.component.spec.ts b/lib/core/pagination/pagination.component.spec.ts index 7f046a70b3..e0b9e46ba4 100644 --- a/lib/core/pagination/pagination.component.spec.ts +++ b/lib/core/pagination/pagination.component.spec.ts @@ -250,9 +250,6 @@ describe('PaginationComponent', () => { expect(component.isFirstPage).toBe(true, 'isFirstPage'); expect(component.isLastPage).toBe(true, 'isLastPage'); - // tslint:disable-next-line: no-console - console.log(JSON.stringify(component.pagination)); - expect(component.range).toEqual([ 0, 0 ], 'range'); expect(component.pages).toEqual([ 1 ], 'pages'); }); diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts index 596315f040..03ad25610d 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts @@ -21,7 +21,7 @@ import { FormCloudService } from '../../../services/form-cloud.service'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; - /* tslint:disable:component-selector */ +/* tslint:disable:component-selector */ @Component({ selector: 'dropdown-cloud-widget', @@ -48,14 +48,14 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI constructor(public formService: FormService, private formCloudService: FormCloudService, private logService: LogService) { - super(formService); - } + super(formService); + } - ngOnInit() { - if (this.field && this.field.restUrl) { + ngOnInit() { + if (this.field && this.field.restUrl) { this.getValuesFromRestApi(); - } - } + } + } getValuesFromRestApi() { if (this.isValidRestType()) { @@ -81,18 +81,18 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI } compareDropdownValues(opt1: string, opt2: FormFieldOption | string): boolean { - return opt1 && opt2 && typeof opt2 !== 'string' ? ( opt1 === opt2.id || opt1 === opt2.name ) : opt1 === opt2; - } + return opt1 && opt2 && typeof opt2 !== 'string' ? (opt1 === opt2.id || opt1 === opt2.name) : opt1 === opt2; + } - getOptionValue(option: FormFieldOption, fieldValue: string): string { - let optionValue: string = ''; - if (option.id === 'empty' || option.name !== fieldValue) { - optionValue = option.id; - } else { - optionValue = option.name; - } - return optionValue; - } + getOptionValue(option: FormFieldOption, fieldValue: string): string { + let optionValue: string = ''; + if (option.id === 'empty' || option.name !== fieldValue) { + optionValue = option.id; + } else { + optionValue = option.name; + } + return optionValue; + } isValidRestType(): boolean { return this.field.optionType === 'rest' && !!this.field.restUrl; @@ -110,4 +110,4 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI this.onDestroy$.next(true); this.onDestroy$.complete(); } - } +} diff --git a/lib/process-services/src/lib/form/form.component.spec.ts b/lib/process-services/src/lib/form/form.component.spec.ts index 8cb165cb3b..f117cee2db 100644 --- a/lib/process-services/src/lib/form/form.component.spec.ts +++ b/lib/process-services/src/lib/form/form.component.spec.ts @@ -975,13 +975,13 @@ describe('FormComponent', () => { formComponent.form = new FormModel(JSON.parse(JSON.stringify(fakeForm))); let formFields = formComponent.form.getFormFields(); - let labelField = formFields.find((field) => field.id === 'label'); + let dropdownField = formFields.find((field) => field.id === 'dropdownId'); let radioField = formFields.find((field) => field.id === 'radio'); - expect(labelField.value).toBe('empty'); + expect(dropdownField.value).toBe('empty'); expect(radioField.value).toBeNull(); const formValues: any = {}; - formValues.label = { + formValues.dropdownId = { id: 'option_2', name: 'test2' }; @@ -991,9 +991,11 @@ describe('FormComponent', () => { formComponent.ngOnChanges({ 'data': change }); formFields = formComponent.form.getFormFields(); - labelField = formFields.find((field) => field.id === 'label'); + dropdownField = formFields.find((field) => field.id === 'dropdownId'); radioField = formFields.find((field) => field.id === 'radio'); - expect(labelField.value).toBe('option_2'); + + expect(dropdownField.value.id).toBe('option_2'); + expect(dropdownField.value.name).toBe('test2'); expect(radioField.value).toBe('option_2'); });