From 6f8edaf4bade5bfb0dd0315e671518d09523d938 Mon Sep 17 00:00:00 2001 From: Nicoleta Panaghiu Date: Tue, 27 Aug 2024 14:28:40 +0300 Subject: [PATCH] RED-9887: fixed component fields cancel and revert actions. --- ...-structured-component-value.component.html | 18 ++-- ...le-structured-component-value.component.ts | 95 ++++++++++--------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/apps/red-ui/src/app/modules/file-preview/components/editable-structured-component-value/editable-structured-component-value.component.html b/apps/red-ui/src/app/modules/file-preview/components/editable-structured-component-value/editable-structured-component-value.component.html index 6a0624a6e..1d8262154 100644 --- a/apps/red-ui/src/app/modules/file-preview/components/editable-structured-component-value/editable-structured-component-value.component.html +++ b/apps/red-ui/src/app/modules/file-preview/components/editable-structured-component-value/editable-structured-component-value.component.html @@ -3,12 +3,12 @@ @if (!editing) {
- @for (componentValue of entry.componentValues; track componentValue) { + @for (componentValue of currentEntry().componentValues; track componentValue) { }
- @if (canEdit) { + @if (canEdit()) { - @if (hasUpdatedValues) { + @if (hasUpdatedValues()) {
} } @@ -24,10 +24,10 @@
} @else {
- @for (value of entry.componentValues; track value) { + @for (value of currentEntry().componentValues; track value) {
-
+
- @if (hasUpdatedValues && canEdit) { + @if (hasUpdatedValues() && canEdit()) { (); + currentEntry: WritableSignal; + readonly canEdit = input(); + readonly deselectLast = output(); + readonly overrideValue = output(); + readonly revertOverride = output(); + hasUpdatedValues = computed(() => this.currentEntry().overridden); + selected = false; + valueBeforeCurrentEdit: IComponentValue[]; protected entryLabel: string; protected editing = false; - protected hasUpdatedValues = false; protected initialEntry: IComponentLogEntry; + disabled = computed(() => { + for (let i = 0; i < this.currentEntry().componentValues.length; i++) { + if (this.currentEntry().componentValues[i].value !== this.initialEntry.componentValues[i]?.value) { + return false; + } + } + return this.currentEntry().componentValues.length === this.initialEntry.componentValues.length; + }); protected readonly iconButtonTypes = IconButtonTypes; - @Input() entry: IComponentLogEntry; - @Input() canEdit: boolean; - @Output() readonly deselectLast = new EventEmitter(); - @Output() readonly overrideValue = new EventEmitter(); - @Output() readonly revertOverride = new EventEmitter(); - selected = false; constructor( readonly helpModeService: HelpModeService, @@ -52,39 +62,18 @@ export class EditableStructuredComponentValueComponent implements OnInit { private readonly _state: FilePreviewStateService, ) {} - get disabled() { - for (let i = 0; i < this.entry.componentValues.length; i++) { - if (this.entry.componentValues[i].value !== this.initialEntry.componentValues[i]?.value) { - return false; - } - } - return this.entry.componentValues.length === this.initialEntry.componentValues.length; - } - - get #hasUpdatedValues() { - for (const value of this.entry.componentValues) { - if (value.originalValue === null && value.value === '') { - continue; - } - if (value.originalValue !== value.value) { - return true; - } - } - return false; - } - get #initialEntry() { - return JSON.parse(JSON.stringify(this.entry)); + return JSON.parse(JSON.stringify(this.entry())); } ngOnInit() { + this.currentEntry = signal(this.entry()); this.reset(); } reset() { this.initialEntry = this.#initialEntry; - this.hasUpdatedValues = this.#hasUpdatedValues; - this.entryLabel = this.parseName(this.entry.name); + this.entryLabel = this.parseName(this.currentEntry().name); this.deselect(); } @@ -96,11 +85,12 @@ export class EditableStructuredComponentValueComponent implements OnInit { } this.deselectLast.emit(); this.selected = true; - this._state.componentReferenceIds = this.#getUniqueReferencesIds(this.entry.componentValues); + this._state.componentReferenceIds = this.#getUniqueReferencesIds(this.currentEntry().componentValues); } } edit() { + this.valueBeforeCurrentEdit = JSON.parse(JSON.stringify([...this.currentEntry().componentValues])); this.deselectLast.emit(); this.selected = true; this.editing = true; @@ -114,37 +104,48 @@ export class EditableStructuredComponentValueComponent implements OnInit { this._state.componentReferenceIds = null; } + cancel($event?: MouseEvent) { + this.currentEntry.update(value => ({ ...value, componentValues: this.valueBeforeCurrentEdit })); + this.deselect($event); + } + removeValue(index: number) { - this.entry.componentValues.splice(index, 1); + this.currentEntry.update(value => ({ ...value, componentValues: value.componentValues.filter((_, i) => i !== index) })); } save() { - this.entry.overridden = true; - this.overrideValue.emit(this.entry); + this.currentEntry.update(value => ({ ...value, overridden: true })); + this.overrideValue.emit(this.currentEntry()); this.reset(); } async undo() { - const dialog = this._iqserDialog.openDefault(RevertValueDialogComponent, { data: { entry: this.entry }, width: '800px' }); + const dialog = this._iqserDialog.openDefault(RevertValueDialogComponent, { data: { entry: this.currentEntry() }, width: '800px' }); const result = await dialog.result(); if (result) { - this.revertOverride.emit(this.entry.name); + this.revertOverride.emit(this.currentEntry().name); this.reset(); } } add() { - this.entry.componentValues.push({ - componentRuleId: null, - entityReferences: [], - originalValue: null, - value: '', - valueDescription: '', - }); + this.currentEntry.update(value => ({ + ...value, + componentValues: [ + ...value.componentValues, + { + componentRuleId: null, + entityReferences: [], + originalValue: null, + value: '', + valueDescription: '', + }, + ], + })); } drop(event: CdkDragDrop) { - moveItemInArray(this.entry.componentValues, event.previousIndex, event.currentIndex); + moveItemInArray(this.currentEntry().componentValues, event.previousIndex, event.currentIndex); } parseName(name: string) { @@ -167,7 +168,7 @@ export class EditableStructuredComponentValueComponent implements OnInit { #updateTextAreaHeight() { setTimeout(() => { - for (let i = 0; i < this.entry.componentValues.length; i++) { + for (let i = 0; i < this.currentEntry().componentValues.length; i++) { const textArea = document.getElementById(`value-input-${i}`); textArea.style.height = 'auto'; textArea.style.height = `${textArea.scrollHeight}px`;