1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

chore: add new action hooks (#5992)

https://linear.app/unleash/issue/2-1857/create-new-action-hooks-on-the-frontend

Adds action hooks to help us with CRUD operations on the frontend,
similar to https://github.com/Unleash/unleash/pull/5788 and
https://github.com/Unleash/unleash/pull/5790
This commit is contained in:
Nuno Góis 2024-01-22 17:31:04 +00:00 committed by GitHub
parent b6a219dd36
commit 0847c2e52c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,73 @@
import { IAction, IActionSet } from 'interfaces/action';
import useAPI from '../useApi/useApi';
const ENDPOINT = 'api/admin/actions';
export type ActionPayload = Omit<
IAction,
'id' | 'createdAt' | 'createdByUserId'
>;
export type ActionSetPayload = Omit<
IActionSet,
'id' | 'createdAt' | 'createdByUserId'
> & {
actions: ActionPayload[];
};
export const useActionsApi = () => {
const { loading, makeRequest, createRequest, errors } = useAPI({
propagateErrors: true,
});
const addActionSet = async (actionSet: ActionSetPayload) => {
const requestId = 'addActionSet';
const req = createRequest(
ENDPOINT,
{
method: 'POST',
body: JSON.stringify(actionSet),
},
requestId,
);
const response = await makeRequest(req.caller, req.id);
return response.json();
};
const updateActionSet = async (
actionSetId: number,
actionSet: ActionSetPayload,
) => {
const requestId = 'updateActionSet';
const req = createRequest(
`${ENDPOINT}/${actionSetId}`,
{
method: 'PUT',
body: JSON.stringify(actionSet),
},
requestId,
);
await makeRequest(req.caller, req.id);
};
const removeActionSet = async (actionSetId: number) => {
const requestId = 'removeActionSet';
const req = createRequest(
`${ENDPOINT}/${actionSetId}`,
{ method: 'DELETE' },
requestId,
);
await makeRequest(req.caller, req.id);
};
return {
addActionSet,
updateActionSet,
removeActionSet,
errors,
loading,
};
};

View File

@ -0,0 +1,37 @@
import { useMemo } from 'react';
import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler';
import { useConditionalSWR } from '../useConditionalSWR/useConditionalSWR';
import useUiConfig from '../useUiConfig/useUiConfig';
import { IActionSet } from 'interfaces/action';
import { useUiFlag } from 'hooks/useUiFlag';
const ENDPOINT = 'api/admin/actions';
export const useActions = () => {
const { isEnterprise } = useUiConfig();
const actionsEnabled = useUiFlag('automatedActions');
const { data, error, mutate } = useConditionalSWR(
isEnterprise() && actionsEnabled,
{ actions: [] },
formatApiPath(ENDPOINT),
fetcher,
);
return useMemo(
() => ({
actions: (data?.actions ?? []) as IActionSet[],
loading: !error && !data,
refetch: () => mutate(),
error,
}),
[data, error, mutate],
);
};
const fetcher = (path: string) => {
return fetch(path)
.then(handleErrorResponses('Actions'))
.then((res) => res.json());
};

View File

@ -0,0 +1,27 @@
export interface IActionSet {
id: number;
name: string;
project: string;
actorId: number;
match: IMatch;
actions: IAction[];
createdAt: string;
createdByUserId: number;
}
type MatchSource = 'incoming-webhook';
export interface IMatch {
source: MatchSource;
sourceId: number;
payload: Record<string, unknown>;
}
export interface IAction {
id: number;
action: string;
sortOrder: number;
executionParams: Record<string, unknown>;
createdAt: string;
createdByUserId: number;
}

View File

@ -68,6 +68,7 @@ export type UiFlags = {
featureSearchFrontend?: boolean;
newStrategyConfiguration?: boolean;
incomingWebhooks?: boolean;
automatedActions?: boolean;
celebrateUnleash?: boolean;
increaseUnleashWidth?: boolean;
featureSearchFeedback?: boolean;