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.
327 lines
11 KiB
327 lines
11 KiB
2 years ago
|
import type { ColumnType, LinkToAnotherRecordType, PaginatedType, TableType } from 'nocodb-sdk'
|
||
2 years ago
|
import type { ComputedRef, Ref } from 'vue'
|
||
2 years ago
|
import { Modal, message } from 'ant-design-vue'
|
||
2 years ago
|
import { useI18n } from 'vue-i18n'
|
||
2 years ago
|
import {
|
||
2 years ago
|
IsPublicInj,
|
||
2 years ago
|
NOCO,
|
||
|
computed,
|
||
|
extractSdkResponseErrorMsg,
|
||
|
reactive,
|
||
|
ref,
|
||
|
useInjectionState,
|
||
|
useMetas,
|
||
|
useNuxtApp,
|
||
|
useProject,
|
||
|
} from '#imports'
|
||
2 years ago
|
import type { Row } from '~/composables'
|
||
2 years ago
|
import { SharedViewPasswordInj } from '~/context'
|
||
2 years ago
|
|
||
2 years ago
|
interface DataApiResponse {
|
||
|
list: Record<string, any>
|
||
|
pageInfo: PaginatedType
|
||
|
}
|
||
|
|
||
2 years ago
|
/** Store for managing Link to another cells */
|
||
2 years ago
|
const [useProvideLTARStore, useLTARStore] = useInjectionState(
|
||
2 years ago
|
(column: Ref<Required<ColumnType>>, row: Ref<Row>, isNewRow: ComputedRef<boolean> | Ref<boolean>, reloadData = () => {}) => {
|
||
2 years ago
|
// state
|
||
|
const { metas, getMeta } = useMetas()
|
||
|
const { project } = useProject()
|
||
|
const { $api } = useNuxtApp()
|
||
2 years ago
|
const sharedViewPassword = inject(SharedViewPasswordInj, ref(null))
|
||
2 years ago
|
const childrenExcludedList = ref<DataApiResponse | undefined>()
|
||
|
const childrenList = ref<DataApiResponse | undefined>()
|
||
2 years ago
|
const childrenExcludedListPagination = reactive({
|
||
|
page: 1,
|
||
|
query: '',
|
||
|
size: 10,
|
||
|
})
|
||
|
const childrenListPagination = reactive({
|
||
|
page: 1,
|
||
|
query: '',
|
||
|
size: 10,
|
||
|
})
|
||
2 years ago
|
const { t } = useI18n()
|
||
2 years ago
|
|
||
2 years ago
|
const isPublic: boolean = $(inject(IsPublicInj, ref(false)))
|
||
|
|
||
2 years ago
|
const colOptions = $computed(() => column?.value.colOptions as LinkToAnotherRecordType)
|
||
2 years ago
|
|
||
2 years ago
|
const { sharedView } = useSharedView() as Record<string, any>
|
||
2 years ago
|
const projectId = project?.value?.id || sharedView.value?.view?.project_id
|
||
2 years ago
|
|
||
2 years ago
|
// getters
|
||
2 years ago
|
const meta = computed(() => metas?.value?.[column?.value?.fk_model_id as string])
|
||
2 years ago
|
const relatedTableMeta = computed<TableType>(() => {
|
||
2 years ago
|
return metas.value?.[colOptions?.fk_related_model_id as string]
|
||
2 years ago
|
})
|
||
|
|
||
|
const rowId = computed(() =>
|
||
|
meta.value.columns
|
||
|
.filter((c: Required<ColumnType>) => c.pk)
|
||
|
.map((c: Required<ColumnType>) => row?.value?.row?.[c.title])
|
||
|
.join('___'),
|
||
2 years ago
|
)
|
||
|
|
||
2 years ago
|
// actions
|
||
|
const getRelatedTableRowId = (row: Record<string, any>) => {
|
||
|
return relatedTableMeta.value?.columns
|
||
|
?.filter((c) => c.pk)
|
||
|
.map((c) => row?.[c.title as string])
|
||
|
.join('___')
|
||
|
}
|
||
|
|
||
|
const loadRelatedTableMeta = async () => {
|
||
|
await getMeta(colOptions?.fk_related_model_id as string)
|
||
|
}
|
||
|
|
||
|
const relatedTablePrimaryValueProp = computed(() => {
|
||
|
return (relatedTableMeta?.value?.columns?.find((c) => c.pv) || relatedTableMeta?.value?.columns?.[0])?.title
|
||
|
})
|
||
2 years ago
|
|
||
|
const relatedTablePrimaryKeyProps = computed(() => {
|
||
|
return relatedTableMeta?.value?.columns?.filter((c) => c.pk)?.map((c) => c.title) ?? []
|
||
|
})
|
||
2 years ago
|
const primaryValueProp = computed(() => {
|
||
|
return (meta?.value?.columns?.find((c: Required<ColumnType>) => c.pv) || relatedTableMeta?.value?.columns?.[0])?.title
|
||
|
})
|
||
|
|
||
2 years ago
|
const loadChildrenExcludedList = async () => {
|
||
2 years ago
|
try {
|
||
2 years ago
|
if (isPublic) {
|
||
|
const route = useRoute()
|
||
|
childrenExcludedList.value = await $api.public.dataRelationList(
|
||
|
route.params.viewId as string,
|
||
|
column?.value?.id,
|
||
|
{},
|
||
|
{
|
||
|
headers: {
|
||
2 years ago
|
'xc-password': sharedViewPassword.value,
|
||
2 years ago
|
},
|
||
|
query: {
|
||
|
limit: childrenExcludedListPagination.size,
|
||
|
offset: childrenExcludedListPagination.size * (childrenExcludedListPagination.page - 1),
|
||
|
where:
|
||
|
childrenExcludedListPagination.query &&
|
||
|
`(${relatedTablePrimaryValueProp.value},like,${childrenExcludedListPagination.query})`,
|
||
|
fields: [relatedTablePrimaryValueProp.value, ...relatedTablePrimaryKeyProps.value],
|
||
|
} as any,
|
||
|
},
|
||
|
)
|
||
|
|
||
|
/** if new row load all records */
|
||
|
} else if (isNewRow?.value) {
|
||
2 years ago
|
childrenExcludedList.value = await $api.dbTableRow.list(
|
||
|
NOCO,
|
||
2 years ago
|
projectId,
|
||
2 years ago
|
relatedTableMeta?.value?.id as string,
|
||
|
{
|
||
|
limit: childrenExcludedListPagination.size,
|
||
|
offset: childrenExcludedListPagination.size * (childrenExcludedListPagination.page - 1),
|
||
|
where:
|
||
|
childrenExcludedListPagination.query &&
|
||
|
`(${relatedTablePrimaryValueProp.value},like,${childrenExcludedListPagination.query})`,
|
||
|
fields: [relatedTablePrimaryValueProp.value, ...relatedTablePrimaryKeyProps.value],
|
||
|
} as any,
|
||
|
)
|
||
|
} else {
|
||
|
childrenExcludedList.value = await $api.dbTableRow.nestedChildrenExcludedList(
|
||
|
NOCO,
|
||
2 years ago
|
projectId,
|
||
2 years ago
|
meta.value.id,
|
||
|
rowId.value,
|
||
|
colOptions.type as 'mm' | 'hm',
|
||
2 years ago
|
encodeURIComponent(column?.value?.title),
|
||
2 years ago
|
{
|
||
2 years ago
|
limit: String(childrenExcludedListPagination.size),
|
||
|
offset: String(childrenExcludedListPagination.size * (childrenExcludedListPagination.page - 1)),
|
||
|
// todo: where clause is missing from type
|
||
2 years ago
|
where:
|
||
|
childrenExcludedListPagination.query &&
|
||
|
`(${relatedTablePrimaryValueProp.value},like,${childrenExcludedListPagination.query})`,
|
||
2 years ago
|
} as any,
|
||
2 years ago
|
)
|
||
|
}
|
||
2 years ago
|
} catch (e: any) {
|
||
2 years ago
|
message.error(`${t('msg.error.failedToLoadList')}: ${await extractSdkResponseErrorMsg(e)}`)
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
|
|
||
|
const loadChildrenList = async () => {
|
||
2 years ago
|
try {
|
||
2 years ago
|
if (colOptions.type === 'bt') return
|
||
2 years ago
|
|
||
2 years ago
|
if (isPublic) {
|
||
|
childrenList.value = await $api.public.dataNestedList(
|
||
|
sharedView.value?.uuid as string,
|
||
|
rowId.value,
|
||
|
colOptions.type as 'mm' | 'hm',
|
||
|
encodeURIComponent(column?.value?.id),
|
||
|
{
|
||
|
limit: String(childrenListPagination.size),
|
||
|
offset: String(childrenListPagination.size * (childrenListPagination.page - 1)),
|
||
2 years ago
|
where:
|
||
|
childrenListPagination.query && `(${relatedTablePrimaryValueProp.value},like,${childrenListPagination.query})`,
|
||
2 years ago
|
} as any,
|
||
|
)
|
||
2 years ago
|
} else {
|
||
2 years ago
|
childrenList.value = await $api.dbTableRow.nestedList(
|
||
|
NOCO,
|
||
|
(project?.value?.id || sharedView?.value?.view?.project_id) as string,
|
||
|
meta.value.id,
|
||
|
rowId.value,
|
||
|
colOptions.type as 'mm' | 'hm',
|
||
|
encodeURIComponent(column?.value?.title),
|
||
|
{
|
||
|
limit: String(childrenListPagination.size),
|
||
|
offset: String(childrenListPagination.size * (childrenListPagination.page - 1)),
|
||
2 years ago
|
where:
|
||
|
childrenListPagination.query && `(${relatedTablePrimaryValueProp.value},like,${childrenListPagination.query})`,
|
||
2 years ago
|
} as any,
|
||
|
)
|
||
|
}
|
||
2 years ago
|
} catch (e: any) {
|
||
2 years ago
|
message.error(`${t('msg.error.failedToLoadChildrenList')}: ${await extractSdkResponseErrorMsg(e)}`)
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
const deleteRelatedRow = async (row: Record<string, any>, onSuccess?: (row: Record<string, any>) => void) => {
|
||
2 years ago
|
Modal.confirm({
|
||
|
title: 'Do you want to delete the record?',
|
||
|
type: 'warning',
|
||
|
onOk: async () => {
|
||
|
const id = getRelatedTableRowId(row)
|
||
|
try {
|
||
2 years ago
|
$api.dbTableRow.delete(NOCO, projectId, relatedTableMeta.value.id as string, id as string)
|
||
2 years ago
|
reloadData?.()
|
||
2 years ago
|
|
||
|
/** reload child list if not a new row */
|
||
|
if (!isNewRow?.value) {
|
||
|
await loadChildrenList()
|
||
|
}
|
||
|
onSuccess?.(row)
|
||
2 years ago
|
} catch (e: any) {
|
||
2 years ago
|
message.error(`${t('msg.error.deleteFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
|
||
2 years ago
|
}
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
2 years ago
|
const unlink = async (row: Record<string, any>) => {
|
||
|
// const column = meta.columns.find(c => c.id === this.column.colOptions.fk_child_column_id);
|
||
|
// todo: handle if new record
|
||
|
// if (this.isNew) {
|
||
|
// this.$emit('updateCol', this.row, _cn, null);
|
||
|
// this.localState = null;
|
||
|
// this.$emit('update:localState', this.localState);
|
||
|
// return;
|
||
|
// }
|
||
|
// todo: handle bt column if required
|
||
|
// if (column.rqd) {
|
||
|
// this.$toast.info('Unlink is not possible, instead map to another parent.').goAway(3000);
|
||
|
// return;
|
||
|
// }
|
||
2 years ago
|
try {
|
||
|
// todo: audit
|
||
|
await $api.dbTableRow.nestedRemove(
|
||
|
NOCO,
|
||
|
project.value.title as string,
|
||
|
meta.value.title,
|
||
|
rowId.value,
|
||
|
colOptions.type as 'mm' | 'hm',
|
||
2 years ago
|
encodeURIComponent(column?.value?.title),
|
||
2 years ago
|
getRelatedTableRowId(row) as string,
|
||
|
)
|
||
2 years ago
|
} catch (e: any) {
|
||
2 years ago
|
message.error(`${t('msg.error.unlinkFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
|
||
2 years ago
|
}
|
||
2 years ago
|
reloadData?.()
|
||
|
// todo: reload table data and children list
|
||
|
// this.$emit('loadTableData');
|
||
|
// if (this.isForm && this.$refs.childList) {
|
||
|
// this.$refs.childList.loadData();
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
const link = async (row: Record<string, any>) => {
|
||
|
// todo: handle new record
|
||
|
// const pid = this._extractRowId(parent, this.parentMeta);
|
||
|
// const id = this._extractRowId(this.row, this.meta);
|
||
|
// const _cn = this.meta.columns.find(c => c.id === this.column.colOptions.fk_child_column_id).title;
|
||
|
//
|
||
|
// if (this.isNew) {
|
||
|
// const _rcn = this.parentMeta.columns.find(c => c.id === this.column.colOptions.fk_parent_column_id).title;
|
||
|
// this.localState = parent;
|
||
|
// this.$emit('update:localState', this.localState);
|
||
|
// this.$emit('updateCol', this.row, _cn, parent[_rcn]);
|
||
|
// this.newRecordModal = false;
|
||
|
// return;
|
||
|
// }
|
||
2 years ago
|
try {
|
||
|
await $api.dbTableRow.nestedAdd(
|
||
|
NOCO,
|
||
|
project.value.title as string,
|
||
|
meta.value.title as string,
|
||
|
rowId.value,
|
||
|
colOptions.type as 'mm' | 'hm',
|
||
2 years ago
|
encodeURIComponent(column?.value?.title),
|
||
2 years ago
|
getRelatedTableRowId(row) as string,
|
||
|
)
|
||
2 years ago
|
await loadChildrenList()
|
||
2 years ago
|
} catch (e: any) {
|
||
2 years ago
|
message.error(`Linking failed: ${await extractSdkResponseErrorMsg(e)}`)
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
// todo: reload table data and child list
|
||
|
// this.pid = pid;
|
||
|
//
|
||
|
// this.newRecordModal = false;
|
||
|
//
|
||
|
// this.$emit('loadTableData');
|
||
|
// if (this.isForm && this.$refs.childList) {
|
||
|
// this.$refs.childList.loadData();
|
||
|
// }
|
||
|
|
||
|
reloadData?.()
|
||
|
}
|
||
|
|
||
|
// watchers
|
||
|
watch(childrenExcludedListPagination, async () => {
|
||
|
await loadChildrenExcludedList()
|
||
|
})
|
||
|
watch(childrenListPagination, async () => {
|
||
|
await loadChildrenList()
|
||
|
})
|
||
|
|
||
|
return {
|
||
|
relatedTableMeta,
|
||
|
loadRelatedTableMeta,
|
||
|
relatedTablePrimaryValueProp,
|
||
|
childrenExcludedList,
|
||
|
childrenList,
|
||
|
rowId,
|
||
|
childrenExcludedListPagination,
|
||
|
childrenListPagination,
|
||
|
primaryValueProp,
|
||
|
meta,
|
||
|
unlink,
|
||
|
link,
|
||
|
loadChildrenExcludedList,
|
||
|
loadChildrenList,
|
||
|
row,
|
||
2 years ago
|
deleteRelatedRow,
|
||
2 years ago
|
getRelatedTableRowId,
|
||
2 years ago
|
}
|
||
|
},
|
||
|
'ltar-store',
|
||
|
)
|
||
2 years ago
|
|
||
|
export { useProvideLTARStore }
|
||
|
|
||
|
export function useLTARStoreOrThrow() {
|
||
|
const ltarStore = useLTARStore()
|
||
|
if (ltarStore == null) throw new Error('Please call `useLTARStore` on the appropriate parent component')
|
||
|
return ltarStore
|
||
|
}
|