From ea84af782d9c76cc2bc87460b30818cec671faab Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 3 Dec 2024 10:37:15 +0000 Subject: [PATCH] Type row action endpoints --- .../buttons/grid/GridRowActionsButton.svelte | 4 +- .../builder/src/stores/builder/rowActions.js | 34 +++------ packages/client/src/utils/buttonActions.js | 6 +- .../src/api/{rowActions.js => rowActions.ts} | 72 ++++++++++++------- packages/frontend-core/src/api/types.ts | 3 +- 5 files changed, 58 insertions(+), 61 deletions(-) rename packages/frontend-core/src/api/{rowActions.js => rowActions.ts} (51%) 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 f61e19c19d..496f367c01 100644 --- a/packages/builder/src/components/backend/DataTable/buttons/grid/GridRowActionsButton.svelte +++ b/packages/builder/src/components/backend/DataTable/buttons/grid/GridRowActionsButton.svelte @@ -39,9 +39,9 @@ const toggleAction = async (action, enabled) => { if (enabled) { - await rowActions.enableView(tableId, viewId, action.id) + await rowActions.enableView(tableId, action.id, viewId) } else { - await rowActions.disableView(tableId, viewId, action.id) + await rowActions.disableView(tableId, action.id, viewId) } } diff --git a/packages/builder/src/stores/builder/rowActions.js b/packages/builder/src/stores/builder/rowActions.js index b1f4e7067f..a7ed45e707 100644 --- a/packages/builder/src/stores/builder/rowActions.js +++ b/packages/builder/src/stores/builder/rowActions.js @@ -55,15 +55,12 @@ export class RowActionStore extends BudiStore { } // Create the action - const res = await API.rowActions.create({ - name, - tableId, - }) + const res = await API.rowActions.create(tableId, name) // Enable action on this view if adding via a view if (viewId) { await Promise.all([ - this.enableView(tableId, viewId, res.id), + this.enableView(tableId, res.id, viewId), automationStore.actions.fetch(), ]) } else { @@ -76,21 +73,13 @@ export class RowActionStore extends BudiStore { return res } - enableView = async (tableId, viewId, rowActionId) => { - await API.rowActions.enableView({ - tableId, - viewId, - rowActionId, - }) + enableView = async (tableId, rowActionId, viewId) => { + await API.rowActions.enableView(tableId, rowActionId, viewId) await this.refreshRowActions(tableId) } - disableView = async (tableId, viewId, rowActionId) => { - await API.rowActions.disableView({ - tableId, - viewId, - rowActionId, - }) + disableView = async (tableId, rowActionId, viewId) => { + await API.rowActions.disableView(tableId, rowActionId, viewId) await this.refreshRowActions(tableId) } @@ -105,21 +94,14 @@ export class RowActionStore extends BudiStore { } delete = async (tableId, rowActionId) => { - await API.rowActions.delete({ - tableId, - rowActionId, - }) + await API.rowActions.delete(tableId, rowActionId) await this.refreshRowActions(tableId) // We don't need to refresh automations as we can only delete row actions // from the automations store, so we already handle the state update there } trigger = async (sourceId, rowActionId, rowId) => { - await API.rowActions.trigger({ - sourceId, - rowActionId, - rowId, - }) + await API.rowActions.trigger(sourceId, rowActionId, rowId) } } diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index 561c12556a..010e1fea43 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -487,11 +487,7 @@ const downloadFileHandler = async action => { const rowActionHandler = async action => { const { resourceId, rowId, rowActionId } = action.parameters - await API.rowActions.trigger({ - rowActionId, - sourceId: resourceId, - rowId, - }) + await API.rowActions.trigger(resourceId, rowActionId, rowId) // Refresh related datasources await dataSourceStore.actions.invalidateDataSource(resourceId, { invalidateRelationships: true, diff --git a/packages/frontend-core/src/api/rowActions.js b/packages/frontend-core/src/api/rowActions.ts similarity index 51% rename from packages/frontend-core/src/api/rowActions.js rename to packages/frontend-core/src/api/rowActions.ts index 071af953ef..eef50ed940 100644 --- a/packages/frontend-core/src/api/rowActions.js +++ b/packages/frontend-core/src/api/rowActions.ts @@ -1,13 +1,46 @@ -export const buildRowActionEndpoints = API => ({ +import { + RowActionsResponse, + RowActionResponse, + CreateRowActionRequest, + RowActionPermissionsResponse, + RowActionTriggerRequest, +} from "@budibase/types" +import { BaseAPIClient } from "./types" + +export interface RowActionEndpoints { + fetch: (tableId: string) => Promise> + create: (tableId: string, name: string) => Promise + delete: (tableId: string, rowActionId: string) => Promise + enableView: ( + tableId: string, + rowActionId: string, + viewId: string + ) => Promise + disableView: ( + tableId: string, + rowActionId: string, + viewId: string + ) => Promise + trigger: ( + sourceId: string, + rowActionId: string, + rowId: string + ) => Promise +} + +export const buildRowActionEndpoints = ( + API: BaseAPIClient +): RowActionEndpoints => ({ /** * Gets the available row actions for a table. * @param tableId the ID of the table */ fetch: async tableId => { - const res = await API.get({ - url: `/api/tables/${tableId}/actions`, - }) - return res?.actions || {} + return ( + await API.get({ + url: `/api/tables/${tableId}/actions`, + }) + ).actions }, /** @@ -15,8 +48,8 @@ export const buildRowActionEndpoints = API => ({ * @param name the name of the row action * @param tableId the ID of the table */ - create: async ({ name, tableId }) => { - return await API.post({ + create: async (tableId, name) => { + return await API.post({ url: `/api/tables/${tableId}/actions`, body: { name, @@ -24,27 +57,12 @@ export const buildRowActionEndpoints = API => ({ }) }, - /** - * Updates a row action. - * @param name the new name of the row action - * @param tableId the ID of the table - * @param rowActionId the ID of the row action to update - */ - update: async ({ tableId, rowActionId, name }) => { - return await API.put({ - url: `/api/tables/${tableId}/actions/${rowActionId}`, - body: { - name, - }, - }) - }, - /** * Deletes a row action. * @param tableId the ID of the table * @param rowActionId the ID of the row action to delete */ - delete: async ({ tableId, rowActionId }) => { + delete: async (tableId, rowActionId) => { return await API.delete({ url: `/api/tables/${tableId}/actions/${rowActionId}`, }) @@ -56,7 +74,7 @@ export const buildRowActionEndpoints = API => ({ * @param rowActionId the ID of the row action * @param viewId the ID of the view */ - enableView: async ({ tableId, rowActionId, viewId }) => { + enableView: async (tableId, rowActionId, viewId) => { return await API.post({ url: `/api/tables/${tableId}/actions/${rowActionId}/permissions/${viewId}`, }) @@ -68,7 +86,7 @@ export const buildRowActionEndpoints = API => ({ * @param rowActionId the ID of the row action * @param viewId the ID of the view */ - disableView: async ({ tableId, rowActionId, viewId }) => { + disableView: async (tableId, rowActionId, viewId) => { return await API.delete({ url: `/api/tables/${tableId}/actions/${rowActionId}/permissions/${viewId}`, }) @@ -79,8 +97,8 @@ export const buildRowActionEndpoints = API => ({ * @param tableId the ID of the table * @param rowActionId the ID of the row action to trigger */ - trigger: async ({ sourceId, rowActionId, rowId }) => { - return await API.post({ + trigger: async (sourceId, rowActionId, rowId) => { + return await API.post({ url: `/api/tables/${sourceId}/actions/${rowActionId}/trigger`, body: { rowId, diff --git a/packages/frontend-core/src/api/types.ts b/packages/frontend-core/src/api/types.ts index f3cf6b034f..db8c8856ac 100644 --- a/packages/frontend-core/src/api/types.ts +++ b/packages/frontend-core/src/api/types.ts @@ -23,6 +23,7 @@ import { QueryEndpoints } from "./queries" import { RelationshipEndpoints } from "./relationships" import { RoleEndpoints } from "./roles" import { RouteEndpoints } from "./routes" +import { RowActionEndpoints } from "./rowActions" export enum HTTPMethod { POST = "POST", @@ -117,4 +118,4 @@ export type APIClient = BaseAPIClient & QueryEndpoints & RelationshipEndpoints & RoleEndpoints & - RouteEndpoints & { [key: string]: any } + RouteEndpoints & { rowActions: RowActionEndpoints; [key: string]: any }