Skip to content

Commit 45dc328

Browse files
authored
Merge pull request #2226 from Northeastern-Electric-Racing/#2028-delete-design-review-hook
#2028 use delete design review hook
2 parents 76e13bd + 4654b2b commit 45dc328

4 files changed

Lines changed: 80 additions & 3 deletions

File tree

src/frontend/src/apis/design-reviews.api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ export const getSingleDesignReview = async (id: string) => {
5858
});
5959
};
6060

61+
/**
62+
* Deletes a design review
63+
* @param id the ID of the design review to delete
64+
*/
65+
export const deleteDesignReview = async (id: string) => {
66+
return axios.delete(apiUrls.designReviewDelete(id));
67+
};
68+
6169
export const markUserConfirmed = async (id: string, payload: { availability: number[] }) => {
6270
return axios.post<DesignReview>(apiUrls.designReviewMarkUserConfirmed(id), payload);
6371
};

src/frontend/src/hooks/design-reviews.hooks.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { useMutation, useQuery, useQueryClient } from 'react-query';
66
import { DesignReview, TeamType, WbsNumber, DesignReviewStatus } from 'shared';
77
import {
8+
deleteDesignReview,
89
editDesignReview,
910
createDesignReviews,
1011
getAllDesignReviews,
@@ -98,6 +99,26 @@ export const useAllTeamTypes = () => {
9899
});
99100
};
100101

102+
/**
103+
* Custom react hook to delete a design review
104+
*/
105+
106+
export const useDeleteDesignReview = (id: string) => {
107+
const queryClient = useQueryClient();
108+
return useMutation<DesignReview, Error>(
109+
['design-reviews', 'delete'],
110+
async () => {
111+
const { data } = await deleteDesignReview(id);
112+
return data;
113+
},
114+
{
115+
onSuccess: () => {
116+
queryClient.invalidateQueries(['design-reviews']);
117+
}
118+
}
119+
);
120+
};
121+
101122
/**
102123
* Custom react hook to get a single design review
103124
*

src/frontend/src/pages/CalendarPage/DesignReviewDetailPage/DesignReviewDetailPage.tsx

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,78 @@
1-
import { Autocomplete, Box, Checkbox, Grid, TextField, useTheme } from '@mui/material';
1+
import { Autocomplete, Box, Checkbox, Grid, IconButton, TextField, Typography, useTheme } from '@mui/material';
22
import PageLayout from '../../../components/PageLayout';
33
import { usersToAvailabilities, existingMeetingData } from '../../../utils/design-review.utils';
44
import AvailabilityView from './AvailabilityView';
5-
import { useAllUsers } from '../../../hooks/users.hooks';
5+
import { useAllUsers, useCurrentUser } from '../../../hooks/users.hooks';
66
import LoadingIndicator from '../../../components/LoadingIndicator';
77
import ErrorPage from '../../ErrorPage';
88
import { userToAutocompleteOption } from '../../../utils/teams.utils';
99
import { useState } from 'react';
1010
import CheckBoxIcon from '@mui/icons-material/CheckBox';
1111
import CheckBoxOutlineBlankIcon from '@mui/icons-material/CheckBoxOutlineBlank';
1212
import { routes } from '../../../utils/routes';
13-
import { DesignReview, wbsPipe } from 'shared';
13+
import { DesignReview, isAdmin, wbsPipe } from 'shared';
14+
import { useDeleteDesignReview } from '../../../hooks/design-reviews.hooks';
15+
import { useHistory } from 'react-router-dom';
16+
import { useToast } from '../../../hooks/toasts.hooks';
17+
import NERModal from '../../../components/NERModal';
18+
import DeleteIcon from '@mui/icons-material/Delete';
1419

1520
interface DesignReviewDetailPageProps {
1621
designReview: DesignReview;
1722
}
1823

1924
const DesignReviewDetailPage: React.FC<DesignReviewDetailPageProps> = ({ designReview, designReview: { teamType } }) => {
2025
const theme = useTheme();
26+
const user = useCurrentUser();
2127
const { isLoading: allUsersIsLoading, isError: allUsersIsError, error: allUsersError, data: allUsers } = useAllUsers();
2228
const [requiredUsers, setRequiredUsers] = useState([].map(userToAutocompleteOption));
2329
const [optionalUsers, setOptionalUsers] = useState([].map(userToAutocompleteOption));
30+
const [showDeleteModal, setShowDeleteModal] = useState(false);
31+
const { mutateAsync: deleteDesignReview } = useDeleteDesignReview(designReview.designReviewId);
32+
const history = useHistory();
33+
const toast = useToast();
2434
if (allUsersIsError) return <ErrorPage message={allUsersError?.message} />;
2535
if (allUsersIsLoading || !allUsers) return <LoadingIndicator />;
2636
const designReviewName = `${wbsPipe(designReview.wbsNum)} - ${designReview.wbsName}`;
2737
const users = allUsers.map(userToAutocompleteOption);
2838

39+
const handleDelete = () => {
40+
try {
41+
deleteDesignReview();
42+
history.push(routes.CALENDAR);
43+
} catch (e: unknown) {
44+
if (e instanceof Error) {
45+
toast.error(e.message, 3000);
46+
}
47+
}
48+
};
49+
50+
const DeleteModal = () => {
51+
return (
52+
<NERModal
53+
open={showDeleteModal}
54+
onHide={() => setShowDeleteModal(false)}
55+
title="Warning!"
56+
cancelText="No"
57+
submitText="Yes"
58+
onSubmit={handleDelete}
59+
>
60+
<Typography>Are you sure you want to delete this design review?</Typography>
61+
</NERModal>
62+
);
63+
};
64+
65+
const hasDeletePerms = user.userId === designReview.userCreated.userId || isAdmin(user.role);
66+
2967
return (
3068
<PageLayout
69+
headerRight={
70+
hasDeletePerms && (
71+
<IconButton onClick={() => setShowDeleteModal(true)}>
72+
<DeleteIcon />
73+
</IconButton>
74+
)
75+
}
3176
title="Scheduling"
3277
previousPages={[
3378
{
@@ -36,6 +81,7 @@ const DesignReviewDetailPage: React.FC<DesignReviewDetailPageProps> = ({ designR
3681
}
3782
]}
3883
>
84+
<DeleteModal />
3985
<Grid container spacing={3} display={'flex'} paddingBottom={2}>
4086
<Grid item xs={1}>
4187
<Box

src/frontend/src/utils/urls.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const designReviewsCreate = () => `${designReviews()}/create`;
137137
const teamTypes = () => `${designReviews()}/teamType/all`;
138138
const designReviewsEdit = (designReviewId: string) => `${designReviews()}/${designReviewId}/edit`;
139139
const designReviewById = (id: string) => `${designReviews()}/${id}`;
140+
const designReviewDelete = (id: string) => `${designReviewById(id)}/delete`;
140141
const designReviewMarkUserConfirmed = (id: string) => `${designReviewById(id)}/confirm-schedule`;
141142

142143
/**************** Other Endpoints ****************/
@@ -251,6 +252,7 @@ export const apiUrls = {
251252
designReviewsEdit,
252253
designReviewMarkUserConfirmed,
253254
teamTypes,
255+
designReviewDelete,
254256

255257
version
256258
};

0 commit comments

Comments
 (0)