diff --git a/packages/nc-gui/composables/useData.ts b/packages/nc-gui/composables/useData.ts index d1d3a56ce0..74ec9d851c 100644 --- a/packages/nc-gui/composables/useData.ts +++ b/packages/nc-gui/composables/useData.ts @@ -621,7 +621,8 @@ export function useData(args: { async function deleteSelectedRows() { let row = formattedData.value.length - let removedRowsData: { Id: string; row: Row; rowIndex: number }[] = [] + let removedRowsData: Record[] = [] + let pk = '' while (row--) { const { row: rowObj, rowMeta } = formattedData.value[row] as Record @@ -629,33 +630,44 @@ export function useData(args: { continue } if (!rowMeta.new) { - const id = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[]) + const primaryKey = extractPk(meta?.value?.columns as ColumnType[]) + const pkValue = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[]) - if (id) { - removedRowsData.push({ Id: id, row: clone(formattedData.value[row]), rowIndex: row }) + if (primaryKey && pkValue) { + if (!pk) pk = primaryKey + + removedRowsData.push({ [pk]: pkValue as string, row: clone(formattedData.value[row]) as Row, rowIndex: row as number }) } } } + if (!removedRowsData.length) return + try { - const removedRowIds: { Id: string }[] = await bulkDeleteRows(removedRowsData.map((row) => ({ Id: row.Id }))) + const removedRowIds: Record[] = await bulkDeleteRows( + removedRowsData.map((row) => ({ [pk]: row[pk] as string })), + ) if (Array.isArray(removedRowIds)) { - const removedRowIdsSet = new Set(removedRowIds.map((row) => row.Id)) + const removedRowsDataSet = new Set(removedRowIds.map((row) => row[pk])) - removedRowsData = removedRowsData.filter((row) => removedRowIdsSet.has(row.Id)) + removedRowsData = removedRowsData.filter((row) => removedRowsDataSet.has(row[pk] as string)) const rowIndexesSet = new Set(removedRowsData.map((row) => row.rowIndex)) formattedData.value = formattedData.value.filter((_, index) => rowIndexesSet.has(index)) + } else { + removedRowsData = [] } } catch (e: any) { return message.error(`${t('msg.error.deleteRowFailed')}: ${await extractSdkResponseErrorMsg(e)}`) } + if (!removedRowsData.length) return + addUndo({ redo: { - fn: async function redo(this: UndoRedoAction, removedRowsData: { Id: string; row: Row; rowIndex: number }[]) { - const removedRowIds = await bulkDeleteRows(removedRowsData.map((row) => ({ Id: row.Id }))) + fn: async function redo(this: UndoRedoAction, removedRowsData: Record[], pk: string) { + const removedRowIds = await bulkDeleteRows(removedRowsData.map((row) => ({ [pk]: row[pk] as string }))) if (Array.isArray(removedRowIds)) { for (const { row } of removedRowsData) { @@ -668,12 +680,12 @@ export function useData(args: { await callbacks?.syncPagination?.() }, - args: [removedRowsData], + args: [removedRowsData, pk], }, undo: { fn: async function undo( this: UndoRedoAction, - removedRowsData: { Id: string; row: Row; rowIndex: number }[], + removedRowsData: Record[], pg: { page: number; pageSize: number }, ) { const rowsToInsert = removedRowsData @@ -724,17 +736,25 @@ export function useData(args: { // plus one because we want to include the end row let row = start + 1 - let removedRowsData: { Id: string; row: Row; rowIndex: number }[] = [] + let removedRowsData: Record[] = [] + let pk = '' while (row--) { try { const { row: rowObj, rowMeta } = formattedData.value[row] as Record if (!rowMeta.new) { - const id = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[]) + const primaryKey = extractPk(meta?.value?.columns as ColumnType[]) + const pkValue = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[]) + + if (primaryKey && pkValue) { + if (!pk) pk = primaryKey - if (id) { - removedRowsData.push({ Id: id, row: clone(formattedData.value[row]), rowIndex: row }) + removedRowsData.push({ + [pk]: pkValue as string, + row: clone(formattedData.value[row]) as Row, + rowIndex: row as number, + }) } } } catch (e: any) { @@ -744,25 +764,33 @@ export function useData(args: { if (row === end) break } + if (!removedRowsData.length) return + try { - const removedRowIds: { Id: string }[] = await bulkDeleteRows(removedRowsData.map((row) => ({ Id: row.Id }))) + const removedRowIds: Record[] = await bulkDeleteRows( + removedRowsData.map((row) => ({ [pk]: row[pk] as string })), + ) if (Array.isArray(removedRowIds)) { - const removedRowIdsSet = new Set(removedRowIds.map((row) => row.Id)) + const removedRowsDataSet = new Set(removedRowIds.map((row) => row[pk])) - removedRowsData = removedRowsData.filter((row) => removedRowIdsSet.has(row.Id)) + removedRowsData = removedRowsData.filter((row) => removedRowsDataSet.has(row[pk] as string)) const rowIndexesSet = new Set(removedRowsData.map((row) => row.rowIndex)) formattedData.value = formattedData.value.filter((_, index) => rowIndexesSet.has(index)) + } else { + removedRowsData = [] } } catch (e: any) { return message.error(`${t('msg.error.deleteRowFailed')}: ${await extractSdkResponseErrorMsg(e)}`) } + if (!removedRowsData.length) return + addUndo({ redo: { - fn: async function redo(this: UndoRedoAction, removedRowsData: { Id: string; row: Row; rowIndex: number }[]) { - const removedRowIds = await bulkDeleteRows(removedRowsData.map((row) => ({ Id: row.Id }))) + fn: async function redo(this: UndoRedoAction, removedRowsData: Record[], pk: string) { + const removedRowIds = await bulkDeleteRows(removedRowsData.map((row) => ({ [pk]: row[pk] as string }))) if (Array.isArray(removedRowIds)) { for (const { row } of removedRowsData) { @@ -775,12 +803,12 @@ export function useData(args: { await callbacks?.syncPagination?.() }, - args: [removedRowsData], + args: [removedRowsData, pk], }, undo: { fn: async function undo( this: UndoRedoAction, - removedRowsData: { Id: string; row: Row; rowIndex: number }[], + removedRowsData: Record[], pg: { page: number; pageSize: number }, ) { const rowsToInsert = removedRowsData @@ -824,7 +852,7 @@ export function useData(args: { } async function bulkDeleteRows( - rows: { Id: string }[], + rows: Record[], { metaValue = meta.value, viewMetaValue = viewMeta.value }: { metaValue?: TableType; viewMetaValue?: ViewType } = {}, ) { isPaginationLoading.value = true diff --git a/packages/nc-gui/utils/dataUtils.ts b/packages/nc-gui/utils/dataUtils.ts index b7fda18cb9..0465c388a5 100644 --- a/packages/nc-gui/utils/dataUtils.ts +++ b/packages/nc-gui/utils/dataUtils.ts @@ -22,6 +22,12 @@ export const rowPkData = (row: Record, columns: ColumnType[]) => { return pkData } +export const extractPk = (columns: ColumnType[]) => { + if (!columns && !Array.isArray(columns)) return null + const pkData = columns?.find((c) => c.pk) + return pkData ? pkData?.title || pkData?.column_name : null +} + export const findIndexByPk = (pk: Record, data: Row[]) => { for (const [i, row] of Object.entries(data)) { if (Object.keys(pk).every((k) => pk[k] === row.row[k])) {