mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-01 00:08:27 +01:00
4167a60588
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.
81 lines
2.8 KiB
TypeScript
81 lines
2.8 KiB
TypeScript
import { useCallback, useState } from 'react';
|
|
import useToast from 'hooks/useToast';
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
|
import { useChangeRequestApi } from './api/actions/useChangeRequestApi/useChangeRequestApi';
|
|
import { usePendingChangeRequests } from './api/getters/usePendingChangeRequests/usePendingChangeRequests';
|
|
|
|
export const useChangeRequestToggle = (project: string) => {
|
|
const { setToastData, setToastApiError } = useToast();
|
|
const { addChange } = useChangeRequestApi();
|
|
const { refetch: refetchChangeRequests } =
|
|
usePendingChangeRequests(project);
|
|
|
|
const [changeRequestDialogDetails, setChangeRequestDialogDetails] =
|
|
useState<{
|
|
enabled?: boolean;
|
|
shouldActivateDisabledStrategies?: boolean;
|
|
featureName?: string;
|
|
environment?: string;
|
|
isOpen: boolean;
|
|
}>({ isOpen: false });
|
|
|
|
const onChangeRequestToggle = useCallback(
|
|
(
|
|
featureName: string,
|
|
environment: string,
|
|
enabled: boolean,
|
|
shouldActivateDisabledStrategies: boolean,
|
|
) => {
|
|
setChangeRequestDialogDetails({
|
|
featureName,
|
|
environment,
|
|
enabled,
|
|
shouldActivateDisabledStrategies,
|
|
isOpen: true,
|
|
});
|
|
},
|
|
[],
|
|
);
|
|
|
|
const onChangeRequestToggleClose = useCallback(() => {
|
|
setChangeRequestDialogDetails((prev) => ({ ...prev, isOpen: false }));
|
|
}, []);
|
|
|
|
const onChangeRequestToggleConfirm = useCallback(async () => {
|
|
try {
|
|
await addChange(project, changeRequestDialogDetails.environment!, {
|
|
feature: changeRequestDialogDetails.featureName!,
|
|
action: 'updateEnabled',
|
|
payload: {
|
|
enabled: Boolean(changeRequestDialogDetails.enabled),
|
|
shouldActivateDisabledStrategies: Boolean(
|
|
changeRequestDialogDetails.shouldActivateDisabledStrategies,
|
|
),
|
|
},
|
|
});
|
|
refetchChangeRequests();
|
|
setChangeRequestDialogDetails((prev) => ({
|
|
...prev,
|
|
isOpen: false,
|
|
}));
|
|
setToastData({
|
|
type: 'success',
|
|
title: 'Changes added to the draft!',
|
|
});
|
|
} catch (error) {
|
|
setToastApiError(formatUnknownError(error));
|
|
setChangeRequestDialogDetails((prev) => ({
|
|
...prev,
|
|
isOpen: false,
|
|
}));
|
|
}
|
|
}, [addChange]);
|
|
|
|
return {
|
|
onChangeRequestToggle,
|
|
onChangeRequestToggleClose,
|
|
onChangeRequestToggleConfirm,
|
|
changeRequestDialogDetails,
|
|
};
|
|
};
|