2022-11-02 07:34:14 +01:00
|
|
|
import { useCallback, useState } from 'react';
|
|
|
|
import useToast from 'hooks/useToast';
|
|
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
|
|
|
import { useChangeRequestApi } from './api/actions/useChangeRequestApi/useChangeRequestApi';
|
2022-11-23 13:45:19 +01:00
|
|
|
import { usePendingChangeRequests } from './api/getters/usePendingChangeRequests/usePendingChangeRequests';
|
2022-11-02 07:34:14 +01:00
|
|
|
|
|
|
|
export const useChangeRequestToggle = (project: string) => {
|
|
|
|
const { setToastData, setToastApiError } = useToast();
|
2022-11-30 12:04:29 +01:00
|
|
|
const { addChange } = useChangeRequestApi();
|
2022-11-23 13:45:19 +01:00
|
|
|
const { refetch: refetchChangeRequests } =
|
|
|
|
usePendingChangeRequests(project);
|
2024-02-08 10:27:51 +01:00
|
|
|
const [pending, setPending] = useState(false);
|
2022-11-02 07:34:14 +01:00
|
|
|
|
|
|
|
const [changeRequestDialogDetails, setChangeRequestDialogDetails] =
|
|
|
|
useState<{
|
|
|
|
enabled?: boolean;
|
2023-05-23 09:16:09 +02:00
|
|
|
shouldActivateDisabledStrategies?: boolean;
|
2022-11-02 07:34:14 +01:00
|
|
|
featureName?: string;
|
|
|
|
environment?: string;
|
|
|
|
isOpen: boolean;
|
|
|
|
}>({ isOpen: false });
|
|
|
|
|
|
|
|
const onChangeRequestToggle = useCallback(
|
2023-05-23 09:16:09 +02:00
|
|
|
(
|
|
|
|
featureName: string,
|
|
|
|
environment: string,
|
|
|
|
enabled: boolean,
|
2023-10-02 14:25:46 +02:00
|
|
|
shouldActivateDisabledStrategies: boolean,
|
2023-05-23 09:16:09 +02:00
|
|
|
) => {
|
2022-11-02 07:34:14 +01:00
|
|
|
setChangeRequestDialogDetails({
|
|
|
|
featureName,
|
|
|
|
environment,
|
|
|
|
enabled,
|
2023-05-23 09:16:09 +02:00
|
|
|
shouldActivateDisabledStrategies,
|
2022-11-02 07:34:14 +01:00
|
|
|
isOpen: true,
|
|
|
|
});
|
|
|
|
},
|
2023-10-02 14:25:46 +02:00
|
|
|
[],
|
2022-11-02 07:34:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const onChangeRequestToggleClose = useCallback(() => {
|
2023-10-02 14:25:46 +02:00
|
|
|
setChangeRequestDialogDetails((prev) => ({ ...prev, isOpen: false }));
|
2022-11-02 07:34:14 +01:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onChangeRequestToggleConfirm = useCallback(async () => {
|
|
|
|
try {
|
2024-02-08 10:27:51 +01:00
|
|
|
setPending(true);
|
2022-11-30 12:04:29 +01:00
|
|
|
await addChange(project, changeRequestDialogDetails.environment!, {
|
|
|
|
feature: changeRequestDialogDetails.featureName!,
|
|
|
|
action: 'updateEnabled',
|
|
|
|
payload: {
|
|
|
|
enabled: Boolean(changeRequestDialogDetails.enabled),
|
2023-05-23 09:16:09 +02:00
|
|
|
shouldActivateDisabledStrategies: Boolean(
|
2023-10-02 14:25:46 +02:00
|
|
|
changeRequestDialogDetails.shouldActivateDisabledStrategies,
|
2023-05-23 09:16:09 +02:00
|
|
|
),
|
2022-11-30 12:04:29 +01:00
|
|
|
},
|
|
|
|
});
|
2022-11-02 07:34:14 +01:00
|
|
|
refetchChangeRequests();
|
2023-10-02 14:25:46 +02:00
|
|
|
setChangeRequestDialogDetails((prev) => ({
|
|
|
|
...prev,
|
|
|
|
isOpen: false,
|
|
|
|
}));
|
2022-11-02 07:34:14 +01:00
|
|
|
setToastData({
|
|
|
|
type: 'success',
|
|
|
|
title: 'Changes added to the draft!',
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
setToastApiError(formatUnknownError(error));
|
2023-10-02 14:25:46 +02:00
|
|
|
setChangeRequestDialogDetails((prev) => ({
|
|
|
|
...prev,
|
|
|
|
isOpen: false,
|
|
|
|
}));
|
2024-02-08 10:27:51 +01:00
|
|
|
} finally {
|
|
|
|
setPending(false);
|
2022-11-02 07:34:14 +01:00
|
|
|
}
|
2022-11-30 12:04:29 +01:00
|
|
|
}, [addChange]);
|
2022-11-02 07:34:14 +01:00
|
|
|
|
|
|
|
return {
|
2024-02-08 10:27:51 +01:00
|
|
|
pending,
|
2022-11-02 07:34:14 +01:00
|
|
|
onChangeRequestToggle,
|
|
|
|
onChangeRequestToggleClose,
|
|
|
|
onChangeRequestToggleConfirm,
|
|
|
|
changeRequestDialogDetails,
|
|
|
|
};
|
|
|
|
};
|