Browse Source

fix(nc-gui): review changes

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

Loading…
Cancel
Save