|
|
@ -20,123 +20,126 @@ import { |
|
|
|
useVirtualCell, |
|
|
|
useVirtualCell, |
|
|
|
} from '#imports' |
|
|
|
} from '#imports' |
|
|
|
|
|
|
|
|
|
|
|
const [useProvideSmartsheetRowStore, useSmartsheetRowStore] = useInjectionState((meta: Ref<TableType>, row: MaybeRef<Row>) => { |
|
|
|
const [useProvideSmartsheetRowStore, useSmartsheetRowStore] = useInjectionState( |
|
|
|
const { $api } = useNuxtApp() |
|
|
|
(meta: Ref<TableType | undefined>, row: MaybeRef<Row>) => { |
|
|
|
|
|
|
|
const { $api } = useNuxtApp() |
|
|
|
|
|
|
|
|
|
|
|
const { t } = useI18n() |
|
|
|
const { t } = useI18n() |
|
|
|
|
|
|
|
|
|
|
|
const { project } = useProject() |
|
|
|
const { project } = useProject() |
|
|
|
|
|
|
|
|
|
|
|
const { metas } = useMetas() |
|
|
|
const { metas } = useMetas() |
|
|
|
|
|
|
|
|
|
|
|
const currentRow = ref(row) |
|
|
|
const currentRow = ref(row) |
|
|
|
|
|
|
|
|
|
|
|
// state
|
|
|
|
// state
|
|
|
|
const state = ref<Record<string, Record<string, any> | Record<string, any>[] | null>>({}) |
|
|
|
const state = ref<Record<string, Record<string, any> | Record<string, any>[] | null>>({}) |
|
|
|
|
|
|
|
|
|
|
|
// getters
|
|
|
|
// getters
|
|
|
|
const isNew = computed(() => unref(row).rowMeta?.new ?? false) |
|
|
|
const isNew = computed(() => unref(row).rowMeta?.new ?? false) |
|
|
|
|
|
|
|
|
|
|
|
// actions
|
|
|
|
// actions
|
|
|
|
const addLTARRef = async (value: Record<string, any>, column: ColumnType) => { |
|
|
|
const addLTARRef = async (value: Record<string, any>, column: ColumnType) => { |
|
|
|
const { isHm, isMm, isBt } = $(useVirtualCell(ref(column))) |
|
|
|
const { isHm, isMm, isBt } = $(useVirtualCell(ref(column))) |
|
|
|
if (isHm || isMm) { |
|
|
|
if (isHm || isMm) { |
|
|
|
if (!state.value[column.title!]) state.value[column.title!] = [] |
|
|
|
if (!state.value[column.title!]) state.value[column.title!] = [] |
|
|
|
|
|
|
|
|
|
|
|
if (state.value[column.title!]!.find((ln: Record<string, any>) => deepCompare(ln, value))) { |
|
|
|
if (state.value[column.title!]!.find((ln: Record<string, any>) => deepCompare(ln, value))) { |
|
|
|
// This value is already in the list
|
|
|
|
// This value is already in the list
|
|
|
|
return message.info(t('msg.info.valueAlreadyInList')) |
|
|
|
return message.info(t('msg.info.valueAlreadyInList')) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
state.value[column.title!]!.push(value) |
|
|
|
state.value[column.title!]!.push(value) |
|
|
|
} else if (isBt) { |
|
|
|
} else if (isBt) { |
|
|
|
state.value[column.title!] = value |
|
|
|
state.value[column.title!] = value |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// actions
|
|
|
|
|
|
|
|
const removeLTARRef = async (value: Record<string, any>, column: ColumnType) => { |
|
|
|
|
|
|
|
const { isHm, isMm, isBt } = $(useVirtualCell(ref(column))) |
|
|
|
|
|
|
|
if (isHm || isMm) { |
|
|
|
|
|
|
|
state.value[column.title!]?.splice(state.value[column.title!]?.indexOf(value), 1) |
|
|
|
|
|
|
|
} else if (isBt) { |
|
|
|
|
|
|
|
state.value[column.title!] = null |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const linkRecord = async (rowId: string, relatedRowId: string, column: ColumnType, type: RelationTypes) => { |
|
|
|
// actions
|
|
|
|
try { |
|
|
|
const removeLTARRef = async (value: Record<string, any>, column: ColumnType) => { |
|
|
|
await $api.dbTableRow.nestedAdd( |
|
|
|
const { isHm, isMm, isBt } = $(useVirtualCell(ref(column))) |
|
|
|
NOCO, |
|
|
|
if (isHm || isMm) { |
|
|
|
project.value.title as string, |
|
|
|
state.value[column.title!]?.splice(state.value[column.title!]?.indexOf(value), 1) |
|
|
|
meta.value.title as string, |
|
|
|
} else if (isBt) { |
|
|
|
rowId, |
|
|
|
state.value[column.title!] = null |
|
|
|
type as 'mm' | 'hm', |
|
|
|
} |
|
|
|
column.title as string, |
|
|
|
|
|
|
|
relatedRowId, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} catch (e: any) { |
|
|
|
|
|
|
|
message.error(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 linkRecord = async (rowId: string, relatedRowId: string, column: ColumnType, type: RelationTypes) => { |
|
|
|
const relatedTableMeta = metas.value?.[colOptions?.fk_related_model_id as string] |
|
|
|
try { |
|
|
|
|
|
|
|
await $api.dbTableRow.nestedAdd( |
|
|
|
|
|
|
|
NOCO, |
|
|
|
|
|
|
|
project.value.title as string, |
|
|
|
|
|
|
|
meta.value?.title as string, |
|
|
|
|
|
|
|
rowId, |
|
|
|
|
|
|
|
type as 'mm' | 'hm', |
|
|
|
|
|
|
|
column.title as string, |
|
|
|
|
|
|
|
relatedRowId, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} catch (e: any) { |
|
|
|
|
|
|
|
message.error(await extractSdkResponseErrorMsg(e)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (isHm || isMm) { |
|
|
|
/** sync LTAR relations kept in local state */ |
|
|
|
const relatedRows = (state.value?.[column.title!] ?? []) as Record<string, any>[] |
|
|
|
const syncLTARRefs = async (row: Record<string, any>) => { |
|
|
|
for (const relatedRow of relatedRows) { |
|
|
|
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 as RelationTypes, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else if (isBt && state?.value?.[column.title!]) { |
|
|
|
await linkRecord( |
|
|
|
await linkRecord( |
|
|
|
id, |
|
|
|
id, |
|
|
|
extractPkFromRow(relatedRow, relatedTableMeta.columns as ColumnType[]), |
|
|
|
extractPkFromRow(state.value?.[column.title!] as Record<string, any>, relatedTableMeta.columns as ColumnType[]), |
|
|
|
column, |
|
|
|
column, |
|
|
|
colOptions.type as RelationTypes, |
|
|
|
colOptions.type as RelationTypes, |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (isBt && state?.value?.[column.title!]) { |
|
|
|
|
|
|
|
await linkRecord( |
|
|
|
|
|
|
|
id, |
|
|
|
|
|
|
|
extractPkFromRow(state.value?.[column.title!] as Record<string, any>, relatedTableMeta.columns as ColumnType[]), |
|
|
|
|
|
|
|
column, |
|
|
|
|
|
|
|
colOptions.type as RelationTypes, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const loadRow = async () => { |
|
|
|
const loadRow = async () => { |
|
|
|
const record = await $api.dbTableRow.read( |
|
|
|
const record = await $api.dbTableRow.read( |
|
|
|
NOCO, |
|
|
|
NOCO, |
|
|
|
project.value?.id as string, |
|
|
|
project?.value?.id as string, |
|
|
|
meta.value?.title as string, |
|
|
|
meta.value.title, |
|
|
|
extractPkFromRow(ref(row).value?.row, meta.value?.columns as ColumnType[]), |
|
|
|
extractPkFromRow(ref(row).value?.row, meta.value.columns as ColumnType[]), |
|
|
|
) |
|
|
|
) |
|
|
|
Object.assign(ref(row).value, { |
|
|
|
Object.assign(ref(row).value, { |
|
|
|
row: record, |
|
|
|
row: record, |
|
|
|
oldRow: { ...record }, |
|
|
|
oldRow: { ...record }, |
|
|
|
rowMeta: {}, |
|
|
|
rowMeta: {}, |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
row, |
|
|
|
row, |
|
|
|
state, |
|
|
|
state, |
|
|
|
isNew, |
|
|
|
isNew, |
|
|
|
// todo: use better name
|
|
|
|
// todo: use better name
|
|
|
|
addLTARRef, |
|
|
|
addLTARRef, |
|
|
|
removeLTARRef, |
|
|
|
removeLTARRef, |
|
|
|
syncLTARRefs, |
|
|
|
syncLTARRefs, |
|
|
|
loadRow, |
|
|
|
loadRow, |
|
|
|
currentRow, |
|
|
|
currentRow, |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, 'smartsheet-row-store') |
|
|
|
'smartsheet-row-store', |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
export { useProvideSmartsheetRowStore } |
|
|
|
export { useProvideSmartsheetRowStore } |
|
|
|
|
|
|
|
|
|
|
|