Browse Source

feat: added undo redo for bulk delete

pull/7389/head
Ramesh Mane 11 months ago
parent
commit
e905cd12a2
  1. 147
      packages/nc-gui/composables/useData.ts

147
packages/nc-gui/composables/useData.ts

@ -1,5 +1,5 @@
import type { ColumnType, LinkToAnotherRecordType, PaginatedType, RelationTypes, TableType, ViewType } from 'nocodb-sdk' import type { ColumnType, LinkToAnotherRecordType, PaginatedType, RelationTypes, TableType, ViewType } from 'nocodb-sdk'
import { UITypes } from 'nocodb-sdk' import { UITypes, getSystemColumnsTitles, isCreatedOrLastModifiedByCol, isCreatedOrLastModifiedTimeCol } from 'nocodb-sdk'
import type { ComputedRef, Ref } from 'vue' import type { ComputedRef, Ref } from 'vue'
import { import {
NOCO, NOCO,
@ -163,13 +163,26 @@ export function useData(args: {
const rowsToInsert = [] const rowsToInsert = []
try { try {
for (const currentRow of rows) { for (const currentRow of rows) {
const { missingRequiredColumns, insertObj } = await populateInsertObject({ let { missingRequiredColumns, insertObj } = await populateInsertObject({
meta: metaValue!, meta: metaValue!,
ltarState, ltarState,
getMeta, getMeta,
row: currentRow.row, row: currentRow.row,
undo, undo,
}) })
const autoGeneratedKeys = clone(metaValue?.columns || [])
.filter((c) => 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]
}
}
if (missingRequiredColumns.size) continue if (missingRequiredColumns.size) continue
else rowsToInsert.push({ ...insertObj, ...(ltarState || {}) }) else rowsToInsert.push({ ...insertObj, ...(ltarState || {}) })
} }
@ -614,7 +627,6 @@ export function useData(args: {
async function deleteSelectedRows() { async function deleteSelectedRows() {
let row = formattedData.value.length let row = formattedData.value.length
const removedRowsData: { Id: string; row: Row; rowIndex: number }[] = [] const removedRowsData: { Id: string; row: Row; rowIndex: number }[] = []
console.log('formated rows', formattedData.value)
while (row--) { while (row--) {
const { row: rowObj, rowMeta } = formattedData.value[row] as Record<string, any> const { row: rowObj, rowMeta } = formattedData.value[row] as Record<string, any>
@ -641,80 +653,85 @@ export function useData(args: {
} }
}), }),
) )
if (Array.isArray(removedRowsData)) { if (Array.isArray(removedRowIds)) {
const removedRowsMap: Map<string, string | number> = new Map(removedRowIds.map((row) => [row.Id as string, ''])) const removedRowsMap: Map<string, string | number> = new Map(removedRowIds.map((row) => [row.Id as string, '']))
removedRowsData.filter((row) => { removedRowsData.filter((row) => {
if (removedRowsMap.has(row.Id)) { if (removedRowsMap.has(row.Id)) {
removedRowsMap.set(row.Id, row.rowIndex)
return true return true
} }
}) })
const rowIndex = removedRowsData.map((row) => row.rowIndex) const rowIndexes = removedRowsData.map((row) => row.rowIndex)
formattedData.value = formattedData.value.filter((_, index) => rowIndex.includes(index)) formattedData.value = formattedData.value.filter((_, index) => rowIndexes.includes(index))
} }
} catch (e: any) { } catch (e: any) {
return message.error(`${t('msg.error.bulkDeleteRowsFailed')}: ${await extractSdkResponseErrorMsg(e)}`) return message.error(`${t('msg.error.bulkDeleteRowsFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
} }
// addUndo({ addUndo({
// redo: { redo: {
// fn: async function redo(this: UndoRedoAction, removedRowsData: { Id: string; row: Row; rowIndex: number }[]) { fn: async function redo(this: UndoRedoAction, removedRowsData: { Id: string; row: Row; rowIndex: number }[]) {
// const removedRowIds = await bulkDeleteRows( const removedRowIds = await bulkDeleteRows(
// removedRowsData.map((row) => { removedRowsData.map((row) => {
// return { return {
// Id: row.Id, Id: row.Id,
// } }
// }), }),
// ) )
// if (Array.isArray(removedRowIds)) {
// for (const { row } of removedRowsData) {
// const pk: Record<string, string> = 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],
// },
// undo: {
// fn: async function undo(
// this: UndoRedoAction,
// removedRowsData: { Id: string; row: Row; rowIndex: number }[],
// pg: { page: number; pageSize: number },
// ) {
// const rowsToInsert = removedRowsData.reverse().map((row) => {
// const pkData = rowPkData(row.row, meta.value?.columns as ColumnType[])
// row.row = { ...pkData, ...row.row }
// return row
// })
// const insertedRowIds = await bulkInsertRows( if (Array.isArray(removedRowIds)) {
// rowsToInsert.map((row) => row.row), for (const { row } of removedRowsData) {
// {}, const pk: Record<string, string> = rowPkData(row.row, meta?.value?.columns as ColumnType[])
// undefined, const rowIndex = findIndexByPk(pk, formattedData.value)
// true, if (rowIndex !== -1) formattedData.value.splice(rowIndex, 1)
// ) paginationData.value.totalRows = paginationData.value.totalRows! - 1
// if (Array.isArray(insertedRowIds)) { }
// for (const { row, rowIndex } of rowsToInsert) { }
// recoverLTARRefs(row.row)
// if (rowIndex !== -1 && pg.pageSize === paginationData.value.pageSize) { await callbacks?.syncPagination?.()
// if (pg.page === paginationData.value.page) { },
// formattedData.value.splice(rowIndex, 0, row) args: [removedRowsData],
// } else { },
// await callbacks?.changePage?.(pg.page) undo: {
// } fn: async function undo(
// } else { this: UndoRedoAction,
// await callbacks?.loadData?.() removedRowsData: { Id: string; row: Row; rowIndex: number }[],
// } pg: { page: number; pageSize: number },
// } ) {
// } const rowsToInsert = removedRowsData
// }, .map((row) => {
// args: [removedRowsData, { page: paginationData.value.page, pageSize: paginationData.value.pageSize }], const pkData = rowPkData(row.row, meta.value?.columns as ColumnType[])
// }, row.row = { ...pkData, ...row.row }
// scope: defineViewScope({ view: viewMeta.value }), 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?.loadData?.()
}
}
}
},
args: [removedRowsData, { page: paginationData.value.page, pageSize: paginationData.value.pageSize }],
},
scope: defineViewScope({ view: viewMeta.value }),
})
await callbacks?.syncCount?.() await callbacks?.syncCount?.()
await callbacks?.syncPagination?.() await callbacks?.syncPagination?.()

Loading…
Cancel
Save