Browse Source

Merge pull request #9077 from nocodb/nc-feat/3424-prefill-links

Nc feat/3424 prefill links
pull/9081/head
Pranav C 4 months ago committed by GitHub
parent
commit
d94af332b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 60
      packages/nc-gui/composables/useSharedFormViewStore.ts

60
packages/nc-gui/composables/useSharedFormViewStore.ts

@ -46,7 +46,7 @@ const [useProvideSharedFormStore, useSharedFormStore] = useInjectionState((share
const { api, isLoading } = useApi() const { api, isLoading } = useApi()
const { metas, setMeta } = useMetas() const { metas, setMeta, getMeta } = useMetas()
const baseStore = useBase() const baseStore = useBase()
const { base, sqlUis } = storeToRefs(baseStore) const { base, sqlUis } = storeToRefs(baseStore)
@ -168,7 +168,7 @@ const [useProvideSharedFormStore, useSharedFormStore] = useInjectionState((share
basesUser.value.set(viewMeta.base_id, viewMeta.users) basesUser.value.set(viewMeta.base_id, viewMeta.users)
} }
handlePreFillForm() await handlePreFillForm()
} catch (e: any) { } catch (e: any) {
const error = await extractSdkResponseErrorMsgv2(e) const error = await extractSdkResponseErrorMsgv2(e)
@ -343,25 +343,36 @@ const [useProvideSharedFormStore, useSharedFormStore] = useInjectionState((share
clearValidate() clearValidate()
} }
function handlePreFillForm() { async function handlePreFillForm() {
if (Object.keys(route.query || {}).length && sharedViewMeta.value.preFillEnabled) { if (Object.keys(route.query || {}).length && sharedViewMeta.value.preFillEnabled) {
columns.value = (columns.value || []).map((c) => { columns.value = await Promise.all(
(columns.value || []).map(async (c) => {
const queryParam = route.query[c.title as string] || route.query[encodeURIComponent(c.title as string)] const queryParam = route.query[c.title as string] || route.query[encodeURIComponent(c.title as string)]
if ( if (
!c.title || !c.title ||
!queryParam || !queryParam ||
isSystemColumn(c) || isSystemColumn(c) ||
isVirtualCol(c) || (isVirtualCol(c) && !isLinksOrLTAR(c)) ||
isAttachment(c) || isAttachment(c) ||
c.uidt === UITypes.SpecificDBType c.uidt === UITypes.SpecificDBType
) { ) {
return c return c
} }
const decodedQueryParam = Array.isArray(queryParam)
? queryParam.map((qp) => decodeURIComponent(qp as string).trim())
: decodeURIComponent(queryParam as string).trim()
const preFillValue = getPreFillValue(c, decodeURIComponent(queryParam as string).trim()) const preFillValue = await getPreFillValue(c, decodedQueryParam)
if (preFillValue !== undefined) { if (preFillValue !== undefined) {
if (isLinksOrLTAR(c)) {
// Prefill Link to another record / Links form state
additionalState.value[c.title] = preFillValue
} else {
// Prefill form state // Prefill form state
formState.value[c.title] = preFillValue formState.value[c.title] = preFillValue
}
// preFilledformState will be used in clear form to fill the prefilled data // preFilledformState will be used in clear form to fill the prefilled data
preFilledformState.value[c.title] = preFillValue preFilledformState.value[c.title] = preFillValue
@ -379,7 +390,8 @@ const [useProvideSharedFormStore, useSharedFormStore] = useInjectionState((share
} }
return c return c
}) }),
)
} }
} }
@ -387,7 +399,7 @@ const [useProvideSharedFormStore, useSharedFormStore] = useInjectionState((share
return (c?.source_id ? sqlUis.value[c?.source_id] : Object.values(sqlUis.value)[0])?.getAbstractType(c) return (c?.source_id ? sqlUis.value[c?.source_id] : Object.values(sqlUis.value)[0])?.getAbstractType(c)
} }
function getPreFillValue(c: ColumnType, value: string) { async function getPreFillValue(c: ColumnType, value: string) {
let preFillValue: any let preFillValue: any
switch (c.uidt) { switch (c.uidt) {
case UITypes.SingleSelect: case UITypes.SingleSelect:
@ -515,6 +527,14 @@ const [useProvideSharedFormStore, useSharedFormStore] = useInjectionState((share
} }
case UITypes.LinkToAnotherRecord: case UITypes.LinkToAnotherRecord:
case UITypes.Links: { case UITypes.Links: {
const values = Array.isArray(value) ? value : value.split(',')
const rows = await loadLinkedRecords(c, values)
preFillValue = rows
// if bt/oo then extract object from array
if (c.colOptions?.type === RelationTypes.BELONGS_TO || c.colOptions?.type === RelationTypes.ONE_TO_ONE) {
preFillValue = preFillValue[0]
}
// Todo: create an api which will fetch query params records and then autofill records // Todo: create an api which will fetch query params records and then autofill records
break break
} }
@ -532,6 +552,30 @@ const [useProvideSharedFormStore, useSharedFormStore] = useInjectionState((share
return preFillValue return preFillValue
} }
async function loadLinkedRecords(column: ColumnType, ids: string[]) {
const relatedMeta = await getMeta((column.colOptions as LinkToAnotherRecordType)?.fk_related_model_id)
const pkCol = relatedMeta?.columns?.find((col) => col.pk)
const pvCol = relatedMeta?.columns?.find((col) => col.pv)
return (
await api.public.dataRelationList(
route.params.viewId as string,
column.id,
{},
{
headers: {
'xc-password': password.value,
},
query: {
limit: Math.max(25, ids.length),
where: `(${pkCol.title},in,${ids.join(',')})`,
fields: [pkCol.title, pvCol.title],
},
},
)
)?.list
}
let intvl: NodeJS.Timeout let intvl: NodeJS.Timeout
/** reset form if show_blank_form is true */ /** reset form if show_blank_form is true */
watch(submitted, (nextVal) => { watch(submitted, (nextVal) => {

Loading…
Cancel
Save