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. 112
      packages/nc-gui/composables/useSharedFormViewStore.ts

112
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,43 +343,55 @@ 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(
const queryParam = route.query[c.title as string] || route.query[encodeURIComponent(c.title as string)] (columns.value || []).map(async (c) => {
if ( const queryParam = route.query[c.title as string] || route.query[encodeURIComponent(c.title as string)]
!c.title ||
!queryParam ||
isSystemColumn(c) ||
isVirtualCol(c) ||
isAttachment(c) ||
c.uidt === UITypes.SpecificDBType
) {
return c
}
const preFillValue = getPreFillValue(c, decodeURIComponent(queryParam as string).trim()) if (
if (preFillValue !== undefined) { !c.title ||
// Prefill form state !queryParam ||
formState.value[c.title] = preFillValue isSystemColumn(c) ||
// preFilledformState will be used in clear form to fill the prefilled data (isVirtualCol(c) && !isLinksOrLTAR(c)) ||
preFilledformState.value[c.title] = preFillValue isAttachment(c) ||
c.uidt === UITypes.SpecificDBType
// Update column ) {
switch (sharedViewMeta.value.preFilledMode) { return c
case PreFilledMode.Hidden: { }
c.show = false const decodedQueryParam = Array.isArray(queryParam)
break ? queryParam.map((qp) => decodeURIComponent(qp as string).trim())
: decodeURIComponent(queryParam as string).trim()
const preFillValue = await getPreFillValue(c, decodedQueryParam)
if (preFillValue !== undefined) {
if (isLinksOrLTAR(c)) {
// Prefill Link to another record / Links form state
additionalState.value[c.title] = preFillValue
} else {
// Prefill form state
formState.value[c.title] = preFillValue
} }
case PreFilledMode.Locked: {
c.read_only = true // preFilledformState will be used in clear form to fill the prefilled data
break preFilledformState.value[c.title] = preFillValue
// Update column
switch (sharedViewMeta.value.preFilledMode) {
case PreFilledMode.Hidden: {
c.show = false
break
}
case PreFilledMode.Locked: {
c.read_only = true
break
}
} }
} }
}
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