1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/frontend/src/hooks/useChangeRequestAddStrategy.ts
Nuno Góis 4167a60588
feat: biome lint frontend (#4903)
Follows up on https://github.com/Unleash/unleash/pull/4853 to add Biome
to the frontend as well.


![image](https://github.com/Unleash/unleash/assets/14320932/1906faf1-fc29-4172-a4d4-b2716d72cd65)

Added a few `biome-ignore` to speed up the process but we may want to
check and fix them in the future.
2023-10-02 13:25:46 +01:00

125 lines
4.1 KiB
TypeScript

import { useCallback, useState } from 'react';
import useToast from 'hooks/useToast';
import { formatUnknownError } from 'utils/formatUnknownError';
import { IFeatureStrategyPayload } from '../interfaces/strategy';
import { useChangeRequestApi } from './api/actions/useChangeRequestApi/useChangeRequestApi';
import { usePendingChangeRequests } from './api/getters/usePendingChangeRequests/usePendingChangeRequests';
export type ChangeRequestStrategyAction =
| 'addStrategy'
| 'updateStrategy'
| 'deleteStrategy';
export const useChangeRequestAddStrategy = (
project: string,
featureName: string,
action: ChangeRequestStrategyAction,
) => {
const { setToastData, setToastApiError } = useToast();
const { addChange } = useChangeRequestApi();
const { refetch } = usePendingChangeRequests(project);
const [changeRequestDialogDetails, setChangeRequestDialogDetails] =
useState<{
strategy?: IFeatureStrategyPayload;
strategies?: IFeatureStrategyPayload[];
featureName?: string;
environment?: string;
fromEnvironment?: string;
isOpen: boolean;
}>({ isOpen: false });
const onChangeRequestAddStrategy = useCallback(
(
environment: string,
strategy: IFeatureStrategyPayload,
fromEnvironment?: string,
) => {
setChangeRequestDialogDetails({
featureName,
environment,
fromEnvironment,
strategy,
isOpen: true,
});
},
[],
);
const onChangeRequestAddStrategies = useCallback(
(
environment: string,
strategies: IFeatureStrategyPayload[],
fromEnvironment: string,
) => {
setChangeRequestDialogDetails({
featureName,
environment,
fromEnvironment,
strategies,
isOpen: true,
});
},
[],
);
const onChangeRequestAddStrategyClose = useCallback(() => {
setChangeRequestDialogDetails({ isOpen: false });
}, []);
const onChangeRequestAddStrategyConfirm = useCallback(async () => {
try {
await addChange(project, changeRequestDialogDetails.environment!, {
feature: changeRequestDialogDetails.featureName!,
action: action,
payload: changeRequestDialogDetails.strategy!,
});
refetch();
setChangeRequestDialogDetails({ isOpen: false });
setToastData({
type: 'success',
title: 'Changes added to the draft!',
});
} catch (error) {
setToastApiError(formatUnknownError(error));
setChangeRequestDialogDetails({ isOpen: false });
}
}, [addChange]);
const onChangeRequestAddStrategiesConfirm = useCallback(async () => {
try {
await Promise.all(
changeRequestDialogDetails.strategies!.map((strategy) => {
return addChange(
project,
changeRequestDialogDetails.environment!,
{
feature: changeRequestDialogDetails.featureName!,
action: action,
payload: strategy,
},
);
}),
);
refetch();
setChangeRequestDialogDetails({ isOpen: false });
setToastData({
type: 'success',
title: 'Changes added to the draft!',
});
} catch (error) {
setToastApiError(formatUnknownError(error));
setChangeRequestDialogDetails({ isOpen: false });
}
}, [addChange]);
return {
onChangeRequestAddStrategy,
onChangeRequestAddStrategies,
onChangeRequestAddStrategyClose,
onChangeRequestAddStrategyConfirm,
onChangeRequestAddStrategiesConfirm,
changeRequestDialogDetails,
};
};