Browse Source

fix(nc-gui): review changes

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

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

@ -1,5 +1,5 @@
import type { ColumnType, LinkToAnotherRecordType, PaginatedType, RelationTypes, TableType, ViewType } from 'nocodb-sdk'
import { UITypes, isCreatedOrLastModifiedTimeCol } from 'nocodb-sdk'
import { UITypes, isCreatedOrLastModifiedByCol, isCreatedOrLastModifiedTimeCol } from 'nocodb-sdk'
import type { ComputedRef, Ref } from 'vue'
import {
NOCO,
@ -163,17 +163,14 @@ export function useData(args: {
{ metaValue = meta.value, viewMetaValue = viewMeta.value }: { metaValue?: TableType; viewMetaValue?: ViewType } = {},
undo = false,
) {
let rowsToInsert = []
isPaginationLoading.value = true
// Todo: use isCreatedOrLastModifiedByCol insted of hardcoded values once upgrader is ready for oss
const autoGeneratedKeys = clone(metaValue?.columns || [])
.filter((c) => !c.pk && (isCreatedOrLastModifiedTimeCol(c) || c.uidt === 'CreatedBy' || c.uidt === 'LastModifiedBy'))
.filter((c) => !c.pk && (isCreatedOrLastModifiedTimeCol(c) || isCreatedOrLastModifiedByCol(c)))
.map((c) => c.title)
try {
rowsToInsert =
const rowsToInsert =
(
await Promise.all(
rows.map(async (currentRow) => {
@ -620,7 +617,7 @@ export function useData(args: {
async function deleteSelectedRows() {
let row = formattedData.value.length
let removedRowsData: Record<string, any>[] = []
let pk = ''
let compositePrimaryKey = ''
while (row--) {
const { row: rowObj, rowMeta } = formattedData.value[row] as Record<string, any>
@ -628,13 +625,17 @@ export function useData(args: {
continue
}
if (!rowMeta.new) {
const primaryKey = extractPk(meta?.value?.columns as ColumnType[])
const pkValue = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[])
const extractedPk = extractPk(meta?.value?.columns as ColumnType[])
const compositePkValue = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[])
if (primaryKey && pkValue) {
if (!pk) pk = primaryKey
if (extractedPk && compositePkValue) {
if (!compositePrimaryKey) compositePrimaryKey = extractedPk
removedRowsData.push({ [pk]: pkValue as string, row: clone(formattedData.value[row]) as Row, rowIndex: row as number })
removedRowsData.push({
[compositePrimaryKey]: compositePkValue as string,
row: clone(formattedData.value[row]) as Row,
rowIndex: row as number,
})
}
}
}
@ -643,13 +644,13 @@ export function useData(args: {
try {
const removedRowIds: Record<string, any>[] = await bulkDeleteRows(
removedRowsData.map((row) => ({ [pk]: row[pk] as string })),
removedRowsData.map((row) => ({ [compositePrimaryKey]: row[compositePrimaryKey] as string })),
)
if (Array.isArray(removedRowIds)) {
const removedRowsDataSet = new Set(removedRowIds.map((row) => row[pk]))
const removedRowsDataSet = new Set(removedRowIds.map((row) => row[compositePrimaryKey]))
removedRowsData = removedRowsData.filter((row) => removedRowsDataSet.has(row[pk] as string))
removedRowsData = removedRowsData.filter((row) => removedRowsDataSet.has(row[compositePrimaryKey] as string))
const rowIndexesSet = new Set(removedRowsData.map((row) => row.rowIndex))
formattedData.value = formattedData.value.filter((_, index) => rowIndexesSet.has(index))
@ -664,13 +665,15 @@ export function useData(args: {
addUndo({
redo: {
fn: async function redo(this: UndoRedoAction, removedRowsData: Record<string, any>[], pk: string) {
const removedRowIds = await bulkDeleteRows(removedRowsData.map((row) => ({ [pk]: row[pk] as string })))
fn: async function redo(this: UndoRedoAction, removedRowsData: Record<string, any>[], compositePrimaryKey: string) {
const removedRowIds = await bulkDeleteRows(
removedRowsData.map((row) => ({ [compositePrimaryKey]: row[compositePrimaryKey] as string })),
)
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)
const primaryKey: Record<string, string> = rowPkData(row.row, meta?.value?.columns as ColumnType[])
const rowIndex = findIndexByPk(primaryKey, formattedData.value)
if (rowIndex !== -1) formattedData.value.splice(rowIndex, 1)
paginationData.value.totalRows = paginationData.value.totalRows! - 1
}
@ -678,7 +681,7 @@ export function useData(args: {
await callbacks?.syncPagination?.()
},
args: [removedRowsData, pk],
args: [removedRowsData, compositePrimaryKey],
},
undo: {
fn: async function undo(
@ -735,21 +738,21 @@ export function useData(args: {
let row = start + 1
let removedRowsData: Record<string, any>[] = []
let pk = ''
let compositePrimaryKey = ''
while (row--) {
try {
const { row: rowObj, rowMeta } = formattedData.value[row] as Record<string, any>
if (!rowMeta.new) {
const primaryKey = extractPk(meta?.value?.columns as ColumnType[])
const pkValue = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[])
const extractedPk = extractPk(meta?.value?.columns as ColumnType[])
const compositePkValue = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[])
if (primaryKey && pkValue) {
if (!pk) pk = primaryKey
if (extractedPk && compositePkValue) {
if (!compositePrimaryKey) compositePrimaryKey = extractedPk
removedRowsData.push({
[pk]: pkValue as string,
[compositePrimaryKey]: compositePkValue as string,
row: clone(formattedData.value[row]) as Row,
rowIndex: row as number,
})
@ -766,13 +769,13 @@ export function useData(args: {
try {
const removedRowIds: Record<string, any>[] = await bulkDeleteRows(
removedRowsData.map((row) => ({ [pk]: row[pk] as string })),
removedRowsData.map((row) => ({ [compositePrimaryKey]: row[compositePrimaryKey] as string })),
)
if (Array.isArray(removedRowIds)) {
const removedRowsDataSet = new Set(removedRowIds.map((row) => row[pk]))
const removedRowsDataSet = new Set(removedRowIds.map((row) => row[compositePrimaryKey]))
removedRowsData = removedRowsData.filter((row) => removedRowsDataSet.has(row[pk] as string))
removedRowsData = removedRowsData.filter((row) => removedRowsDataSet.has(row[compositePrimaryKey] as string))
const rowIndexesSet = new Set(removedRowsData.map((row) => row.rowIndex))
formattedData.value = formattedData.value.filter((_, index) => rowIndexesSet.has(index))
@ -787,13 +790,13 @@ export function useData(args: {
addUndo({
redo: {
fn: async function redo(this: UndoRedoAction, removedRowsData: Record<string, any>[], pk: string) {
const removedRowIds = await bulkDeleteRows(removedRowsData.map((row) => ({ [pk]: row[pk] as string })))
fn: async function redo(this: UndoRedoAction, removedRowsData: Record<string, any>[], compositePrimaryKey: string) {
const removedRowIds = await bulkDeleteRows(removedRowsData.map((row) => ({ [compositePrimaryKey]: row[compositePrimaryKey] as string })))
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)
const primaryKey: Record<string, string> = rowPkData(row.row, meta?.value?.columns as ColumnType[])
const rowIndex = findIndexByPk(primaryKey, formattedData.value)
if (rowIndex !== -1) formattedData.value.splice(rowIndex, 1)
paginationData.value.totalRows = paginationData.value.totalRows! - 1
}
@ -801,7 +804,7 @@ export function useData(args: {
await callbacks?.syncPagination?.()
},
args: [removedRowsData, pk],
args: [removedRowsData, compositePrimaryKey],
},
undo: {
fn: async function undo(

Loading…
Cancel
Save