|
| 1 | +import { Controller, useForm } from 'react-hook-form'; |
| 2 | +import { useToast } from '../../hooks/toasts.hooks'; |
| 3 | +import * as yup from 'yup'; |
| 4 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 5 | +import { CreateTeamPayload, useCreateTeam } from '../../hooks/teams.hooks'; |
| 6 | +import ErrorPage from '../ErrorPage'; |
| 7 | +import LoadingIndicator from '../../components/LoadingIndicator'; |
| 8 | +import NERFormModal from '../../components/NERFormModal'; |
| 9 | +import { FormControl, FormHelperText, FormLabel } from '@mui/material'; |
| 10 | +import ReactHookTextField from '../../components/ReactHookTextField'; |
| 11 | +import { useAllUsers } from '../../hooks/users.hooks'; |
| 12 | +import { User, isHead } from 'shared'; |
| 13 | +import NERAutocomplete from '../../components/NERAutocomplete'; |
| 14 | +import { fullNamePipe } from '../../utils/pipes'; |
| 15 | + |
| 16 | +const schema = yup.object().shape({ |
| 17 | + teamName: yup.string().required('Team Name is Required'), |
| 18 | + headId: yup.string().required('You must set a Head'), |
| 19 | + slackId: yup.string().required('Team Channel SlackId is required'), |
| 20 | + description: yup.string().required('Description is Required') |
| 21 | +}); |
| 22 | + |
| 23 | +interface CreateTeamModalProps { |
| 24 | + showModal: boolean; |
| 25 | + handleClose: () => void; |
| 26 | +} |
| 27 | + |
| 28 | +const userToAutocompleteOption = (user: User): { label: string; id: string } => { |
| 29 | + return { label: `${fullNamePipe(user)} (${user.email})`, id: `${user.userId}` }; |
| 30 | +}; |
| 31 | + |
| 32 | +const CreateTeamModal = ({ showModal, handleClose }: CreateTeamModalProps) => { |
| 33 | + const { isLoading, mutateAsync } = useCreateTeam(); |
| 34 | + const { isLoading: allUsersIsLoading, isError: allUsersIsError, error: allUsersError, data: users } = useAllUsers(); |
| 35 | + const toast = useToast(); |
| 36 | + |
| 37 | + const defaultValues = { |
| 38 | + teamName: '', |
| 39 | + slackId: '', |
| 40 | + description: '', |
| 41 | + headId: '' |
| 42 | + }; |
| 43 | + |
| 44 | + const { |
| 45 | + handleSubmit, |
| 46 | + control, |
| 47 | + formState: { errors }, |
| 48 | + reset |
| 49 | + } = useForm({ |
| 50 | + resolver: yupResolver(schema), |
| 51 | + defaultValues |
| 52 | + }); |
| 53 | + |
| 54 | + if (allUsersIsError) return <ErrorPage message={allUsersError?.message} />; |
| 55 | + if (isLoading || allUsersIsLoading || !users) return <LoadingIndicator />; |
| 56 | + |
| 57 | + const onFormSubmit = async (data: CreateTeamPayload) => { |
| 58 | + try { |
| 59 | + await mutateAsync({ ...data, headId: Number(data.headId) }); |
| 60 | + reset(defaultValues); |
| 61 | + handleClose(); |
| 62 | + } catch (error: unknown) { |
| 63 | + if (error instanceof Error) { |
| 64 | + toast.error(error.message, 5000); |
| 65 | + } |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + const headOptions = users |
| 70 | + .filter((user) => isHead(user.role)) |
| 71 | + .sort((a, b) => (a.firstName > b.firstName ? 1 : -1)) |
| 72 | + .map(userToAutocompleteOption); |
| 73 | + |
| 74 | + return ( |
| 75 | + <NERFormModal |
| 76 | + open={showModal} |
| 77 | + onHide={handleClose} |
| 78 | + title={'Create Team'} |
| 79 | + reset={reset} |
| 80 | + handleUseFormSubmit={handleSubmit} |
| 81 | + onFormSubmit={onFormSubmit} |
| 82 | + formId={'create-team-form'} |
| 83 | + showCloseButton |
| 84 | + > |
| 85 | + <FormControl fullWidth> |
| 86 | + <FormLabel>Team Name</FormLabel> |
| 87 | + <ReactHookTextField name="teamName" control={control} fullWidth /> |
| 88 | + <FormHelperText error>{errors.teamName?.message}</FormHelperText> |
| 89 | + </FormControl> |
| 90 | + <FormControl fullWidth> |
| 91 | + <FormLabel>Head</FormLabel> |
| 92 | + <Controller |
| 93 | + name="headId" |
| 94 | + control={control} |
| 95 | + render={({ field: { onChange, value } }) => ( |
| 96 | + <NERAutocomplete |
| 97 | + value={headOptions.find((option) => option.id === value) || { id: '', label: '' }} |
| 98 | + onChange={(_event, newValue) => onChange(newValue ? newValue.id : '')} |
| 99 | + options={headOptions} |
| 100 | + id="create-team-head" |
| 101 | + size="small" |
| 102 | + placeholder="Choose a user" |
| 103 | + /> |
| 104 | + )} |
| 105 | + /> |
| 106 | + <FormHelperText error>{errors.headId?.message}</FormHelperText> |
| 107 | + </FormControl> |
| 108 | + <FormControl fullWidth> |
| 109 | + <FormLabel>Slack Channel ID</FormLabel> |
| 110 | + <ReactHookTextField name="slackId" control={control} fullWidth /> |
| 111 | + <FormHelperText error>{errors.slackId?.message}</FormHelperText> |
| 112 | + </FormControl> |
| 113 | + <FormControl fullWidth> |
| 114 | + <FormLabel>Description</FormLabel> |
| 115 | + <ReactHookTextField name="description" control={control} fullWidth multiline rows={5} /> |
| 116 | + <FormHelperText error>{errors.description?.message}</FormHelperText> |
| 117 | + </FormControl> |
| 118 | + </NERFormModal> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +export default CreateTeamModal; |
0 commit comments