|
| 1 | +import { useForm } from 'react-hook-form'; |
| 2 | +import NERFormModal from '../../../components/NERFormModal'; |
| 3 | +import { FormControl, FormLabel, FormHelperText } from '@mui/material'; |
| 4 | +import ReactHookTextField from '../../../components/ReactHookTextField'; |
| 5 | +import * as yup from 'yup'; |
| 6 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 7 | +import useFormPersist from 'react-hook-form-persist'; |
| 8 | +import { FormStorageKey } from '../../../utils/form'; |
| 9 | +import { GuestDefinitionPayload } from '../../../hooks/recruitment.hooks'; |
| 10 | +import { GuestDefinition, GuestDefinitionType } from 'shared'; |
| 11 | + |
| 12 | +interface GuestDefinitionFormModalProps { |
| 13 | + open: boolean; |
| 14 | + handleClose: () => void; |
| 15 | + type: GuestDefinitionType; |
| 16 | + defaultValues?: GuestDefinition; |
| 17 | + onSubmit: (data: GuestDefinitionPayload) => Promise<GuestDefinition>; |
| 18 | +} |
| 19 | + |
| 20 | +const schema = yup.object().shape({ |
| 21 | + term: yup.string().required('Term is required'), |
| 22 | + description: yup.string().required('Description is required'), |
| 23 | + order: yup.number().required('Order is required').min(0, 'Order must be non-negative'), |
| 24 | + icon: yup.string().optional(), |
| 25 | + buttonText: yup.string().optional(), |
| 26 | + buttonLink: yup.string().optional() |
| 27 | +}); |
| 28 | + |
| 29 | +const GuestDefinitionFormModal: React.FC<GuestDefinitionFormModalProps> = ({ |
| 30 | + open, |
| 31 | + handleClose, |
| 32 | + type, |
| 33 | + defaultValues, |
| 34 | + onSubmit |
| 35 | +}) => { |
| 36 | + const onFormSubmit = async (data: Omit<GuestDefinitionPayload, 'type'>) => { |
| 37 | + await onSubmit({ |
| 38 | + ...data, |
| 39 | + type, |
| 40 | + icon: data.icon || undefined, |
| 41 | + buttonText: data.buttonText || undefined, |
| 42 | + buttonLink: data.buttonLink || undefined |
| 43 | + }); |
| 44 | + handleClose(); |
| 45 | + }; |
| 46 | + |
| 47 | + const { |
| 48 | + handleSubmit, |
| 49 | + control, |
| 50 | + reset, |
| 51 | + formState: { errors }, |
| 52 | + watch, |
| 53 | + setValue |
| 54 | + } = useForm({ |
| 55 | + resolver: yupResolver(schema), |
| 56 | + defaultValues: { |
| 57 | + term: defaultValues?.term ?? '', |
| 58 | + description: defaultValues?.description ?? '', |
| 59 | + order: defaultValues?.order ?? 0, |
| 60 | + icon: defaultValues?.icon ?? '', |
| 61 | + buttonText: defaultValues?.buttonText ?? '', |
| 62 | + buttonLink: defaultValues?.buttonLink ?? '' |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + const formStorageKey = defaultValues ? FormStorageKey.EDIT_GUEST_DEFINITION : FormStorageKey.CREATE_GUEST_DEFINITION; |
| 67 | + |
| 68 | + useFormPersist(formStorageKey, { watch, setValue }); |
| 69 | + |
| 70 | + const handleCancel = () => { |
| 71 | + reset({ term: '', description: '', order: 0, icon: '', buttonText: '', buttonLink: '' }); |
| 72 | + sessionStorage.removeItem(formStorageKey); |
| 73 | + handleClose(); |
| 74 | + }; |
| 75 | + |
| 76 | + return ( |
| 77 | + <NERFormModal |
| 78 | + open={open} |
| 79 | + onHide={handleCancel} |
| 80 | + title={`${defaultValues ? 'Edit' : 'New'} ${type === GuestDefinitionType.PROJECT_MANAGEMENT ? 'Project Management' : 'Info Page'} Definition`} |
| 81 | + reset={() => reset({ term: '', description: '', order: 0, icon: '', buttonText: '', buttonLink: '' })} |
| 82 | + handleUseFormSubmit={handleSubmit} |
| 83 | + onFormSubmit={onFormSubmit} |
| 84 | + formId="guest-definition-form" |
| 85 | + showCloseButton |
| 86 | + > |
| 87 | + <FormControl fullWidth> |
| 88 | + <FormLabel>Term*</FormLabel> |
| 89 | + <ReactHookTextField name="term" control={control} placeholder="Enter term" sx={{ width: 1 }} /> |
| 90 | + <FormHelperText error>{errors.term?.message}</FormHelperText> |
| 91 | + </FormControl> |
| 92 | + <FormControl fullWidth> |
| 93 | + <FormLabel>Description*</FormLabel> |
| 94 | + <ReactHookTextField name="description" control={control} placeholder="Enter description" /> |
| 95 | + <FormHelperText error>{errors.description?.message}</FormHelperText> |
| 96 | + </FormControl> |
| 97 | + <FormControl fullWidth> |
| 98 | + <FormLabel>Order*</FormLabel> |
| 99 | + <ReactHookTextField name="order" control={control} type="number" placeholder="Enter display order" /> |
| 100 | + <FormHelperText error>{errors.order?.message}</FormHelperText> |
| 101 | + </FormControl> |
| 102 | + <FormControl fullWidth> |
| 103 | + <FormLabel>Icon</FormLabel> |
| 104 | + <ReactHookTextField name="icon" control={control} placeholder="Enter icon name (optional)" /> |
| 105 | + <FormHelperText error>{errors.icon?.message}</FormHelperText> |
| 106 | + </FormControl> |
| 107 | + <FormControl fullWidth> |
| 108 | + <FormLabel>Button Text</FormLabel> |
| 109 | + <ReactHookTextField name="buttonText" control={control} placeholder="Enter button text (optional)" /> |
| 110 | + <FormHelperText error>{errors.buttonText?.message}</FormHelperText> |
| 111 | + </FormControl> |
| 112 | + <FormControl fullWidth> |
| 113 | + <FormLabel>Button Link</FormLabel> |
| 114 | + <ReactHookTextField name="buttonLink" control={control} placeholder="Enter button link (optional)" /> |
| 115 | + <FormHelperText error>{errors.buttonLink?.message}</FormHelperText> |
| 116 | + </FormControl> |
| 117 | + </NERFormModal> |
| 118 | + ); |
| 119 | +}; |
| 120 | + |
| 121 | +export default GuestDefinitionFormModal; |
0 commit comments