|
| 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; |
0 commit comments