2022-11-02 07:34:14 +01:00
|
|
|
import { VFC } from 'react';
|
2022-10-26 13:57:59 +02:00
|
|
|
import { Box } from '@mui/material';
|
2022-11-02 07:34:14 +01:00
|
|
|
import { ChangeRequestFeatureToggleChange } from '../ChangeRequestOverview/ChangeRequestFeatureToggleChange/ChangeRequestFeatureToggleChange';
|
2022-10-26 13:57:59 +02:00
|
|
|
import { objectId } from 'utils/objectId';
|
2022-11-02 07:34:14 +01:00
|
|
|
import { ToggleStatusChange } from '../ChangeRequestOverview/ChangeRequestFeatureToggleChange/ToggleStatusChange';
|
|
|
|
import { useChangeRequestApi } from 'hooks/api/actions/useChangeRequestApi/useChangeRequestApi';
|
2022-10-31 13:46:54 +01:00
|
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
|
|
|
import useToast from 'hooks/useToast';
|
2022-11-04 12:52:47 +01:00
|
|
|
import type { IChangeRequest } from '../changeRequest.types';
|
2022-11-04 10:33:07 +01:00
|
|
|
import {
|
|
|
|
StrategyAddedChange,
|
|
|
|
StrategyDeletedChange,
|
|
|
|
StrategyEditedChange,
|
|
|
|
} from '../ChangeRequestOverview/ChangeRequestFeatureToggleChange/StrategyChange';
|
|
|
|
import {
|
|
|
|
formatStrategyName,
|
|
|
|
GetFeatureStrategyIcon,
|
2022-11-04 11:55:45 +01:00
|
|
|
} from 'utils/strategyNames';
|
2022-11-04 12:52:47 +01:00
|
|
|
import { hasNameField } from '../changeRequest.types';
|
2022-10-26 13:57:59 +02:00
|
|
|
|
2022-11-02 16:05:27 +01:00
|
|
|
interface IChangeRequestProps {
|
|
|
|
changeRequest: IChangeRequest;
|
2022-10-31 13:46:54 +01:00
|
|
|
onRefetch?: () => void;
|
|
|
|
onNavigate?: () => void;
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:05:27 +01:00
|
|
|
export const ChangeRequest: VFC<IChangeRequestProps> = ({
|
2022-11-02 07:34:14 +01:00
|
|
|
changeRequest,
|
2022-10-31 13:46:54 +01:00
|
|
|
onRefetch,
|
|
|
|
onNavigate,
|
|
|
|
}) => {
|
2022-11-02 07:34:14 +01:00
|
|
|
const { discardChangeRequestEvent } = useChangeRequestApi();
|
2022-10-31 13:46:54 +01:00
|
|
|
const { setToastData, setToastApiError } = useToast();
|
|
|
|
const onDiscard = (id: number) => async () => {
|
|
|
|
try {
|
2022-11-02 07:34:14 +01:00
|
|
|
await discardChangeRequestEvent(
|
|
|
|
changeRequest.project,
|
|
|
|
changeRequest.id,
|
2022-10-31 13:46:54 +01:00
|
|
|
id
|
|
|
|
);
|
|
|
|
setToastData({
|
2022-11-02 07:34:14 +01:00
|
|
|
title: 'Change discarded from change request draft.',
|
2022-10-31 13:46:54 +01:00
|
|
|
type: 'success',
|
|
|
|
});
|
|
|
|
onRefetch?.();
|
|
|
|
} catch (error: unknown) {
|
|
|
|
setToastApiError(formatUnknownError(error));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-26 13:57:59 +02:00
|
|
|
return (
|
|
|
|
<Box>
|
2022-11-02 07:34:14 +01:00
|
|
|
{changeRequest.features?.map(featureToggleChange => (
|
|
|
|
<ChangeRequestFeatureToggleChange
|
2022-10-28 09:43:49 +02:00
|
|
|
key={featureToggleChange.name}
|
2022-10-31 13:46:54 +01:00
|
|
|
featureName={featureToggleChange.name}
|
2022-11-02 07:34:14 +01:00
|
|
|
projectId={changeRequest.project}
|
2022-10-31 13:46:54 +01:00
|
|
|
onNavigate={onNavigate}
|
2022-10-26 13:57:59 +02:00
|
|
|
>
|
2022-10-28 09:43:49 +02:00
|
|
|
{featureToggleChange.changes.map(change => (
|
2022-11-04 12:52:47 +01:00
|
|
|
<Box
|
|
|
|
key={objectId(change)}
|
|
|
|
sx={theme => ({
|
|
|
|
padding: 2,
|
|
|
|
borderTop: '1px solid',
|
|
|
|
borderColor: theme =>
|
|
|
|
theme.palette.dividerAlternative,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{change.action === 'updateEnabled' && (
|
|
|
|
<ToggleStatusChange
|
|
|
|
enabled={change.payload.enabled}
|
|
|
|
onDiscard={onDiscard(change.id)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{change.action === 'addStrategy' && (
|
|
|
|
<StrategyAddedChange
|
|
|
|
onDiscard={onDiscard(change.id)}
|
|
|
|
>
|
|
|
|
<GetFeatureStrategyIcon
|
|
|
|
strategyName={change.payload.name}
|
2022-10-26 13:57:59 +02:00
|
|
|
/>
|
2022-11-04 12:52:47 +01:00
|
|
|
|
|
|
|
{formatStrategyName(change.payload.name)}
|
|
|
|
</StrategyAddedChange>
|
|
|
|
)}
|
|
|
|
{change.action === 'deleteStrategy' && (
|
|
|
|
<StrategyDeletedChange
|
|
|
|
onDiscard={onDiscard(change.id)}
|
|
|
|
>
|
|
|
|
{hasNameField(change.payload) && (
|
|
|
|
<>
|
|
|
|
<GetFeatureStrategyIcon
|
|
|
|
strategyName={
|
|
|
|
change.payload.name
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
{formatStrategyName(
|
|
|
|
change.payload.name
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</StrategyDeletedChange>
|
|
|
|
)}
|
|
|
|
{change.action === 'updateStrategy' && (
|
|
|
|
<StrategyEditedChange
|
|
|
|
onDiscard={onDiscard(change.id)}
|
|
|
|
>
|
|
|
|
<GetFeatureStrategyIcon
|
|
|
|
strategyName={change.payload.name}
|
|
|
|
/>
|
|
|
|
{formatStrategyName(change.payload.name)}
|
|
|
|
</StrategyEditedChange>
|
|
|
|
)}
|
2022-10-26 13:57:59 +02:00
|
|
|
</Box>
|
|
|
|
))}
|
2022-11-02 07:34:14 +01:00
|
|
|
</ChangeRequestFeatureToggleChange>
|
2022-10-26 13:57:59 +02:00
|
|
|
))}
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|