From 232a2829d2495a60ac108743bb391fbb5f5478bc Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 3 Sep 2024 10:09:07 +0100 Subject: [PATCH] Add UI for controlling row actions on views, and add support for view row actions in button actions --- .../buttons/grid/GridRowActionsButton.svelte | 27 ++++++++++++++++-- .../actions/RowAction.svelte | 16 +++++++++-- .../data/table/[tableId]/index.svelte | 2 +- packages/client/src/utils/buttonActions.js | 2 +- packages/frontend-core/src/api/rowActions.js | 28 +++++++++++++++++-- 5 files changed, 66 insertions(+), 9 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/buttons/grid/GridRowActionsButton.svelte b/packages/builder/src/components/backend/DataTable/buttons/grid/GridRowActionsButton.svelte index a6a7c930f5..c44c2d0f1c 100644 --- a/packages/builder/src/components/backend/DataTable/buttons/grid/GridRowActionsButton.svelte +++ b/packages/builder/src/components/backend/DataTable/buttons/grid/GridRowActionsButton.svelte @@ -38,7 +38,10 @@ return } const res = await API.rowActions.fetch(tableId) - rowActions = Object.values(res || {}) + rowActions = Object.values(res || {}).map(action => ({ + ...action, + enabled: !isView || action.allowedViews?.includes(ds.id), + })) } const createRowAction = async () => { @@ -58,6 +61,23 @@ notifications.error("Error creating row action") } } + + const toggleAction = async (action, enabled) => { + console.log(action, enabled) + if (enabled) { + await API.rowActions.enableView({ + tableId, + rowActionId: action.id, + viewId: ds.id, + }) + } else { + await API.rowActions.disableView({ + tableId, + rowActionId: action.id, + viewId: ds.id, + }) + } + } @@ -87,7 +107,10 @@ {#if isView} - + toggleAction(action, e.detail)} + /> {/if} diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/RowAction.svelte b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/RowAction.svelte index 866dcd2972..427a86b3f8 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/RowAction.svelte +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/RowAction.svelte @@ -15,10 +15,13 @@ })) $: viewOptions = $viewsV2.list.map(view => ({ label: view.name, + tableId: view.tableId, resourceId: view.id, })) + $: console.log($viewsV2.list) $: datasourceOptions = [...(tableOptions || []), ...(viewOptions || [])] - $: fetchRowActions(parameters.resourceId) + $: resourceId = parameters.resourceId + $: fetchRowActions(resourceId) $: rowActionOptions = rowActions.map(action => ({ label: action.name, value: action.id, @@ -30,8 +33,15 @@ return } try { - const res = await API.rowActions.fetch(resourceId) - rowActions = Object.values(res || {}) + const isView = resourceId.startsWith("view_") + let tableId = resourceId + if (isView) { + tableId = viewOptions.find(x => x.resourceId === resourceId).tableId + } + const res = await API.rowActions.fetch(tableId) + rowActions = Object.values(res || {}).filter(action => { + return !isView || action.allowedViews?.includes(resourceId) + }) } catch (err) { console.error(err) rowActions = [] diff --git a/packages/builder/src/pages/builder/app/[application]/data/table/[tableId]/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/table/[tableId]/index.svelte index e9b2b4853f..15b4303473 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/table/[tableId]/index.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/table/[tableId]/index.svelte @@ -63,7 +63,7 @@ onClick: async row => { await API.rowActions.trigger({ rowActionId: action.id, - tableId: id, + sourceId: id, rowId: row._id, }) }, diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index 4dae0ad273..847a32116d 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -497,7 +497,7 @@ const rowActionHandler = async action => { const { resourceId, rowId, rowActionId } = action.parameters await API.rowActions.trigger({ rowActionId, - tableId: resourceId, + sourceId: resourceId, rowId, }) } diff --git a/packages/frontend-core/src/api/rowActions.js b/packages/frontend-core/src/api/rowActions.js index 85ae4f6b96..4c0cd5f58b 100644 --- a/packages/frontend-core/src/api/rowActions.js +++ b/packages/frontend-core/src/api/rowActions.js @@ -50,14 +50,38 @@ export const buildRowActionEndpoints = API => ({ }) }, + /** + * Enables a row action for a certain view + * @param tableId the ID of the parent table + * @param rowActionId the ID of the row action + * @param viewId the ID of the view + */ + enableView: async ({ tableId, rowActionId, viewId }) => { + return await API.post({ + url: `/api/tables/${tableId}/actions/${rowActionId}/permissions/${viewId}`, + }) + }, + + /** + * Disables a row action for a certain view + * @param tableId the ID of the parent table + * @param rowActionId the ID of the row action + * @param viewId the ID of the view + */ + disableView: async ({ tableId, rowActionId, viewId }) => { + return await API.delete({ + url: `/api/tables/${tableId}/actions/${rowActionId}/permissions/${viewId}`, + }) + }, + /** * Triggers a row action. * @param tableId the ID of the table * @param rowActionId the ID of the row action to trigger */ - trigger: async ({ tableId, rowActionId, rowId }) => { + trigger: async ({ sourceId, rowActionId, rowId }) => { return await API.post({ - url: `/api/tables/${tableId}/actions/${rowActionId}/trigger`, + url: `/api/tables/${sourceId}/actions/${rowActionId}/trigger`, body: { rowId, },