1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00

fix: add discard actions (#2405)

* Updates the sidebar with discarded actions
This commit is contained in:
Fredrik Strand Oseberg 2022-11-11 13:24:06 +01:00 committed by GitHub
parent c57baeb35e
commit 89e5043f32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 47 deletions

View File

@ -21,6 +21,8 @@ import { useChangeRequestApi } from 'hooks/api/actions/useChangeRequestApi/useCh
import { ChangeRequestStatusBadge } from '../ChangeRequestStatusBadge/ChangeRequestStatusBadge';
import CloseIcon from '@mui/icons-material/Close';
import { useNavigate } from 'react-router-dom';
import useToast from 'hooks/useToast';
import { formatUnknownError } from 'utils/formatUnknownError';
interface IChangeRequestSidebarProps {
open: boolean;
@ -123,26 +125,26 @@ export const ChangeRequestSidebar: VFC<IChangeRequestSidebarProps> = ({
loading,
refetch: refetchChangeRequest,
} = useChangeRequestOpen(project);
const { changeState } = useChangeRequestApi();
const { changeState, discardDraft } = useChangeRequestApi();
const theme = useTheme();
const navigate = useNavigate();
const { setToastApiError } = useToast();
const onReview = async (draftId: number) => {
try {
await changeState(project, draftId, { state: 'In review' });
refetchChangeRequest();
} catch (e) {
console.log('something went wrong');
} catch (error: unknown) {
setToastApiError(formatUnknownError(error));
}
};
const onDiscard = async () => {
alert('discard');
};
const onApply = async () => {
const onDiscard = async (draftId: number) => {
try {
alert('apply');
} catch (e) {
console.log(e);
await discardDraft(project, draftId);
refetchChangeRequest();
} catch (error: unknown) {
setToastApiError(formatUnknownError(error));
}
};
@ -257,36 +259,6 @@ export const ChangeRequestSidebar: VFC<IChangeRequestSidebarProps> = ({
onRefetch={refetchChangeRequest}
/>
<Box sx={{ display: 'flex' }}>
<ConditionallyRender
condition={
environmentChangeRequest.state ===
'Approved'
}
show={<Typography>Applied</Typography>}
/>
<ConditionallyRender
condition={
environmentChangeRequest.state === 'Applied'
}
show={<Typography>Applied</Typography>}
/>
<ConditionallyRender
condition={
environmentChangeRequest.state ===
'Approved'
}
show={
<>
<Button
sx={{ mt: 2 }}
variant="contained"
onClick={onApply}
>
Apply changes
</Button>
</>
}
/>
<ConditionallyRender
condition={
environmentChangeRequest?.state === 'Draft'
@ -308,7 +280,11 @@ export const ChangeRequestSidebar: VFC<IChangeRequestSidebarProps> = ({
<Button
sx={{ mt: 2, ml: 2 }}
variant="outlined"
onClick={onDiscard}
onClick={() =>
onDiscard(
environmentChangeRequest.id
)
}
>
Discard changes
</Button>
@ -318,7 +294,9 @@ export const ChangeRequestSidebar: VFC<IChangeRequestSidebarProps> = ({
<ConditionallyRender
condition={
environmentChangeRequest.state ===
'In review'
'In review' ||
environmentChangeRequest.state ===
'Approved'
}
show={
<>

View File

@ -28,7 +28,7 @@ const DraftBannerContent: FC<{
<Box className={classes.content}>
<DraftBannerContentWrapper>
<WarningAmberIcon />
<Typography variant="body2" sx={{ ml: 1 }}>
<Typography variant="body2" sx={{ ml: 1, maxWidth: '500px' }}>
<strong>Draft mode!</strong> You have changes{' '}
<ConditionallyRender
condition={Boolean(changeRequest.environment)}
@ -46,6 +46,12 @@ const DraftBannerContent: FC<{
condition={changeRequest.state === 'In review'}
show={'that are in review'}
/>
<ConditionallyRender
condition={changeRequest.state === 'Approved'}
show={
'that are approved. Adding more changes will clear the approvals and require a new review'
}
/>
</Typography>
<Button
variant="contained"
@ -54,9 +60,6 @@ const DraftBannerContent: FC<{
>
View changes ({changeRequest.features.length})
</Button>
<Button variant="text" onClick={() => {}} sx={{ ml: 1 }}>
Discard all
</Button>
</DraftBannerContentWrapper>
</Box>
);
@ -84,7 +87,9 @@ export const DraftBanner: VFC<IDraftBannerProps> = ({ project }) => {
{draft &&
draft
.filter(changeRequest =>
['Draft', 'In review'].includes(changeRequest.state)
['Draft', 'In review', 'Approved'].includes(
changeRequest.state
)
)
.map(changeRequest => (
<DraftBannerContent

View File

@ -77,6 +77,20 @@ export const useChangeRequestApi = () => {
method: 'PUT',
body: JSON.stringify({ changeRequestsEnabled: enabled }),
});
try {
return await makeRequest(req.caller, req.id);
} catch (e) {
throw e;
}
};
const discardDraft = async (projectId: string, draftId: number) => {
const path = `api/admin/projects/${projectId}/change-requests/${draftId}`;
const req = createRequest(path, {
method: 'DELETE',
});
try {
return await makeRequest(req.caller, req.id);
} catch (e) {
@ -89,6 +103,7 @@ export const useChangeRequestApi = () => {
changeState,
discardChangeRequestEvent,
updateChangeRequestEnvironmentConfig,
discardDraft,
errors,
loading,
};