Browse Source

feat: added bulk delete for range delete rows

pull/7389/head
Ramesh Mane 1 year ago
parent
commit
d6359632b4
  1. 130
      packages/nc-gui/composables/useData.ts

130
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<string, any> = {},
{ 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<string, string | number> = 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<string, any>
@ -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<string, string | number> = 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)
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],
@ -787,13 +801,27 @@ 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 rowsToInsert = removedRowsData
.map((row) => {
const pkData = rowPkData(row.row, meta.value?.columns as ColumnType[])
row.row = { ...pkData, ...row.row }
await insertRow(row, {}, {}, true)
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)
@ -804,6 +832,7 @@ export function useData(args: {
await callbacks?.loadData?.()
}
}
}
},
args: [removedRowsData, { page: paginationData.value.page, pageSize: paginationData.value.pageSize }],
},
@ -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
}
}

Loading…
Cancel
Save