|
|
@ -1,9 +1,9 @@ |
|
|
|
import type { Api, PaginatedType, TableType, ViewType } from 'nocodb-sdk' |
|
|
|
import type { Api, PaginatedType, TableType, ViewType } from 'nocodb-sdk' |
|
|
|
import type { ComputedRef, Ref } from 'vue' |
|
|
|
import type { ComputedRef, Ref } from 'vue' |
|
|
|
|
|
|
|
import { notification } from 'ant-design-vue' |
|
|
|
import { useNuxtApp } from '#app' |
|
|
|
import { useNuxtApp } from '#app' |
|
|
|
import { useProject } from '#imports' |
|
|
|
import { useProject } from '#imports' |
|
|
|
import { NOCO } from '~/lib' |
|
|
|
import { NOCO } from '~/lib' |
|
|
|
import { notification } from 'ant-design-vue' |
|
|
|
|
|
|
|
import { extractSdkResponseErrorMsg } from '~/utils' |
|
|
|
import { extractSdkResponseErrorMsg } from '~/utils' |
|
|
|
|
|
|
|
|
|
|
|
const formatData = (list: Record<string, any>[]) => |
|
|
|
const formatData = (list: Record<string, any>[]) => |
|
|
@ -13,13 +13,18 @@ const formatData = (list: Record<string, any>[]) => |
|
|
|
rowMeta: {}, |
|
|
|
rowMeta: {}, |
|
|
|
})) |
|
|
|
})) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface Row { |
|
|
|
|
|
|
|
row: Record<string, any> |
|
|
|
|
|
|
|
oldRow: Record<string, any> |
|
|
|
|
|
|
|
rowMeta?: any |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function useViewData( |
|
|
|
export function useViewData( |
|
|
|
meta: Ref<TableType> | ComputedRef<TableType> | undefined, |
|
|
|
meta: Ref<TableType> | ComputedRef<TableType> | undefined, |
|
|
|
viewMeta: Ref<ViewType & { id: string }> | ComputedRef<ViewType & { id: string }> | undefined, |
|
|
|
viewMeta: Ref<ViewType & { id: string }> | ComputedRef<ViewType & { id: string }> | undefined, |
|
|
|
where?: ComputedRef<string | undefined>, |
|
|
|
where?: ComputedRef<string | undefined>, |
|
|
|
) { |
|
|
|
) { |
|
|
|
const data = ref<Record<string, any>[]>() |
|
|
|
const formattedData = ref<Row[]>([]) |
|
|
|
const formattedData = ref<{ row: Record<string, any>; oldRow: Record<string, any>; rowMeta?: any }[]>([]) |
|
|
|
|
|
|
|
const paginationData = ref<PaginatedType>({ page: 1, pageSize: 25 }) |
|
|
|
const paginationData = ref<PaginatedType>({ page: 1, pageSize: 25 }) |
|
|
|
const selectedRows = reactive([]) |
|
|
|
const selectedRows = reactive([]) |
|
|
|
|
|
|
|
|
|
|
@ -32,7 +37,6 @@ export function useViewData( |
|
|
|
...params, |
|
|
|
...params, |
|
|
|
where: where?.value, |
|
|
|
where: where?.value, |
|
|
|
}) |
|
|
|
}) |
|
|
|
data.value = response.list |
|
|
|
|
|
|
|
formattedData.value = formatData(response.list) |
|
|
|
formattedData.value = formatData(response.list) |
|
|
|
paginationData.value = response.pageInfo |
|
|
|
paginationData.value = response.pageInfo |
|
|
|
} |
|
|
|
} |
|
|
@ -74,11 +78,10 @@ export function useViewData( |
|
|
|
}) |
|
|
|
}) |
|
|
|
.then(() => {}) |
|
|
|
.then(() => {}) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
} catch (error) { |
|
|
|
} catch (error: any) { |
|
|
|
notification.error({ |
|
|
|
notification.error({ |
|
|
|
message: 'Row update failed', |
|
|
|
message: 'Row update failed', |
|
|
|
description: |
|
|
|
description: await extractSdkResponseErrorMsg(error), |
|
|
|
await extractSdkResponseErrorMsg(error), |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -104,11 +107,10 @@ export function useViewData( |
|
|
|
rowMeta: {}, |
|
|
|
rowMeta: {}, |
|
|
|
oldRow: { ...insertedData }, |
|
|
|
oldRow: { ...insertedData }, |
|
|
|
}) |
|
|
|
}) |
|
|
|
} catch (error) { |
|
|
|
} catch (error: any) { |
|
|
|
notification.error({ |
|
|
|
notification.error({ |
|
|
|
message: 'Row insert failed', |
|
|
|
message: 'Row insert failed', |
|
|
|
description: |
|
|
|
description: await extractSdkResponseErrorMsg(error), |
|
|
|
await extractSdkResponseErrorMsg(error), |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -118,23 +120,104 @@ export function useViewData( |
|
|
|
await loadData({ offset: (page - 1) * (paginationData.value.pageSize || 25), where: where?.value } as any) |
|
|
|
await loadData({ offset: (page - 1) * (paginationData.value.pageSize || 25), where: where?.value } as any) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const addRow = () => { |
|
|
|
const addEmptyRow = (addAfter = formattedData.value.length) => { |
|
|
|
formattedData.value.push({ |
|
|
|
formattedData.value[addAfter] = { |
|
|
|
row: {}, |
|
|
|
row: {}, |
|
|
|
oldRow: {}, |
|
|
|
oldRow: {}, |
|
|
|
rowMeta: { new: true }, |
|
|
|
rowMeta: { new: true }, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deleteRowById = async (id: string) => { |
|
|
|
|
|
|
|
if (!id) { |
|
|
|
|
|
|
|
throw new Error("Delete not allowed for table which doesn't have primary Key") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const res: any = await $api.dbViewRow.delete( |
|
|
|
|
|
|
|
'noco', |
|
|
|
|
|
|
|
project.value.id as string, |
|
|
|
|
|
|
|
meta?.value.id as string, |
|
|
|
|
|
|
|
viewMeta?.value.id as string, |
|
|
|
|
|
|
|
id, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (res.message) { |
|
|
|
|
|
|
|
notification.info({ |
|
|
|
|
|
|
|
message: 'Row delete failed', |
|
|
|
|
|
|
|
description: h('div', { |
|
|
|
|
|
|
|
innerHTML: `<div style="padding:10px 4px">Unable to delete row with ID ${id} because of the following:
|
|
|
|
|
|
|
|
<br><br>${res.message.join('<br>')}<br><br> |
|
|
|
|
|
|
|
Clear the data first & try again</div>`,
|
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deleteRow = async (rowIndex: number) => { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
const row = formattedData.value[rowIndex] |
|
|
|
|
|
|
|
if (row.rowMeta.new) { |
|
|
|
|
|
|
|
const id = meta?.value?.columns |
|
|
|
|
|
|
|
?.filter((c) => c.pk) |
|
|
|
|
|
|
|
.map((c) => row.row[c.title as any]) |
|
|
|
|
|
|
|
.join('___') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deleted = await deleteRowById(id as string) |
|
|
|
|
|
|
|
if (!deleted) { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
formattedData.value.splice(rowIndex, 1) |
|
|
|
|
|
|
|
} catch (e: any) { |
|
|
|
|
|
|
|
notification.error({ |
|
|
|
|
|
|
|
message: 'Failed to delete row', |
|
|
|
|
|
|
|
description: await extractSdkResponseErrorMsg(e), |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deleteSelectedRows = async () => { |
|
|
|
|
|
|
|
let row = formattedData.value.length |
|
|
|
|
|
|
|
while (row--) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
const { row: rowObj, rowMeta } = formattedData.value[row] |
|
|
|
|
|
|
|
if (!rowMeta.selected) { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!rowMeta.new) { |
|
|
|
|
|
|
|
const id = meta?.value?.columns |
|
|
|
|
|
|
|
?.filter((c) => c.pk) |
|
|
|
|
|
|
|
.map((c) => rowObj[c.title as string]) |
|
|
|
|
|
|
|
.join('___') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const successfulDeletion = await deleteRowById(id as string) |
|
|
|
|
|
|
|
if (!successfulDeletion) { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
formattedData.value.splice(row, 1) |
|
|
|
|
|
|
|
} catch (e: any) { |
|
|
|
|
|
|
|
return notification.error({ |
|
|
|
|
|
|
|
message: 'Failed to delete row', |
|
|
|
|
|
|
|
description: await extractSdkResponseErrorMsg(e), |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
data, |
|
|
|
|
|
|
|
loadData, |
|
|
|
loadData, |
|
|
|
paginationData, |
|
|
|
paginationData, |
|
|
|
formattedData, |
|
|
|
formattedData, |
|
|
|
insertRow, |
|
|
|
insertRow, |
|
|
|
updateRowProperty, |
|
|
|
updateRowProperty, |
|
|
|
changePage, |
|
|
|
changePage, |
|
|
|
addRow, |
|
|
|
addEmptyRow, |
|
|
|
selectedRows, |
|
|
|
selectedRows, |
|
|
|
|
|
|
|
deleteRow, |
|
|
|
|
|
|
|
deleteSelectedRows, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|