mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.9 KiB
132 lines
3.9 KiB
2 years ago
|
import { message } from 'ant-design-vue'
|
||
2 years ago
|
import { UITypes } from 'nocodb-sdk'
|
||
2 years ago
|
import type { ColumnType, LinkToAnotherRecordType, RelationTypes, TableType } from 'nocodb-sdk'
|
||
2 years ago
|
import type { Ref } from 'vue'
|
||
2 years ago
|
import type { MaybeRef } from '@vueuse/core'
|
||
2 years ago
|
import { useI18n } from 'vue-i18n'
|
||
2 years ago
|
import type { Row } from './useViewData'
|
||
2 years ago
|
import {
|
||
|
NOCO,
|
||
|
computed,
|
||
|
deepCompare,
|
||
|
extractPkFromRow,
|
||
|
extractSdkResponseErrorMsg,
|
||
|
ref,
|
||
|
unref,
|
||
|
useInjectionState,
|
||
|
useMetas,
|
||
|
useNuxtApp,
|
||
|
useProject,
|
||
|
useVirtualCell,
|
||
|
} from '#imports'
|
||
2 years ago
|
|
||
2 years ago
|
const [useProvideSmartsheetRowStore, useSmartsheetRowStore] = useInjectionState((meta: Ref<TableType>, row: MaybeRef<Row>) => {
|
||
2 years ago
|
const { $api } = useNuxtApp()
|
||
2 years ago
|
|
||
2 years ago
|
const { t } = useI18n()
|
||
|
|
||
2 years ago
|
const { project } = useProject()
|
||
2 years ago
|
|
||
2 years ago
|
const { metas } = useMetas()
|
||
|
|
||
2 years ago
|
// state
|
||
2 years ago
|
const state = ref<Record<string, Record<string, any> | Record<string, any>[] | null>>({})
|
||
2 years ago
|
|
||
|
// getters
|
||
2 years ago
|
const isNew = computed(() => unref(row).rowMeta?.new ?? false)
|
||
2 years ago
|
|
||
|
// actions
|
||
2 years ago
|
const addLTARRef = async (value: Record<string, any>, column: ColumnType) => {
|
||
2 years ago
|
const { isHm, isMm, isBt } = $(useVirtualCell(ref(column)))
|
||
2 years ago
|
if (isHm || isMm) {
|
||
2 years ago
|
if (!state.value[column.title!]) state.value[column.title!] = []
|
||
2 years ago
|
|
||
|
if (state.value[column.title!]!.find((ln: Record<string, any>) => deepCompare(ln, value))) {
|
||
2 years ago
|
// This value is already in the list
|
||
|
return message.info(t('msg.info.valueAlreadyInList'))
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
state.value[column.title!]!.push(value)
|
||
2 years ago
|
} else if (isBt) {
|
||
2 years ago
|
state.value[column.title!] = value
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
// actions
|
||
|
const removeLTARRef = async (value: Record<string, any>, column: ColumnType) => {
|
||
2 years ago
|
const { isHm, isMm, isBt } = $(useVirtualCell(ref(column)))
|
||
2 years ago
|
if (isHm || isMm) {
|
||
|
state.value[column.title!]?.splice(state.value[column.title!]?.indexOf(value), 1)
|
||
2 years ago
|
} else if (isBt) {
|
||
2 years ago
|
state.value[column.title!] = null
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
const linkRecord = async (rowId: string, relatedRowId: string, column: ColumnType, type: RelationTypes) => {
|
||
2 years ago
|
try {
|
||
|
await $api.dbTableRow.nestedAdd(
|
||
|
NOCO,
|
||
|
project.value.title as string,
|
||
|
meta.value.title as string,
|
||
|
rowId,
|
||
2 years ago
|
type as 'mm' | 'hm',
|
||
2 years ago
|
column.title as string,
|
||
|
relatedRowId,
|
||
|
)
|
||
2 years ago
|
} catch (e: any) {
|
||
2 years ago
|
message.error(await extractSdkResponseErrorMsg(e))
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/** 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) {
|
||
2 years ago
|
await linkRecord(
|
||
|
id,
|
||
|
extractPkFromRow(relatedRow, relatedTableMeta.columns as ColumnType[]),
|
||
|
column,
|
||
|
colOptions.type as RelationTypes,
|
||
|
)
|
||
2 years ago
|
}
|
||
2 years ago
|
} else if (isBt && state?.value?.[column.title!]) {
|
||
2 years ago
|
await linkRecord(
|
||
|
id,
|
||
2 years ago
|
extractPkFromRow(state.value?.[column.title!] as Record<string, any>, relatedTableMeta.columns as ColumnType[]),
|
||
2 years ago
|
column,
|
||
2 years ago
|
colOptions.type as RelationTypes,
|
||
2 years ago
|
)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
return {
|
||
|
row,
|
||
2 years ago
|
state,
|
||
2 years ago
|
isNew,
|
||
2 years ago
|
// todo: use better name
|
||
|
addLTARRef,
|
||
2 years ago
|
removeLTARRef,
|
||
2 years ago
|
syncLTARRefs,
|
||
2 years ago
|
}
|
||
|
}, 'smartsheet-row-store')
|
||
|
|
||
|
export { useProvideSmartsheetRowStore }
|
||
|
|
||
|
export function useSmartsheetRowStoreOrThrow() {
|
||
|
const smartsheetRowStore = useSmartsheetRowStore()
|
||
2 years ago
|
|
||
2 years ago
|
if (smartsheetRowStore == null) throw new Error('Please call `useSmartsheetRowStore` on the appropriate parent component')
|
||
2 years ago
|
|
||
2 years ago
|
return smartsheetRowStore
|
||
|
}
|