|
| 1 | +/* eslint-disable react/jsx-props-no-spreading */ |
| 2 | +import { |
| 3 | + Box, |
| 4 | + FormLabel, |
| 5 | + Input, |
| 6 | + FormControl, |
| 7 | + FormErrorMessage, |
| 8 | + Button, |
| 9 | + Textarea, |
| 10 | + useToast, |
| 11 | + Heading, |
| 12 | + Flex |
| 13 | +} from '@chakra-ui/react'; |
| 14 | +import { PropTypes } from 'prop-types'; |
| 15 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 16 | +import { useForm } from 'react-hook-form'; |
| 17 | +import * as yup from 'yup'; |
| 18 | +import { NPOBackend } from '../../utils/auth_utils'; |
| 19 | + |
| 20 | +const schema = yup.object({ |
| 21 | + date: yup.date().nullable().transform((curr, orig) => orig === '' ? null : curr).required('Date required'), |
| 22 | + location: yup.string().required('Location required').max(50, 'Location exceeds 50 character limit'), |
| 23 | + details: yup |
| 24 | + .string() |
| 25 | + .max(50, 'Details exceeds 50 character limit'), |
| 26 | +}); |
| 27 | + |
| 28 | +const AddDayForm = ({ onClose, onOpen, setDayId, dayData, setShouldDataRevalidate }) => { |
| 29 | + const toast = useToast(); |
| 30 | + const { |
| 31 | + handleSubmit, |
| 32 | + register, |
| 33 | + formState: { errors }, |
| 34 | + } = useForm({ |
| 35 | + resolver: yupResolver(schema), |
| 36 | + }); |
| 37 | + |
| 38 | + const submitData = async data => { |
| 39 | + const { date, details, location } = data; |
| 40 | + toast.closeAll(); |
| 41 | + try { |
| 42 | + const payload = { |
| 43 | + eventDate: date, |
| 44 | + location: location, |
| 45 | + notes: details, |
| 46 | + }; |
| 47 | + |
| 48 | + let response; |
| 49 | + if (dayData) { |
| 50 | + response = await NPOBackend.put(`/day/${dayData.id}`, payload); |
| 51 | + setShouldDataRevalidate(true); |
| 52 | + } else { |
| 53 | + response = await NPOBackend.post('/day/', payload); |
| 54 | + } |
| 55 | + |
| 56 | + if (response.status === 200 || (response.status == 201 && response.data.status === 'Success')) { |
| 57 | + const id = dayData ? dayData.id : response.data['id']; |
| 58 | + setDayId(id); |
| 59 | + onOpen(id); |
| 60 | + } else if (response.status == 201 && response.data.status === 'Failed') { |
| 61 | + toast({ |
| 62 | + title: 'This date already exists in the schedule.', |
| 63 | + description: `${date.toLocaleDateString()}`, |
| 64 | + status: 'error', |
| 65 | + variant: 'subtle', |
| 66 | + position: 'top-right', |
| 67 | + containerStyle: { |
| 68 | + mt: '6rem', |
| 69 | + }, |
| 70 | + duration: 3000, |
| 71 | + isClosable: true, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + } catch (error) { |
| 76 | + console.log(error); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <Box p="20px"> |
| 82 | + <Heading size="lg" textAlign="center" mb="8px" color="#2D3748">{!dayData ? 'Add New Day' : 'Edit Day Details'}</Heading> |
| 83 | + <form onSubmit={handleSubmit(data => submitData(data))}> |
| 84 | + <Box mb="4vh"> |
| 85 | + {/* DATE */} |
| 86 | + <Box mb="15px"> |
| 87 | + <FormControl isInvalid={errors && errors.date} > |
| 88 | + <FormLabel fontWeight="bold" color="gray.600">Date *</FormLabel> |
| 89 | + <Input |
| 90 | + size="md" |
| 91 | + type="date" |
| 92 | + {...register('date')} |
| 93 | + defaultValue={dayData && dayData.eventDate} |
| 94 | + /> |
| 95 | + <FormErrorMessage>{errors.date && errors.date.message}</FormErrorMessage> |
| 96 | + </FormControl> |
| 97 | + </Box> |
| 98 | + |
| 99 | + {/* LOCATION */} |
| 100 | + <Box mb="15px"> |
| 101 | + <FormControl isInvalid={errors && errors.location} > |
| 102 | + <FormLabel fontWeight="bold" color="gray.600">Location *</FormLabel> |
| 103 | + <Input placeholder='Location' {...register('location')} defaultValue={dayData && dayData.location}/> |
| 104 | + <FormErrorMessage>{errors.location && errors.location.message}</FormErrorMessage> |
| 105 | + </FormControl> |
| 106 | + </Box> |
| 107 | + |
| 108 | + |
| 109 | + {/* DETAILS */} |
| 110 | + <Box mb="15px"> |
| 111 | + <FormControl isInvalid={errors && errors.details}> |
| 112 | + <FormLabel fontWeight="bold" color="gray.600">Details</FormLabel> |
| 113 | + <Textarea placeholder='Details' {...register('details')} defaultValue={dayData && dayData.details}/> |
| 114 | + <FormErrorMessage> |
| 115 | + {errors.details && errors.details.message} |
| 116 | + </FormErrorMessage> |
| 117 | + </FormControl> |
| 118 | + </Box> |
| 119 | + </Box> |
| 120 | + |
| 121 | + <Flex justifyContent="space-between"> |
| 122 | + <Button width="48%" size='lg' onClick={onClose}> |
| 123 | + Cancel |
| 124 | + </Button> |
| 125 | + <Button |
| 126 | + width="48%" |
| 127 | + type='submit' |
| 128 | + size='lg' |
| 129 | + bgColor="#2c93d1" |
| 130 | + color="white" |
| 131 | + _hover={{ bgColor: '#1b6896' }} |
| 132 | + > |
| 133 | + {!dayData ? '+ Add To Schedule' : 'Save Changes'} |
| 134 | + </Button> |
| 135 | + </Flex> |
| 136 | + </form> |
| 137 | + </Box> |
| 138 | + ); |
| 139 | +}; |
| 140 | + |
| 141 | +AddDayForm.propTypes = { |
| 142 | + onClose: PropTypes.func, |
| 143 | + onOpen: PropTypes.func, |
| 144 | + setDayId: PropTypes.func, |
| 145 | + dayData: PropTypes.object, |
| 146 | + setShouldDataRevalidate: PropTypes.func |
| 147 | +}; |
| 148 | +AddDayForm.defaultProps = { |
| 149 | + onClose: () => {}, |
| 150 | + onOpen: () => {}, |
| 151 | + dayData: null |
| 152 | +}; |
| 153 | +export default AddDayForm; |
0 commit comments