From d6359632b411bb8d3cfc96fb6eee4ae0ac408d02 Mon Sep 17 00:00:00 2001 From: Ramesh Mane <101566080+rameshmane7218@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:33:49 +0000 Subject: [PATCH] feat: added bulk delete for range delete rows --- packages/nc-gui/composables/useData.ts | 154 ++++++++++++++----------- 1 file changed, 86 insertions(+), 68 deletions(-) diff --git a/packages/nc-gui/composables/useData.ts b/packages/nc-gui/composables/useData.ts index 0ef5f28c74..f63b9d39cc 100644 --- a/packages/nc-gui/composables/useData.ts +++ b/packages/nc-gui/composables/useData.ts @@ -45,6 +45,8 @@ export function useData(args: { const { $api } = useNuxtApp() + const { isPaginationLoading } = storeToRefs(useViewsStore()) + const selectedAllRecords = computed({ get() { return !!formattedData.value.length && formattedData.value.every((row: Row) => row.rowMeta.selected) @@ -112,6 +114,7 @@ export function useData(args: { ) { row.row = { ...pkData, ...row.row } const insertedData = await insertRow(row, ltarState, undefined, true) + if (rowIndex !== -1 && pg.pageSize === paginationData.value.pageSize) { if (pg.page === paginationData.value.page) { formattedData.value.splice(rowIndex, 0, { @@ -156,61 +159,47 @@ export function useData(args: { async function bulkInsertRows( rows: Row[], - ltarState: Record = {}, { metaValue = meta.value, viewMetaValue = viewMeta.value }: { metaValue?: TableType; viewMetaValue?: ViewType } = {}, undo = false, ) { const rowsToInsert = [] + + isPaginationLoading.value = true try { for (const currentRow of rows) { let { missingRequiredColumns, insertObj } = await populateInsertObject({ meta: metaValue!, - ltarState, + ltarState: {}, getMeta, row: currentRow.row, undo, }) const autoGeneratedKeys = clone(metaValue?.columns || []) - .filter((c) => isCreatedOrLastModifiedByCol(c) || isCreatedOrLastModifiedTimeCol(c)) + .filter((c) => c.uidt !== UITypes.ID && (isCreatedOrLastModifiedByCol(c) || isCreatedOrLastModifiedTimeCol(c))) .map((c) => c.title) - console.log('auto', autoGeneratedKeys, currentRow) // delete auto generated keys for (const key of autoGeneratedKeys) { - if (key !== 'Id') { - delete insertObj[key] - } + delete insertObj[key!] } if (missingRequiredColumns.size) continue - else rowsToInsert.push({ ...insertObj, ...(ltarState || {}) }) + else rowsToInsert.push({ ...insertObj }) } - const bulkInsertedData = await $api.dbDataTableRow.create(metaValue?.id as string, rowsToInsert, { + const bulkInsertedIds = await $api.dbDataTableRow.create(metaValue?.id as string, rowsToInsert, { viewId: viewMetaValue?.id as string, }) - // if (!undo) { - // addUndo({ - // redo: { - // fn: async function redo() {}, - // args: [], - // }, - // undo: { - // fn: async function undo(this: UndoRedoAction) {}, - // args: [], - // }, - // scope: defineViewScope({ view: viewMeta.value }), - // }) - // } - await callbacks?.syncCount?.() - return bulkInsertedData + + return bulkInsertedIds } catch (error: any) { message.error(await extractSdkResponseErrorMsg(error)) } finally { await callbacks?.globalCallback?.() + isPaginationLoading.value = false } } @@ -653,13 +642,12 @@ export function useData(args: { } }), ) + if (Array.isArray(removedRowIds)) { const removedRowsMap: Map = new Map(removedRowIds.map((row) => [row.Id as string, ''])) - removedRowsData.filter((row) => { - if (removedRowsMap.has(row.Id)) { - return true - } - }) + + removedRowsData.filter((row) => removedRowsMap.has(row.Id)) + const rowIndexes = removedRowsData.map((row) => row.rowIndex) formattedData.value = formattedData.value.filter((_, index) => rowIndexes.includes(index)) } @@ -707,7 +695,6 @@ export function useData(args: { const insertedRowIds = await bulkInsertRows( rowsToInsert.map((row) => row.row), - {}, undefined, true, ) @@ -746,7 +733,7 @@ export function useData(args: { // plus one because we want to include the end row let row = start + 1 - const removedRowsData: { id?: string; row: Row; rowIndex: number }[] = [] + const removedRowsData: { Id: string; row: Row; rowIndex: number }[] = [] while (row--) { try { const { row: rowObj, rowMeta } = formattedData.value[row] as Record @@ -756,13 +743,10 @@ export function useData(args: { .map((c) => rowObj[c.title as string]) .join('___') - const successfulDeletion = await deleteRowById(id as string) - if (!successfulDeletion) { - continue + if (id) { + removedRowsData.push({ Id: id, row: clone(formattedData.value[row]), rowIndex: row }) } - removedRowsData.push({ id, row: clone(formattedData.value[row]), rowIndex: row }) } - formattedData.value.splice(row, 1) } catch (e: any) { return message.error(`${t('msg.error.deleteRowFailed')}: ${await extractSdkResponseErrorMsg(e)}`) } @@ -770,16 +754,46 @@ export function useData(args: { if (row === end) break } + try { + const removedRowIds: { Id: string }[] = await bulkDeleteRows( + removedRowsData.map((row) => { + return { + Id: row.Id, + } + }), + ) + if (Array.isArray(removedRowIds)) { + const removedRowsMap: Map = new Map(removedRowIds.map((row) => [row.Id as string, ''])) + + removedRowsData.filter((row) => removedRowsMap.has(row.Id)) + + const rowIndexes = removedRowsData.map((row) => row.rowIndex) + formattedData.value = formattedData.value.filter((_, index) => rowIndexes.includes(index)) + } + } catch (e: any) { + return message.error(`${t('msg.error.bulkDeleteRowsFailed')}: ${await extractSdkResponseErrorMsg(e)}`) + } + addUndo({ redo: { - fn: async function redo(this: UndoRedoAction, removedRowsData: { id?: string; row: Row; rowIndex: number }[]) { - for (const { id, row } of removedRowsData) { - await deleteRowById(id as string) - const pk: Record = rowPkData(row.row, meta?.value?.columns as ColumnType[]) - const rowIndex = findIndexByPk(pk, formattedData.value) - if (rowIndex !== -1) formattedData.value.splice(rowIndex, 1) - paginationData.value.totalRows = paginationData.value.totalRows! - 1 + fn: async function redo(this: UndoRedoAction, removedRowsData: { Id: string; row: Row; rowIndex: number }[]) { + const removedRowIds = await bulkDeleteRows( + removedRowsData.map((row) => { + return { + Id: row.Id, + } + }), + ) + + if (Array.isArray(removedRowIds)) { + for (const { row } of removedRowsData) { + const pk: Record = rowPkData(row.row, meta?.value?.columns as ColumnType[]) + const rowIndex = findIndexByPk(pk, formattedData.value) + if (rowIndex !== -1) formattedData.value.splice(rowIndex, 1) + paginationData.value.totalRows = paginationData.value.totalRows! - 1 + } } + await callbacks?.syncPagination?.() }, args: [removedRowsData], @@ -787,21 +801,36 @@ export function useData(args: { undo: { fn: async function undo( this: UndoRedoAction, - removedRowsData: { id?: string; row: Row; rowIndex: number }[], + removedRowsData: { Id: string; row: Row; rowIndex: number }[], pg: { page: number; pageSize: number }, ) { - for (const { row, rowIndex } of removedRowsData.slice().reverse()) { - const pkData = rowPkData(row.row, meta.value?.columns as ColumnType[]) - row.row = { ...pkData, ...row.row } - await insertRow(row, {}, {}, true) - if (rowIndex !== -1 && pg.pageSize === paginationData.value.pageSize) { - if (pg.page === paginationData.value.page) { - formattedData.value.splice(rowIndex, 0, row) + const rowsToInsert = removedRowsData + .map((row) => { + const pkData = rowPkData(row.row, meta.value?.columns as ColumnType[]) + row.row = { ...pkData, ...row.row } + return row + }) + .reverse() + + const insertedRowIds = await bulkInsertRows( + rowsToInsert.map((row) => row.row), + undefined, + true, + ) + + if (Array.isArray(insertedRowIds)) { + for (const { row, rowIndex } of rowsToInsert) { + recoverLTARRefs(row.row) + + if (rowIndex !== -1 && pg.pageSize === paginationData.value.pageSize) { + if (pg.page === paginationData.value.page) { + formattedData.value.splice(rowIndex, 0, row) + } else { + await callbacks?.changePage?.(pg.page) + } } else { - await callbacks?.changePage?.(pg.page) + await callbacks?.loadData?.() } - } else { - await callbacks?.loadData?.() } } }, @@ -818,32 +847,21 @@ export function useData(args: { async function bulkDeleteRows( rows: { Id: string }[], { metaValue = meta.value, viewMetaValue = viewMeta.value }: { metaValue?: TableType; viewMetaValue?: ViewType } = {}, - undo = false, ) { + isPaginationLoading.value = true try { const bulkDeletedRowsData = await $api.dbDataTableRow.delete(metaValue?.id as string, rows, { viewId: viewMetaValue?.id as string, }) - // if (!undo) { - // addUndo({ - // redo: { - // fn: async function redo() {}, - // args: [], - // }, - // undo: { - // fn: async function undo(this: UndoRedoAction) {}, - // args: [], - // }, - // scope: defineViewScope({ view: viewMeta.value }), - // }) - // } await callbacks?.syncCount?.() + return bulkDeletedRowsData } catch (error: any) { message.error(await extractSdkResponseErrorMsg(error)) } finally { await callbacks?.globalCallback?.() + isPaginationLoading.value = false } }