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 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) { if (Array.isArray(removedRowIds)) {
// const pk: Record<string, string> = rowPkData(row.row, meta?.value?.columns as ColumnType[]) for (const { row } of removedRowsData) {
// const rowIndex = findIndexByPk(pk, formattedData.value) const pk: Record<string, string> = rowPkData(row.row, meta?.value?.columns as ColumnType[])
// if (rowIndex !== -1) formattedData.value.splice(rowIndex, 1) const rowIndex = findIndexByPk(pk, formattedData.value)
// paginationData.value.totalRows = paginationData.value.totalRows! - 1 if (rowIndex !== -1) formattedData.value.splice(rowIndex, 1)
// } paginationData.value.totalRows = paginationData.value.totalRows! - 1
// } }
// await callbacks?.syncPagination?.() }
// },
// args: [removedRowsData], await callbacks?.syncPagination?.()
// }, },
// undo: { args: [removedRowsData],
// fn: async function undo( },
// this: UndoRedoAction, undo: {
// removedRowsData: { Id: string; row: Row; rowIndex: number }[], fn: async function undo(
// pg: { page: number; pageSize: number }, this: UndoRedoAction,
// ) { removedRowsData: { Id: string; row: Row; rowIndex: number }[],
// const rowsToInsert = removedRowsData.reverse().map((row) => { pg: { page: number; pageSize: number },
// const pkData = rowPkData(row.row, meta.value?.columns as ColumnType[]) ) {
// row.row = { ...pkData, ...row.row } const rowsToInsert = removedRowsData
// return row .map((row) => {
// }) const pkData = rowPkData(row.row, meta.value?.columns as ColumnType[])
row.row = { ...pkData, ...row.row }
// const insertedRowIds = await bulkInsertRows( return row
// rowsToInsert.map((row) => row.row), })
// {}, .reverse()
// undefined,
// true, const insertedRowIds = await bulkInsertRows(
// ) rowsToInsert.map((row) => row.row),
// if (Array.isArray(insertedRowIds)) { {},
// for (const { row, rowIndex } of rowsToInsert) { undefined,
// recoverLTARRefs(row.row) true,
// if (rowIndex !== -1 && pg.pageSize === paginationData.value.pageSize) { )
// if (pg.page === paginationData.value.page) {
// formattedData.value.splice(rowIndex, 0, row) if (Array.isArray(insertedRowIds)) {
// } else { for (const { row, rowIndex } of rowsToInsert) {
// await callbacks?.changePage?.(pg.page) recoverLTARRefs(row.row)
// }
// } else { if (rowIndex !== -1 && pg.pageSize === paginationData.value.pageSize) {
// await callbacks?.loadData?.() if (pg.page === paginationData.value.page) {
// } formattedData.value.splice(rowIndex, 0, row)
// } } else {
// } await callbacks?.changePage?.(pg.page)
// }, }
// args: [removedRowsData, { page: paginationData.value.page, pageSize: paginationData.value.pageSize }], } else {
// }, await callbacks?.loadData?.()
// scope: defineViewScope({ view: viewMeta.value }), }
// }) }
}
},
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