Browse Source

feat: added undo redo for bulk delete

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

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

@ -1,5 +1,5 @@
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 {
NOCO,
@ -163,13 +163,26 @@ export function useData(args: {
const rowsToInsert = []
try {
for (const currentRow of rows) {
const { missingRequiredColumns, insertObj } = await populateInsertObject({
let { missingRequiredColumns, insertObj } = await populateInsertObject({
meta: metaValue!,
ltarState,
getMeta,
row: currentRow.row,
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
else rowsToInsert.push({ ...insertObj, ...(ltarState || {}) })
}
@ -614,7 +627,6 @@ export function useData(args: {
async function deleteSelectedRows() {
let row = formattedData.value.length
const removedRowsData: { Id: string; row: Row; rowIndex: number }[] = []
console.log('formated rows', formattedData.value)
while (row--) {
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, '']))
removedRowsData.filter((row) => {
if (removedRowsMap.has(row.Id)) {
removedRowsMap.set(row.Id, row.rowIndex)
return true
}
})
const rowIndex = removedRowsData.map((row) => row.rowIndex)
formattedData.value = formattedData.value.filter((_, index) => rowIndex.includes(index))
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 }[]) {
// const removedRowIds = await bulkDeleteRows(
// removedRowsData.map((row) => {
// return {
// 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(
// 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 }),
// })
addUndo({
redo: {
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<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
.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?.loadData?.()
}
}
}
},
args: [removedRowsData, { page: paginationData.value.page, pageSize: paginationData.value.pageSize }],
},
scope: defineViewScope({ view: viewMeta.value }),
})
await callbacks?.syncCount?.()
await callbacks?.syncPagination?.()

Loading…
Cancel
Save