[Linked fields] Fix change detection on cipherType (#536)

* Fix bug that clears linkedId values when editing

* Add null check

* Fix linting
This commit is contained in:
Thomas Rittson 2021-11-09 21:57:33 +10:00 committed by GitHub
parent 2db9e1ce0d
commit 99ff3feb53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 6 deletions

View File

@ -49,6 +49,10 @@ export class AddEditCustomFieldsComponent implements OnChanges {
ngOnChanges(changes: SimpleChanges) { ngOnChanges(changes: SimpleChanges) {
if (changes.thisCipherType != null) { if (changes.thisCipherType != null) {
this.setLinkedFieldOptions(); this.setLinkedFieldOptions();
if (!changes.thisCipherType.firstChange) {
this.resetCipherLinkedFields();
}
} }
} }
@ -92,9 +96,7 @@ export class AddEditCustomFieldsComponent implements OnChanges {
} }
private setLinkedFieldOptions() { private setLinkedFieldOptions() {
// Delete any Linked custom fields if the item type does not support them
if (this.cipher.linkedFieldOptions == null) { if (this.cipher.linkedFieldOptions == null) {
this.cipher.fields = this.cipher.fields.filter(f => f.type !== FieldType.Linked);
return; return;
} }
@ -102,11 +104,21 @@ export class AddEditCustomFieldsComponent implements OnChanges {
this.cipher.linkedFieldOptions.forEach((linkedFieldOption, id) => this.cipher.linkedFieldOptions.forEach((linkedFieldOption, id) =>
options.push({ name: this.i18nService.t(linkedFieldOption.i18nKey), value: id })); options.push({ name: this.i18nService.t(linkedFieldOption.i18nKey), value: id }));
this.linkedFieldOptions = options.sort(Utils.getSortFunction(this.i18nService, 'name')); this.linkedFieldOptions = options.sort(Utils.getSortFunction(this.i18nService, 'name'));
}
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;
}
if (!this.editMode) {
this.cipher.fields this.cipher.fields
.filter(f => f.type = FieldType.Linked) .filter(f => f.type = FieldType.Linked)
.forEach(f => f.linkedId = this.linkedFieldOptions[0].value); .forEach(f => f.linkedId = this.linkedFieldOptions[0].value);
} }
} }
}