Skip to content

Commit 10b2282

Browse files
committed
#1183 using NERFormModal
1 parent 2da89dd commit 10b2282

6 files changed

Lines changed: 103 additions & 178 deletions

File tree

src/frontend/src/apis/teams.api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const setTeamHead = (id: string, userId: number) => {
3838
};
3939

4040
export const deleteTeam = (id: string) => {
41-
return axios.post(apiUrls.teamsDelete(id));
41+
return axios.post<{ message: string }>(apiUrls.teamsDelete(id));
4242
};
4343

4444
export const setTeamLeads = (id: string, userIds: number[]) => {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { yupResolver } from '@hookform/resolvers/yup';
2+
import { useForm } from 'react-hook-form';
3+
import { useToast } from '../../hooks/toasts.hooks';
4+
import * as yup from 'yup';
5+
import { useDeleteTeam, useSingleTeam } from '../../hooks/teams.hooks';
6+
import { useHistory } from 'react-router-dom';
7+
import LoadingIndicator from '../../components/LoadingIndicator';
8+
import ErrorPage from '../ErrorPage';
9+
import NERFormModal from '../../components/NERFormModal';
10+
import { Typography, FormControl, FormLabel } from '@mui/material';
11+
import ReactHookTextField from '../../components/ReactHookTextField';
12+
13+
interface DeleteTeamInputs {
14+
teamName: string;
15+
}
16+
17+
interface DeleteTeamModalProps {
18+
teamId: string;
19+
showModal: boolean;
20+
onHide: () => void;
21+
}
22+
23+
const DeleteTeamModal: React.FC<DeleteTeamModalProps> = ({ teamId, showModal, onHide }: DeleteTeamModalProps) => {
24+
const { data: team, isLoading: teamIsLoading, isError: teamIsError, error: teamError } = useSingleTeam(teamId);
25+
const { isLoading: deleteIsLoading, isError: deleteIsError, error: deleteError, mutateAsync } = useDeleteTeam();
26+
const toast = useToast();
27+
const teamNameTester = (teamName: string | undefined) =>
28+
team !== undefined && teamName !== undefined && teamName.toLowerCase() === team.teamName.toLowerCase();
29+
const schema = yup.object().shape({
30+
teamName: yup.string().required().test('team-name-test', 'Team name does not match', teamNameTester)
31+
});
32+
const {
33+
handleSubmit,
34+
control,
35+
reset,
36+
formState: { errors, isValid }
37+
} = useForm({
38+
resolver: yupResolver(schema),
39+
defaultValues: {
40+
teamName: ''
41+
},
42+
mode: 'onChange'
43+
});
44+
const history = useHistory();
45+
46+
const handleConfirm = async ({ teamName }: DeleteTeamInputs) => {
47+
if (team?.teamName.toLowerCase() === teamName.toLowerCase()) {
48+
try {
49+
await mutateAsync(teamId);
50+
onHide();
51+
history.goBack();
52+
toast.success('Team deleted successfully!');
53+
} catch (e) {
54+
if (e instanceof Error) {
55+
toast.error(e.message);
56+
}
57+
}
58+
}
59+
};
60+
61+
if (!team || teamIsLoading || deleteIsLoading) return <LoadingIndicator />;
62+
if (teamIsError) return <ErrorPage message={teamError.message} />;
63+
if (deleteIsError) return <ErrorPage message={deleteError?.message} />;
64+
65+
return (
66+
<NERFormModal
67+
open={showModal}
68+
onHide={onHide}
69+
title={`Delete Team ${team.teamName}`}
70+
reset={() => reset({ teamName: '' })}
71+
handleUseFormSubmit={handleSubmit}
72+
onFormSubmit={handleConfirm}
73+
formId="delete-team-form"
74+
submitText="Delete"
75+
disabled={!isValid}
76+
showCloseButton
77+
>
78+
<Typography sx={{ marginBottom: '1rem' }}>Are you sure you want to delete Team {team.teamName}?</Typography>
79+
<Typography sx={{ fontWeight: 'bold' }}>This action cannot be undone!</Typography>
80+
<FormControl>
81+
<FormLabel sx={{ marginTop: '1rem', marginBottom: '1rem' }}>
82+
To confirm deletion, please type in the name of this team.
83+
</FormLabel>
84+
<ReactHookTextField
85+
control={control}
86+
name="teamName"
87+
errorMessage={errors.teamName}
88+
placeholder="Enter Team Name here"
89+
sx={{ width: 1 }}
90+
type="string"
91+
/>
92+
</FormControl>
93+
</NERFormModal>
94+
);
95+
};
96+
97+
export default DeleteTeamModal;

src/frontend/src/pages/TeamsPage/DeleteTeamModalContainer/DeleteTeam.tsx

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/frontend/src/pages/TeamsPage/DeleteTeamModalContainer/DeleteTeamView.tsx

Lines changed: 0 additions & 125 deletions
This file was deleted.

src/frontend/src/pages/TeamsPage/TeamSpecificPage.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { NERButton } from '../../components/NERButton';
1414
import { useCurrentUser } from '../../hooks/users.hooks';
1515
import { isAdmin } from 'shared';
1616
import { useState } from 'react';
17-
import DeleteTeam from './DeleteTeamModalContainer/DeleteTeam';
17+
import DeleteTeamModal from './DeleteTeamModal';
1818

1919
interface ParamTypes {
2020
teamId: string;
@@ -42,7 +42,7 @@ const TeamSpecificPage: React.FC = () => {
4242

4343
return (
4444
<PageLayout
45-
headerRight={deleteButton}
45+
headerRight={isAdmin(user.role) && deleteButton}
4646
title={`Team ${data.teamName}`}
4747
previousPages={[{ name: 'Teams', route: routes.TEAMS }]}
4848
>
@@ -63,9 +63,7 @@ const TeamSpecificPage: React.FC = () => {
6363
<DescriptionPageBlock team={data} />
6464
</Grid>
6565
</Grid>
66-
{showDeleteModal && (
67-
<DeleteTeam teamId={teamId} showModal={showDeleteModal} handleClose={() => setShowDeleteModal(false)} />
68-
)}
66+
<DeleteTeamModal teamId={teamId} showModal={showDeleteModal} onHide={() => setShowDeleteModal(false)} />
6967
</PageLayout>
7068
);
7169
};

src/frontend/src/pages/TeamsPage/Teams.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import { Route, Switch } from 'react-router-dom';
77
import { routes } from '../../utils/routes';
88
import TeamsPage from './TeamsPage';
9-
import TeamDetailPage from './TeamSpecificPage';
9+
import TeamSpecificPage from './TeamSpecificPage';
1010

1111
const Teams: React.FC = () => {
1212
return (
1313
<Switch>
14-
<Route path={routes.TEAMS_BY_ID} component={TeamDetailPage} />
14+
<Route path={routes.TEAMS_BY_ID} component={TeamSpecificPage} />
1515
<Route path={routes.TEAMS} component={TeamsPage} />
1616
</Switch>
1717
);

0 commit comments

Comments
 (0)