Add UI for controlling row actions on views, and add support for view row actions in button actions

This commit is contained in:
Andrew Kingston 2024-09-03 10:09:07 +01:00
parent 65af0a7352
commit 232a2829d2
No known key found for this signature in database
5 changed files with 66 additions and 9 deletions

View File

@ -38,7 +38,10 @@
return return
} }
const res = await API.rowActions.fetch(tableId) 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 () => { const createRowAction = async () => {
@ -58,6 +61,23 @@
notifications.error("Error creating row action") 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,
})
}
}
</script> </script>
<DetailPopover title="Row actions"> <DetailPopover title="Row actions">
@ -87,7 +107,10 @@
<svelte:fragment slot="right"> <svelte:fragment slot="right">
{#if isView} {#if isView}
<span> <span>
<Toggle /> <Toggle
value={action.enabled}
on:change={e => toggleAction(action, e.detail)}
/>
</span> </span>
{/if} {/if}
</svelte:fragment> </svelte:fragment>

View File

@ -15,10 +15,13 @@
})) }))
$: viewOptions = $viewsV2.list.map(view => ({ $: viewOptions = $viewsV2.list.map(view => ({
label: view.name, label: view.name,
tableId: view.tableId,
resourceId: view.id, resourceId: view.id,
})) }))
$: console.log($viewsV2.list)
$: datasourceOptions = [...(tableOptions || []), ...(viewOptions || [])] $: datasourceOptions = [...(tableOptions || []), ...(viewOptions || [])]
$: fetchRowActions(parameters.resourceId) $: resourceId = parameters.resourceId
$: fetchRowActions(resourceId)
$: rowActionOptions = rowActions.map(action => ({ $: rowActionOptions = rowActions.map(action => ({
label: action.name, label: action.name,
value: action.id, value: action.id,
@ -30,8 +33,15 @@
return return
} }
try { try {
const res = await API.rowActions.fetch(resourceId) const isView = resourceId.startsWith("view_")
rowActions = Object.values(res || {}) 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) { } catch (err) {
console.error(err) console.error(err)
rowActions = [] rowActions = []

View File

@ -63,7 +63,7 @@
onClick: async row => { onClick: async row => {
await API.rowActions.trigger({ await API.rowActions.trigger({
rowActionId: action.id, rowActionId: action.id,
tableId: id, sourceId: id,
rowId: row._id, rowId: row._id,
}) })
}, },

View File

@ -497,7 +497,7 @@ const rowActionHandler = async action => {
const { resourceId, rowId, rowActionId } = action.parameters const { resourceId, rowId, rowActionId } = action.parameters
await API.rowActions.trigger({ await API.rowActions.trigger({
rowActionId, rowActionId,
tableId: resourceId, sourceId: resourceId,
rowId, rowId,
}) })
} }

View File

@ -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. * Triggers a row action.
* @param tableId the ID of the table * @param tableId the ID of the table
* @param rowActionId the ID of the row action to trigger * @param rowActionId the ID of the row action to trigger
*/ */
trigger: async ({ tableId, rowActionId, rowId }) => { trigger: async ({ sourceId, rowActionId, rowId }) => {
return await API.post({ return await API.post({
url: `/api/tables/${tableId}/actions/${rowActionId}/trigger`, url: `/api/tables/${sourceId}/actions/${rowActionId}/trigger`,
body: { body: {
rowId, rowId,
}, },