From 6d26c79fa74ee8820aa886264262b257a089973d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Mon, 12 Feb 2024 09:26:05 +0100 Subject: [PATCH] chore: Move actions inside projects (#6191) ## About the changes Action should be relative to a project (in general). --- .../useIncomingWebhooksForm.ts | 2 ++ .../ProjectActionsForm/useProjectActionsForm.ts | 4 +++- .../ProjectActionsModal/ProjectActionsModal.tsx | 8 +++++--- .../ProjectActionsTable/ProjectActionsTable.tsx | 6 ++++-- .../api/actions/useActionsApi/useActionsApi.ts | 15 +++++++-------- .../hooks/api/getters/useActions/useActions.ts | 10 +++++----- 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/frontend/src/component/incomingWebhooks/IncomingWebhooksModal/IncomingWebhooksForm/useIncomingWebhooksForm.ts b/frontend/src/component/incomingWebhooks/IncomingWebhooksModal/IncomingWebhooksForm/useIncomingWebhooksForm.ts index 5642ad8723..08ddf450f5 100644 --- a/frontend/src/component/incomingWebhooks/IncomingWebhooksModal/IncomingWebhooksForm/useIncomingWebhooksForm.ts +++ b/frontend/src/component/incomingWebhooks/IncomingWebhooksModal/IncomingWebhooksForm/useIncomingWebhooksForm.ts @@ -99,6 +99,8 @@ export const useIncomingWebhooksForm = (incomingWebhook?: IIncomingWebhook) => { return false; } + // TODO call backend to check if token name is unique + clearError(ErrorField.TOKEN_NAME); return true; }; diff --git a/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsModal/ProjectActionsForm/useProjectActionsForm.ts b/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsModal/ProjectActionsForm/useProjectActionsForm.ts index 53105d3e67..e7e27ba3e5 100644 --- a/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsModal/ProjectActionsForm/useProjectActionsForm.ts +++ b/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsModal/ProjectActionsForm/useProjectActionsForm.ts @@ -3,6 +3,7 @@ import { IActionSet } from 'interfaces/action'; import { useEffect, useState } from 'react'; import { UIAction } from './ActionItem'; import { v4 as uuidv4 } from 'uuid'; +import { useRequiredPathParam } from 'hooks/useRequiredPathParam'; export enum ErrorField { NAME = 'name', @@ -27,7 +28,8 @@ const DEFAULT_PROJECT_ACTIONS_FORM_ERRORS = { export type ProjectActionsFormErrors = Record; export const useProjectActionsForm = (action?: IActionSet) => { - const { actions: actionSets } = useActions(); + const projectId = useRequiredPathParam('projectId'); + const { actions: actionSets } = useActions(projectId); const [enabled, setEnabled] = useState(false); const [name, setName] = useState(''); diff --git a/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsModal/ProjectActionsModal.tsx b/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsModal/ProjectActionsModal.tsx index 9d955abe0f..89ed2eaa82 100644 --- a/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsModal/ProjectActionsModal.tsx +++ b/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsModal/ProjectActionsModal.tsx @@ -44,8 +44,8 @@ export const ProjectActionsModal = ({ setOpen, }: IProjectActionsModalProps) => { const projectId = useRequiredPathParam('projectId'); - const { refetch } = useActions(); - const { addActionSet, updateActionSet, loading } = useActionsApi(); + const { refetch } = useActions(projectId); + const { addActionSet, updateActionSet, loading } = useActionsApi(projectId); const { setToastData, setToastApiError } = useToast(); const { uiConfig } = useUiConfig(); @@ -103,7 +103,9 @@ export const ProjectActionsModal = ({ const formatApiCode = () => `curl --location --request ${ editing ? 'PUT' : 'POST' - } '${uiConfig.unleashUrl}/api/admin/actions${editing ? `/${action.id}` : ''}' \\ + } '${uiConfig.unleashUrl}/api/admin/projects/${projectId}/actions${ + editing ? `/${action.id}` : '' + }' \\ --header 'Authorization: INSERT_API_KEY' \\ --header 'Content-Type: application/json' \\ --data-raw '${JSON.stringify(payload, undefined, 2)}'`; diff --git a/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsTable.tsx b/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsTable.tsx index 441f729296..77877a9c97 100644 --- a/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsTable.tsx +++ b/frontend/src/component/project/Project/ProjectSettings/ProjectActions/ProjectActionsTable/ProjectActionsTable.tsx @@ -22,6 +22,7 @@ import { ProjectActionsModal } from './ProjectActionsModal/ProjectActionsModal'; import { ProjectActionsDeleteDialog } from './ProjectActionsDeleteDialog'; import { useServiceAccounts } from 'hooks/api/getters/useServiceAccounts/useServiceAccounts'; import { useIncomingWebhooks } from 'hooks/api/getters/useIncomingWebhooks/useIncomingWebhooks'; +import { useRequiredPathParam } from 'hooks/useRequiredPathParam'; interface IProjectActionsTableProps { modalOpen: boolean; @@ -40,8 +41,9 @@ export const ProjectActionsTable = ({ }: IProjectActionsTableProps) => { const { setToastData, setToastApiError } = useToast(); - const { actions, refetch } = useActions(); - const { toggleActionSet, removeActionSet } = useActionsApi(); + const projectId = useRequiredPathParam('projectId'); + const { actions, refetch } = useActions(projectId); + const { toggleActionSet, removeActionSet } = useActionsApi(projectId); const { incomingWebhooks } = useIncomingWebhooks(); const { serviceAccounts } = useServiceAccounts(); diff --git a/frontend/src/hooks/api/actions/useActionsApi/useActionsApi.ts b/frontend/src/hooks/api/actions/useActionsApi/useActionsApi.ts index 18cad8fc09..4224289ef6 100644 --- a/frontend/src/hooks/api/actions/useActionsApi/useActionsApi.ts +++ b/frontend/src/hooks/api/actions/useActionsApi/useActionsApi.ts @@ -1,8 +1,6 @@ import { IAction, IActionSet } from 'interfaces/action'; import useAPI from '../useApi/useApi'; -const ENDPOINT = 'api/admin/actions'; - export type ActionPayload = Omit< IAction, 'id' | 'createdAt' | 'createdByUserId' @@ -15,15 +13,16 @@ export type ActionSetPayload = Omit< actions: ActionPayload[]; }; -export const useActionsApi = () => { +export const useActionsApi = (project: string) => { const { loading, makeRequest, createRequest, errors } = useAPI({ propagateErrors: true, }); + const endpoint = `api/admin/projects/${project}/actions`; const addActionSet = async (actionSet: ActionSetPayload) => { const requestId = 'addActionSet'; const req = createRequest( - ENDPOINT, + endpoint, { method: 'POST', body: JSON.stringify(actionSet), @@ -41,7 +40,7 @@ export const useActionsApi = () => { ) => { const requestId = 'updateActionSet'; const req = createRequest( - `${ENDPOINT}/${actionSetId}`, + `${endpoint}/${actionSetId}`, { method: 'PUT', body: JSON.stringify(actionSet), @@ -55,7 +54,7 @@ export const useActionsApi = () => { const enableActionSet = async (actionSetId: number) => { const requestId = 'enableActionSet'; const req = createRequest( - `${ENDPOINT}/${actionSetId}/on`, + `${endpoint}/${actionSetId}/on`, { method: 'POST', }, @@ -68,7 +67,7 @@ export const useActionsApi = () => { const disableActionSet = async (actionSetId: number) => { const requestId = 'disableActionSet'; const req = createRequest( - `${ENDPOINT}/${actionSetId}/off`, + `${endpoint}/${actionSetId}/off`, { method: 'POST', }, @@ -89,7 +88,7 @@ export const useActionsApi = () => { const removeActionSet = async (actionSetId: number) => { const requestId = 'removeActionSet'; const req = createRequest( - `${ENDPOINT}/${actionSetId}`, + `${endpoint}/${actionSetId}`, { method: 'DELETE' }, requestId, ); diff --git a/frontend/src/hooks/api/getters/useActions/useActions.ts b/frontend/src/hooks/api/getters/useActions/useActions.ts index c709a3f675..46a291bd5a 100644 --- a/frontend/src/hooks/api/getters/useActions/useActions.ts +++ b/frontend/src/hooks/api/getters/useActions/useActions.ts @@ -6,16 +6,16 @@ import useUiConfig from '../useUiConfig/useUiConfig'; import { IActionSet } from 'interfaces/action'; import { useUiFlag } from 'hooks/useUiFlag'; -const ENDPOINT = 'api/admin/actions'; - -export const useActions = () => { +export const useActions = (project: string) => { const { isEnterprise } = useUiConfig(); const actionsEnabled = useUiFlag('automatedActions'); - const { data, error, mutate } = useConditionalSWR( + const { data, error, mutate } = useConditionalSWR<{ + actions: IActionSet[]; + }>( isEnterprise() && actionsEnabled, { actions: [] }, - formatApiPath(ENDPOINT), + formatApiPath(`api/admin/projects/${project}/actions`), fetcher, );