|
|
|
@ -1,10 +1,21 @@
|
|
|
|
|
import type { ColumnType, TableType } from 'nocodb-sdk' |
|
|
|
|
import { notification } from 'ant-design-vue' |
|
|
|
|
import { UITypes } from 'nocodb-sdk' |
|
|
|
|
import type { ColumnType, LinkToAnotherRecordType, TableType } from 'nocodb-sdk' |
|
|
|
|
import type { Ref } from 'vue' |
|
|
|
|
import { useNuxtApp } from '#app' |
|
|
|
|
import { useInjectionState } from '#imports' |
|
|
|
|
import { useMetas } from '~/composables/useMetas' |
|
|
|
|
import { useProject } from '~/composables/useProject' |
|
|
|
|
import type { Row } from '~/composables/useViewData' |
|
|
|
|
import { useVirtualCell } from '~/composables/useVirtualCell' |
|
|
|
|
import { NOCO } from '~/lib' |
|
|
|
|
import { extractPkFromRow, extractSdkResponseErrorMsg } from '~/utils' |
|
|
|
|
|
|
|
|
|
const [useProvideSmartsheetRowStore, useSmartsheetRowStore] = useInjectionState((meta: Ref<TableType>, row: Ref<Row>) => { |
|
|
|
|
const { $api } = useNuxtApp() |
|
|
|
|
const { project } = useProject() |
|
|
|
|
const { metas } = useMetas() |
|
|
|
|
|
|
|
|
|
// state
|
|
|
|
|
const state = ref<Record<string, Record<string, any> | Record<string, any>[] | null>>({}) |
|
|
|
|
|
|
|
|
@ -13,25 +24,75 @@ const [useProvideSmartsheetRowStore, useSmartsheetRowStore] = useInjectionState(
|
|
|
|
|
|
|
|
|
|
// actions
|
|
|
|
|
const addLTARRef = async (value: Record<string, any>, column: ColumnType) => { |
|
|
|
|
const { isHm, isMm } = $(useVirtualCell(ref(column))) |
|
|
|
|
const { isHm, isMm, isBt } = $(useVirtualCell(ref(column))) |
|
|
|
|
if (isHm || isMm) { |
|
|
|
|
state.value[column.title!] = state.value[column.title!] || [] |
|
|
|
|
state.value[column.title!]!.push(value) |
|
|
|
|
} else { |
|
|
|
|
} else if (isBt) { |
|
|
|
|
state.value[column.title!] = value |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// actions
|
|
|
|
|
const removeLTARRef = async (value: Record<string, any>, column: ColumnType) => { |
|
|
|
|
const { isHm, isMm } = $(useVirtualCell(ref(column))) |
|
|
|
|
const { isHm, isMm, isBt } = $(useVirtualCell(ref(column))) |
|
|
|
|
if (isHm || isMm) { |
|
|
|
|
state.value[column.title!]?.splice(state.value[column.title!]?.indexOf(value), 1) |
|
|
|
|
} else { |
|
|
|
|
} else if (isBt) { |
|
|
|
|
state.value[column.title!] = null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const linkRecord = async (rowId: string, relatedRowId: string, column: ColumnType, type) => { |
|
|
|
|
try { |
|
|
|
|
await $api.dbTableRow.nestedAdd( |
|
|
|
|
NOCO, |
|
|
|
|
project.value.title as string, |
|
|
|
|
meta.value.title as string, |
|
|
|
|
rowId, |
|
|
|
|
type, |
|
|
|
|
column.title as string, |
|
|
|
|
relatedRowId, |
|
|
|
|
) |
|
|
|
|
} catch (e) { |
|
|
|
|
notification.error({ |
|
|
|
|
message: 'Linking failed', |
|
|
|
|
description: await extractSdkResponseErrorMsg(e), |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** sync LTAR relations kept in local state */ |
|
|
|
|
const syncLTARRefs = async (row: Record<string, any>) => { |
|
|
|
|
const id = extractPkFromRow(row, meta.value.columns as ColumnType[]) |
|
|
|
|
for (const column of meta?.value?.columns ?? []) { |
|
|
|
|
if (column.uidt !== UITypes.LinkToAnotherRecord) continue |
|
|
|
|
const colOptions = column?.colOptions as LinkToAnotherRecordType |
|
|
|
|
|
|
|
|
|
const { isHm, isMm, isBt } = $(useVirtualCell(ref(column))) |
|
|
|
|
const relatedTableMeta = metas.value?.[colOptions?.fk_related_model_id as string] |
|
|
|
|
|
|
|
|
|
if (isHm || isMm) { |
|
|
|
|
const relatedRows = (state.value?.[column.title!] ?? []) as Record<string, any>[] |
|
|
|
|
for (const relatedRow of relatedRows) { |
|
|
|
|
await linkRecord( |
|
|
|
|
id, |
|
|
|
|
extractPkFromRow(relatedRow, relatedTableMeta.columns as ColumnType[]), |
|
|
|
|
column, |
|
|
|
|
colOptions.type, |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} else if (isBt && state.value?.[column.title!]) { |
|
|
|
|
await linkRecord( |
|
|
|
|
id, |
|
|
|
|
extractPkFromRow(state.value?.[column.title!], relatedTableMeta.columns as ColumnType[]), |
|
|
|
|
column, |
|
|
|
|
colOptions.type, |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
row, |
|
|
|
|
state, |
|
|
|
@ -39,6 +100,7 @@ const [useProvideSmartsheetRowStore, useSmartsheetRowStore] = useInjectionState(
|
|
|
|
|
// todo: use better name
|
|
|
|
|
addLTARRef, |
|
|
|
|
removeLTARRef, |
|
|
|
|
syncLTARRefs, |
|
|
|
|
} |
|
|
|
|
}, 'smartsheet-row-store') |
|
|
|
|
|
|
|
|
|