2022-11-04 10:33:07 +01:00
|
|
|
import { useCallback, useState } from 'react';
|
|
|
|
import useToast from 'hooks/useToast';
|
|
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
2022-11-09 09:29:33 +01:00
|
|
|
import { IFeatureStrategyPayload } from '../interfaces/strategy';
|
2022-11-04 10:33:07 +01:00
|
|
|
import { useChangeRequestApi } from './api/actions/useChangeRequestApi/useChangeRequestApi';
|
2022-11-23 13:45:19 +01:00
|
|
|
import { usePendingChangeRequests } from './api/getters/usePendingChangeRequests/usePendingChangeRequests';
|
2022-11-04 10:33:07 +01:00
|
|
|
|
|
|
|
export type ChangeRequestStrategyAction =
|
|
|
|
| 'addStrategy'
|
|
|
|
| 'updateStrategy'
|
|
|
|
| 'deleteStrategy';
|
|
|
|
|
|
|
|
export const useChangeRequestAddStrategy = (
|
|
|
|
project: string,
|
|
|
|
featureName: string,
|
2023-10-02 14:25:46 +02:00
|
|
|
action: ChangeRequestStrategyAction,
|
2022-11-04 10:33:07 +01:00
|
|
|
) => {
|
|
|
|
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 } = usePendingChangeRequests(project);
|
2022-11-04 10:33:07 +01:00
|
|
|
|
|
|
|
const [changeRequestDialogDetails, setChangeRequestDialogDetails] =
|
|
|
|
useState<{
|
2022-11-09 09:29:33 +01:00
|
|
|
strategy?: IFeatureStrategyPayload;
|
|
|
|
strategies?: IFeatureStrategyPayload[];
|
2022-11-04 10:33:07 +01:00
|
|
|
featureName?: string;
|
|
|
|
environment?: string;
|
|
|
|
fromEnvironment?: string;
|
|
|
|
isOpen: boolean;
|
|
|
|
}>({ isOpen: false });
|
|
|
|
|
|
|
|
const onChangeRequestAddStrategy = useCallback(
|
|
|
|
(
|
|
|
|
environment: string,
|
2022-11-09 09:29:33 +01:00
|
|
|
strategy: IFeatureStrategyPayload,
|
2023-10-02 14:25:46 +02:00
|
|
|
fromEnvironment?: string,
|
2022-11-04 10:33:07 +01:00
|
|
|
) => {
|
|
|
|
setChangeRequestDialogDetails({
|
|
|
|
featureName,
|
|
|
|
environment,
|
|
|
|
fromEnvironment,
|
|
|
|
strategy,
|
|
|
|
isOpen: true,
|
|
|
|
});
|
|
|
|
},
|
2023-10-02 14:25:46 +02:00
|
|
|
[],
|
2022-11-04 10:33:07 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const onChangeRequestAddStrategies = useCallback(
|
|
|
|
(
|
|
|
|
environment: string,
|
2022-11-09 09:29:33 +01:00
|
|
|
strategies: IFeatureStrategyPayload[],
|
2023-10-02 14:25:46 +02:00
|
|
|
fromEnvironment: string,
|
2022-11-04 10:33:07 +01:00
|
|
|
) => {
|
|
|
|
setChangeRequestDialogDetails({
|
|
|
|
featureName,
|
|
|
|
environment,
|
|
|
|
fromEnvironment,
|
|
|
|
strategies,
|
|
|
|
isOpen: true,
|
|
|
|
});
|
|
|
|
},
|
2023-10-02 14:25:46 +02:00
|
|
|
[],
|
2022-11-04 10:33:07 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const onChangeRequestAddStrategyClose = useCallback(() => {
|
|
|
|
setChangeRequestDialogDetails({ isOpen: false });
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onChangeRequestAddStrategyConfirm = useCallback(async () => {
|
|
|
|
try {
|
2022-11-30 12:04:29 +01:00
|
|
|
await addChange(project, changeRequestDialogDetails.environment!, {
|
|
|
|
feature: changeRequestDialogDetails.featureName!,
|
|
|
|
action: action,
|
|
|
|
payload: changeRequestDialogDetails.strategy!,
|
|
|
|
});
|
2022-11-04 10:33:07 +01:00
|
|
|
refetch();
|
|
|
|
setChangeRequestDialogDetails({ isOpen: false });
|
|
|
|
setToastData({
|
|
|
|
type: 'success',
|
|
|
|
title: 'Changes added to the draft!',
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
setToastApiError(formatUnknownError(error));
|
|
|
|
setChangeRequestDialogDetails({ isOpen: false });
|
|
|
|
}
|
2022-11-30 12:04:29 +01:00
|
|
|
}, [addChange]);
|
2022-11-04 10:33:07 +01:00
|
|
|
|
|
|
|
const onChangeRequestAddStrategiesConfirm = useCallback(async () => {
|
|
|
|
try {
|
|
|
|
await Promise.all(
|
2023-10-02 14:25:46 +02:00
|
|
|
changeRequestDialogDetails.strategies!.map((strategy) => {
|
2022-11-30 12:04:29 +01:00
|
|
|
return addChange(
|
2022-11-04 10:33:07 +01:00
|
|
|
project,
|
|
|
|
changeRequestDialogDetails.environment!,
|
|
|
|
{
|
|
|
|
feature: changeRequestDialogDetails.featureName!,
|
|
|
|
action: action,
|
|
|
|
payload: strategy,
|
2023-10-02 14:25:46 +02:00
|
|
|
},
|
2022-11-04 10:33:07 +01:00
|
|
|
);
|
2023-10-02 14:25:46 +02:00
|
|
|
}),
|
2022-11-04 10:33:07 +01:00
|
|
|
);
|
|
|
|
refetch();
|
|
|
|
setChangeRequestDialogDetails({ isOpen: false });
|
|
|
|
setToastData({
|
|
|
|
type: 'success',
|
|
|
|
title: 'Changes added to the draft!',
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
setToastApiError(formatUnknownError(error));
|
|
|
|
setChangeRequestDialogDetails({ isOpen: false });
|
|
|
|
}
|
2022-11-30 12:04:29 +01:00
|
|
|
}, [addChange]);
|
2022-11-04 10:33:07 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
onChangeRequestAddStrategy,
|
|
|
|
onChangeRequestAddStrategies,
|
|
|
|
onChangeRequestAddStrategyClose,
|
|
|
|
onChangeRequestAddStrategyConfirm,
|
|
|
|
onChangeRequestAddStrategiesConfirm,
|
|
|
|
changeRequestDialogDetails,
|
|
|
|
};
|
|
|
|
};
|