|
| 1 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 2 | +import { useForm } from 'react-hook-form'; |
| 3 | +import { useSingleTeam } from '../../../hooks/teams.hooks'; |
| 4 | +import LoadingIndicator from '../../../components/LoadingIndicator'; |
| 5 | +import ErrorPage from '../../ErrorPage'; |
| 6 | +import * as yup from 'yup'; |
| 7 | +import { DeleteTeamInputs } from './DeleteTeam'; |
| 8 | +import { |
| 9 | + Dialog, |
| 10 | + DialogActions, |
| 11 | + DialogContent, |
| 12 | + DialogTitle, |
| 13 | + FormControl, |
| 14 | + FormLabel, |
| 15 | + IconButton, |
| 16 | + Typography |
| 17 | +} from '@mui/material'; |
| 18 | +import CloseIcon from '@mui/icons-material/Close'; |
| 19 | +import { wbsPipe } from 'shared'; |
| 20 | +import NERFailButton from '../../../components/NERFailButton'; |
| 21 | +import NERSuccessButton from '../../../components/NERSuccessButton'; |
| 22 | +import ReactHookTextField from '../../../components/ReactHookTextField'; |
| 23 | + |
| 24 | +interface DeleteTeamViewProps { |
| 25 | + teamId: string; |
| 26 | + showModal: boolean; |
| 27 | + onHide: () => void; |
| 28 | + onSubmit: (data: DeleteTeamInputs) => Promise<void>; |
| 29 | +} |
| 30 | + |
| 31 | +const DeleteTeamView: React.FC<DeleteTeamViewProps> = ({ teamId, showModal, onHide, onSubmit }: DeleteTeamViewProps) => { |
| 32 | + const { isLoading, isError, data: team, error } = useSingleTeam(teamId); |
| 33 | + |
| 34 | + const teamNameTester = (teamName: string | undefined) => |
| 35 | + team !== undefined && teamName !== undefined && teamName.toLowerCase === team.teamName.toLowerCase; |
| 36 | + |
| 37 | + const schema = yup.object().shape({ |
| 38 | + teamName: yup.string().required().test('team-name-test', 'Team name does not match', teamNameTester) |
| 39 | + }); |
| 40 | + |
| 41 | + const { |
| 42 | + handleSubmit, |
| 43 | + control, |
| 44 | + formState: { errors, isValid } |
| 45 | + } = useForm({ |
| 46 | + resolver: yupResolver(schema), |
| 47 | + defaultValues: { |
| 48 | + teamName: '' |
| 49 | + }, |
| 50 | + mode: 'onChange' |
| 51 | + }); |
| 52 | + |
| 53 | + if (isLoading || !team) return <LoadingIndicator />; |
| 54 | + if (isError) return <ErrorPage message={error.message} />; |
| 55 | + |
| 56 | + const onSubmitWrapper = async (data: { teamName: string }) => { |
| 57 | + const submitVal = { teamId }; |
| 58 | + |
| 59 | + await onSubmit(submitVal); |
| 60 | + }; |
| 61 | + |
| 62 | + return ( |
| 63 | + <Dialog open={showModal} onClose={onHide}> |
| 64 | + <DialogTitle |
| 65 | + className="font-weight-bold" |
| 66 | + sx={{ |
| 67 | + '&.MuiDialogTitle-root': { |
| 68 | + padding: '1rem 1.5rem 0' |
| 69 | + } |
| 70 | + }} |
| 71 | + >{`Delete Team ${team.teamName}`}</DialogTitle> |
| 72 | + <IconButton |
| 73 | + aria-label="close" |
| 74 | + onClick={onHide} |
| 75 | + sx={{ |
| 76 | + position: 'absolute', |
| 77 | + right: 8, |
| 78 | + top: 8, |
| 79 | + color: (theme) => theme.palette.grey[500] |
| 80 | + }} |
| 81 | + > |
| 82 | + <CloseIcon /> |
| 83 | + </IconButton> |
| 84 | + <DialogContent |
| 85 | + sx={{ |
| 86 | + '&.MuiDialogContent-root': { |
| 87 | + padding: '1rem 1.5rem' |
| 88 | + } |
| 89 | + }} |
| 90 | + > |
| 91 | + <Typography sx={{ marginBottom: '1rem' }}>Are you sure you want to delete Team {team.teamName}?</Typography> |
| 92 | + <Typography sx={{ fontWeight: 'bold' }}>This action cannot be undone!</Typography> |
| 93 | + <form |
| 94 | + id="delete-team-form" |
| 95 | + onSubmit={(e) => { |
| 96 | + e.preventDefault(); |
| 97 | + e.stopPropagation(); |
| 98 | + handleSubmit(onSubmitWrapper)(e); |
| 99 | + }} |
| 100 | + > |
| 101 | + <FormControl> |
| 102 | + <FormLabel sx={{ marginTop: '1rem', marginBottom: '1rem' }}> |
| 103 | + To confirm deletion, please type in the name of this team. |
| 104 | + </FormLabel> |
| 105 | + <ReactHookTextField |
| 106 | + control={control} |
| 107 | + name="teamName" |
| 108 | + errorMessage={errors.teamName} |
| 109 | + placeholder="Enter Team Name here" |
| 110 | + sx={{ width: 1 }} |
| 111 | + type="string" |
| 112 | + /> |
| 113 | + </FormControl> |
| 114 | + </form> |
| 115 | + </DialogContent> |
| 116 | + <DialogActions> |
| 117 | + <NERSuccessButton variant="contained" sx={{ mx: 1 }} onClick={onHide}> |
| 118 | + Cancel |
| 119 | + </NERSuccessButton> |
| 120 | + <NERFailButton variant="contained" type="submit" form="delete-wp-form" sx={{ mx: 1 }} disabled={!isValid}> |
| 121 | + Delete |
| 122 | + </NERFailButton> |
| 123 | + </DialogActions> |
| 124 | + </Dialog> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +export default DeleteTeamView; |
0 commit comments