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';
|
|
|
|
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
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 10:33:07 +01:00
|
|
|
import type {
|
|
|
|
IChangeRequest,
|
|
|
|
IChangeRequestAddStrategy,
|
|
|
|
} from '../changeRequest.types';
|
|
|
|
import {
|
|
|
|
StrategyAddedChange,
|
|
|
|
StrategyDeletedChange,
|
|
|
|
StrategyEditedChange,
|
|
|
|
} from '../ChangeRequestOverview/ChangeRequestFeatureToggleChange/StrategyChange';
|
|
|
|
import {
|
|
|
|
formatStrategyName,
|
|
|
|
GetFeatureStrategyIcon,
|
|
|
|
} from '../../../utils/strategyNames';
|
|
|
|
import { IChangeRequestEnabled } 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-10-26 13:57:59 +02:00
|
|
|
<Box key={objectId(change)}>
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={change.action === 'updateEnabled'}
|
|
|
|
show={
|
|
|
|
<ToggleStatusChange
|
2022-11-04 10:33:07 +01:00
|
|
|
enabled={
|
|
|
|
(change as IChangeRequestEnabled)
|
|
|
|
?.payload?.enabled
|
|
|
|
}
|
|
|
|
onDiscard={onDiscard(change.id!)}
|
2022-10-26 13:57:59 +02:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2022-11-04 10:33:07 +01:00
|
|
|
<ConditionallyRender
|
2022-10-26 13:57:59 +02:00
|
|
|
condition={change.action === 'addStrategy'}
|
|
|
|
show={
|
|
|
|
<StrategyAddedChange>
|
|
|
|
<GetFeatureStrategyIcon
|
2022-11-04 10:33:07 +01:00
|
|
|
strategyName={
|
|
|
|
(
|
|
|
|
change as IChangeRequestAddStrategy
|
|
|
|
)?.payload.name!
|
|
|
|
}
|
2022-10-26 13:57:59 +02:00
|
|
|
/>
|
|
|
|
{formatStrategyName(
|
2022-11-04 10:33:07 +01:00
|
|
|
(
|
|
|
|
change as IChangeRequestAddStrategy
|
|
|
|
)?.payload.name!
|
2022-10-26 13:57:59 +02:00
|
|
|
)}
|
|
|
|
</StrategyAddedChange>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={change.action === 'deleteStrategy'}
|
|
|
|
show={<StrategyDeletedChange />}
|
|
|
|
/>
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={change.action === 'updateStrategy'}
|
|
|
|
show={<StrategyEditedChange />}
|
2022-11-04 10:33:07 +01:00
|
|
|
/>
|
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>
|
|
|
|
);
|
|
|
|
};
|