From 99ff3feb53c10d45dd7509aca0fa0b82052c2e14 Mon Sep 17 00:00:00 2001 From: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Date: Tue, 9 Nov 2021 21:57:33 +1000 Subject: [PATCH] [Linked fields] Fix change detection on cipherType (#536) * Fix bug that clears linkedId values when editing * Add null check * Fix linting --- .../add-edit-custom-fields.component.ts | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/angular/src/components/add-edit-custom-fields.component.ts b/angular/src/components/add-edit-custom-fields.component.ts index ffd6731bad..e1e4d14916 100644 --- a/angular/src/components/add-edit-custom-fields.component.ts +++ b/angular/src/components/add-edit-custom-fields.component.ts @@ -49,6 +49,10 @@ export class AddEditCustomFieldsComponent implements OnChanges { ngOnChanges(changes: SimpleChanges) { if (changes.thisCipherType != null) { this.setLinkedFieldOptions(); + + if (!changes.thisCipherType.firstChange) { + this.resetCipherLinkedFields(); + } } } @@ -92,9 +96,7 @@ export class AddEditCustomFieldsComponent implements OnChanges { } private setLinkedFieldOptions() { - // Delete any Linked custom fields if the item type does not support them if (this.cipher.linkedFieldOptions == null) { - this.cipher.fields = this.cipher.fields.filter(f => f.type !== FieldType.Linked); return; } @@ -102,11 +104,21 @@ export class AddEditCustomFieldsComponent implements OnChanges { this.cipher.linkedFieldOptions.forEach((linkedFieldOption, id) => options.push({ name: this.i18nService.t(linkedFieldOption.i18nKey), value: id })); this.linkedFieldOptions = options.sort(Utils.getSortFunction(this.i18nService, 'name')); + } - if (!this.editMode) { - this.cipher.fields - .filter(f => f.type = FieldType.Linked) - .forEach(f => f.linkedId = this.linkedFieldOptions[0].value); + private resetCipherLinkedFields() { + if (this.cipher.fields == null || this.cipher.fields.length === 0) { + return; } + + // Delete any Linked custom fields if the item type does not support them + if (this.cipher.linkedFieldOptions == null) { + this.cipher.fields = this.cipher.fields.filter(f => f.type !== FieldType.Linked); + return; + } + + this.cipher.fields + .filter(f => f.type = FieldType.Linked) + .forEach(f => f.linkedId = this.linkedFieldOptions[0].value); } }