From ec84cc9eff33907c6064f844c20629cb42a1de99 Mon Sep 17 00:00:00 2001 From: mertmit Date: Sat, 11 Mar 2023 16:25:14 +0300 Subject: [PATCH] feat: undo/redo for LTAR Signed-off-by: mertmit --- packages/nc-gui/composables/useLTARStore.ts | 37 ++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/nc-gui/composables/useLTARStore.ts b/packages/nc-gui/composables/useLTARStore.ts index 443492b65d..f726c4e744 100644 --- a/packages/nc-gui/composables/useLTARStore.ts +++ b/packages/nc-gui/composables/useLTARStore.ts @@ -43,6 +43,8 @@ const [useProvideLTARStore, useLTARStore] = useInjectionState( const { $api } = useNuxtApp() + const { addUndo, clone } = useUndoRedo() + const sharedViewPassword = inject(SharedViewPasswordInj, ref(null)) const childrenExcludedList = ref() @@ -245,7 +247,7 @@ const [useProvideLTARStore, useLTARStore] = useInjectionState( }) } - const unlink = async (row: Record) => { + const unlink = async (row: Record, { metaValue = meta.value }: { metaValue?: TableType } = {}, undo = false) => { // const column = meta.columns.find(c => c.id === this.column.colOptions.fk_child_column_id); // todo: handle if new record // if (this.isNew) { @@ -264,12 +266,26 @@ const [useProvideLTARStore, useLTARStore] = useInjectionState( await $api.dbTableRow.nestedRemove( NOCO, project.value.title as string, - meta.value.title, + metaValue.title, rowId.value, colOptions.type as 'mm' | 'hm', encodeURIComponent(column?.value?.title), getRelatedTableRowId(row) as string, ) + + if (!undo) { + addUndo({ + redo: { + fn: (row: Record, { metaValue }: { metaValue?: TableType } = {}) => unlink(row, { metaValue }, true), + args: [clone(row), clone({ metaValue })], + }, + undo: { + // eslint-disable-next-line @typescript-eslint/no-use-before-define + fn: (row: Record, { metaValue }: { metaValue?: TableType } = {}) => link(row, { metaValue }, true), + args: [clone(row), clone({ metaValue })], + }, + }) + } } catch (e: any) { message.error(`${t('msg.error.unlinkFailed')}: ${await extractSdkResponseErrorMsg(e)}`) } @@ -277,7 +293,7 @@ const [useProvideLTARStore, useLTARStore] = useInjectionState( reloadData?.(false) } - const link = async (row: Record) => { + const link = async (row: Record, { metaValue = meta.value }: { metaValue?: TableType } = {}, undo = false) => { // todo: handle new record // const pid = this._extractRowId(parent, this.parentMeta); // const id = this._extractRowId(this.row, this.meta); @@ -295,13 +311,26 @@ const [useProvideLTARStore, useLTARStore] = useInjectionState( await $api.dbTableRow.nestedAdd( NOCO, project.value.title as string, - meta.value.title as string, + metaValue.title as string, rowId.value, colOptions.type as 'mm' | 'hm', encodeURIComponent(column?.value?.title), getRelatedTableRowId(row) as string, ) await loadChildrenList() + + if (!undo) { + addUndo({ + redo: { + fn: (row: Record, { metaValue }: { metaValue?: TableType } = {}) => link(row, { metaValue }, true), + args: [clone(row), clone({ metaValue })], + }, + undo: { + fn: (row: Record, { metaValue }: { metaValue?: TableType } = {}) => unlink(row, { metaValue }, true), + args: [clone(row), clone({ metaValue })], + }, + }) + } } catch (e: any) { message.error(`Linking failed: ${await extractSdkResponseErrorMsg(e)}`) }