From cb689d08e046961b7a7048b005673631f0a851b6 Mon Sep 17 00:00:00 2001 From: Ramesh Mane <101566080+rameshmane7218@users.noreply.github.com> Date: Tue, 26 Nov 2024 05:46:56 +0000 Subject: [PATCH] fix: custom url with prefill form query param support --- .../dlg/share-and-collaborate/SharePage.vue | 3 +- .../index/[typeOrId]/form/[viewId]/index.vue | 14 ++++- packages/nocodb/src/services/views.service.ts | 63 ++++++++++--------- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/packages/nc-gui/components/dlg/share-and-collaborate/SharePage.vue b/packages/nc-gui/components/dlg/share-and-collaborate/SharePage.vue index 19c55ec78c..ce89c69b88 100644 --- a/packages/nc-gui/components/dlg/share-and-collaborate/SharePage.vue +++ b/packages/nc-gui/components/dlg/share-and-collaborate/SharePage.vue @@ -292,7 +292,8 @@ async function updateSharedView(custUrl = undefined) { const res = await $api.dbViewShare.update(activeView.value.id!, { meta, password: activeView.value.password, - ...(custUrl !== undefined ? { custom_url_path: custUrl ?? null, original_url: sharedViewUrl(false) } : {}), + original_url: sharedViewUrl(false), + ...(custUrl !== undefined ? { custom_url_path: custUrl ?? null } : {}), }) if (custUrl !== undefined) { diff --git a/packages/nc-gui/pages/index/[typeOrId]/form/[viewId]/index.vue b/packages/nc-gui/pages/index/[typeOrId]/form/[viewId]/index.vue index df1a698f56..10fa43ea95 100644 --- a/packages/nc-gui/pages/index/[typeOrId]/form/[viewId]/index.vue +++ b/packages/nc-gui/pages/index/[typeOrId]/form/[viewId]/index.vue @@ -13,9 +13,19 @@ onMounted(() => { const shouldRedirect = (to: string) => { if (sharedViewMeta.value.surveyMode) { - if (!to.includes('survey')) navigateTo(`/nc/form/${route.params.viewId}/survey`) + if (!to.includes('survey')) { + navigateTo({ + path: `/nc/form/${route.params.viewId}/survey`, + query: route.query, + }) + } } else { - if (to.includes('survey')) navigateTo(`/nc/form/${route.params.viewId}`) + if (to.includes('survey')) { + navigateTo({ + path: `/nc/form/${route.params.viewId}`, + query: route.query, + }) + } } } diff --git a/packages/nocodb/src/services/views.service.ts b/packages/nocodb/src/services/views.service.ts index ef3958b2af..d6ea5ca209 100644 --- a/packages/nocodb/src/services/views.service.ts +++ b/packages/nocodb/src/services/views.service.ts @@ -278,38 +278,45 @@ export class ViewsService { NcError.viewNotFound(param.viewId); } - let customUrl: CustomUrl | undefined; + let customUrl: CustomUrl | undefined = await CustomUrl.get({ + view_id: view.id, + id: view.fk_custom_url_id, + }); - if (param.sharedView.custom_url_path !== undefined) { - customUrl = await CustomUrl.get({ - view_id: view.id, - id: view.fk_custom_url_id, - }); + // Update an existing custom URL if it exists + if (customUrl?.id) { + if (param.sharedView.custom_url_path || param.sharedView.original_url) { + // Prepare updated fields conditionally + const updates: Partial = {}; + + if (param.sharedView.custom_url_path !== undefined) { + updates.custom_path = param.sharedView.custom_url_path; + } - if (customUrl?.id) { - if (param.sharedView.custom_url_path) { - await CustomUrl.update(view.fk_custom_url_id, { - custom_path: param.sharedView.custom_url_path, - ...(customUrl.original_path !== param.sharedView.original_url - ? { - original_path: param.sharedView.original_url, - } - : {}), - }); - } else { - await CustomUrl.delete({ id: view.fk_custom_url_id as string }); - customUrl = undefined; + if (customUrl.original_path !== param.sharedView.original_url) { + updates.original_path = param.sharedView.original_url; } - } else if (param.sharedView.custom_url_path) { - customUrl = await CustomUrl.insert({ - fk_workspace_id: view.fk_workspace_id, - base_id: view.base_id, - fk_model_id: view.fk_model_id, - view_id: view.id, - original_path: param.sharedView.original_url, - custom_path: param.sharedView.custom_url_path, - }); + + // Perform the update if there are changes + if (Object.keys(updates).length > 0) { + await CustomUrl.update(view.fk_custom_url_id, updates); + } + } else if (param.sharedView.custom_url_path !== undefined) { + // Delete the custom URL if only the custom path is undefined + await CustomUrl.delete({ id: view.fk_custom_url_id as string }); + customUrl = undefined; } + } else if (param.sharedView.custom_url_path) { + // Insert a new custom URL if it doesn't exist + + customUrl = await CustomUrl.insert({ + fk_workspace_id: view.fk_workspace_id, + base_id: view.base_id, + fk_model_id: view.fk_model_id, + view_id: view.id, + original_path: param.sharedView.original_url, + custom_path: param.sharedView.custom_url_path, + }); } const result = await View.update(context, param.viewId, {