Browse Source

refactor: bulk delete useData fun

pull/7389/head
Ramesh Mane 12 months ago
parent
commit
8c19de9f14
  1. 104
      packages/nc-gui/composables/useData.ts

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

@ -165,40 +165,41 @@ export function useData(args: {
const rowsToInsert = [] const rowsToInsert = []
isPaginationLoading.value = true isPaginationLoading.value = true
try {
for (const currentRow of rows) {
const { missingRequiredColumns, insertObj } = await populateInsertObject({
meta: metaValue!,
ltarState: {},
getMeta,
row: currentRow.row,
undo,
})
// Todo: use isCreatedOrLastModifiedByCol insted of hardcoded values once upgrader is ready for oss // Todo: use isCreatedOrLastModifiedByCol insted of hardcoded values once upgrader is ready for oss
const autoGeneratedKeys = clone(metaValue?.columns || []) const autoGeneratedKeys = clone(metaValue?.columns || [])
.filter( .filter(
(c) => (c) =>
c.uidt !== UITypes.ID && c.uidt !== UITypes.ID && (isCreatedOrLastModifiedTimeCol(c) || c.uidt === 'CreatedBy' || c.uidt === 'LastModifiedBy'),
(isCreatedOrLastModifiedTimeCol(c) || c.uidt === 'CreatedBy' || c.uidt === 'LastModifiedBy'),
) )
.map((c) => c.title) .map((c) => c.title)
// delete auto generated keys try {
for (const key of autoGeneratedKeys) { rowsToInsert =
delete insertObj[key!] (
} await Promise.all(
rows.map(async (currentRow) => {
const { missingRequiredColumns, insertObj } = await populateInsertObject({
meta: metaValue!,
ltarState: {},
getMeta,
row: currentRow.row,
undo,
})
if (missingRequiredColumns.size) continue if (missingRequiredColumns.size === 0) {
else rowsToInsert.push({ ...insertObj }) autoGeneratedKeys.forEach((key) => delete insertObj[key])
return insertObj
} }
}),
)
)?.filter(Boolean) ?? [] // Filter out undefined values (if any)
const bulkInsertedIds = 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, viewId: viewMetaValue?.id as string,
}) })
await callbacks?.syncCount?.() await callbacks?.syncCount?.()
return bulkInsertedIds return bulkInsertedIds
} catch (error: any) { } catch (error: any) {
message.error(await extractSdkResponseErrorMsg(error)) message.error(await extractSdkResponseErrorMsg(error))
@ -620,7 +621,7 @@ 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 }[] = [] let removedRowsData: { Id: string; row: Row; rowIndex: number }[] = []
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>
@ -628,10 +629,7 @@ export function useData(args: {
continue continue
} }
if (!rowMeta.new) { if (!rowMeta.new) {
const id = meta?.value?.columns const id = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[])
?.filter((c) => c.pk)
.map((c) => rowObj[c.title as string])
.join('___')
if (id) { if (id) {
removedRowsData.push({ Id: id, row: clone(formattedData.value[row]), rowIndex: row }) removedRowsData.push({ Id: id, row: clone(formattedData.value[row]), rowIndex: row })
@ -640,21 +638,15 @@ export function useData(args: {
} }
try { try {
const removedRowIds: { Id: string }[] = await bulkDeleteRows( const removedRowIds: { Id: string }[] = await bulkDeleteRows(removedRowsData.map((row) => ({ Id: row.Id })))
removedRowsData.map((row) => {
return {
Id: row.Id,
}
}),
)
if (Array.isArray(removedRowIds)) { if (Array.isArray(removedRowIds)) {
const removedRowsMap: Map<string, string | number> = new Map(removedRowIds.map((row) => [row.Id as string, ''])) const removedRowIdsSet = new Set(removedRowIds.map((row) => row.Id))
removedRowsData.filter((row) => removedRowsMap.has(row.Id)) removedRowsData = removedRowsData.filter((row) => removedRowIdsSet.has(row.Id))
const rowIndexes = removedRowsData.map((row) => row.rowIndex) const rowIndexesSet = new Set(removedRowsData.map((row) => row.rowIndex))
formattedData.value = formattedData.value.filter((_, index) => rowIndexes.includes(index)) formattedData.value = formattedData.value.filter((_, index) => rowIndexesSet.has(index))
} }
} catch (e: any) { } catch (e: any) {
return message.error(`${t('msg.error.deleteRowFailed')}: ${await extractSdkResponseErrorMsg(e)}`) return message.error(`${t('msg.error.deleteRowFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
@ -663,13 +655,7 @@ export function useData(args: {
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) => ({ Id: row.Id })))
removedRowsData.map((row) => {
return {
Id: row.Id,
}
}),
)
if (Array.isArray(removedRowIds)) { if (Array.isArray(removedRowIds)) {
for (const { row } of removedRowsData) { for (const { row } of removedRowsData) {
@ -738,15 +724,14 @@ export function useData(args: {
// plus one because we want to include the end row // plus one because we want to include the end row
let row = start + 1 let row = start + 1
const removedRowsData: { Id: string; row: Row; rowIndex: number }[] = [] let removedRowsData: { Id: string; row: Row; rowIndex: number }[] = []
while (row--) { while (row--) {
try { try {
const { row: rowObj, rowMeta } = formattedData.value[row] as Record<string, any> const { row: rowObj, rowMeta } = formattedData.value[row] as Record<string, any>
if (!rowMeta.new) { if (!rowMeta.new) {
const id = meta?.value?.columns const id = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[])
?.filter((c) => c.pk)
.map((c) => rowObj[c.title as string])
.join('___')
if (id) { if (id) {
removedRowsData.push({ Id: id, row: clone(formattedData.value[row]), rowIndex: row }) removedRowsData.push({ Id: id, row: clone(formattedData.value[row]), rowIndex: row })
@ -760,20 +745,15 @@ export function useData(args: {
} }
try { try {
const removedRowIds: { Id: string }[] = await bulkDeleteRows( const removedRowIds: { Id: string }[] = await bulkDeleteRows(removedRowsData.map((row) => ({ Id: row.Id })))
removedRowsData.map((row) => {
return {
Id: row.Id,
}
}),
)
if (Array.isArray(removedRowIds)) { if (Array.isArray(removedRowIds)) {
const removedRowsMap: Map<string, string | number> = new Map(removedRowIds.map((row) => [row.Id as string, ''])) const removedRowIdsSet = new Set(removedRowIds.map((row) => row.Id))
removedRowsData.filter((row) => removedRowsMap.has(row.Id)) removedRowsData = removedRowsData.filter((row) => removedRowIdsSet.has(row.Id))
const rowIndexes = removedRowsData.map((row) => row.rowIndex) const rowIndexesSet = new Set(removedRowsData.map((row) => row.rowIndex))
formattedData.value = formattedData.value.filter((_, index) => rowIndexes.includes(index)) formattedData.value = formattedData.value.filter((_, index) => rowIndexesSet.has(index))
} }
} catch (e: any) { } catch (e: any) {
return message.error(`${t('msg.error.deleteRowFailed')}: ${await extractSdkResponseErrorMsg(e)}`) return message.error(`${t('msg.error.deleteRowFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
@ -782,13 +762,7 @@ export function useData(args: {
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) => ({ Id: row.Id })))
removedRowsData.map((row) => {
return {
Id: row.Id,
}
}),
)
if (Array.isArray(removedRowIds)) { if (Array.isArray(removedRowIds)) {
for (const { row } of removedRowsData) { for (const { row } of removedRowsData) {
@ -893,5 +867,7 @@ export function useData(args: {
bulkUpdateView, bulkUpdateView,
selectedAllRecords, selectedAllRecords,
removeRowIfNew, removeRowIfNew,
bulkDeleteRows,
bulkInsertRows,
} }
} }

Loading…
Cancel
Save