|
| 1 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 2 | +import AttachMoneyIcon from '@mui/icons-material/AttachMoney'; |
| 3 | +import { FormControl, FormLabel } from '@mui/material'; |
| 4 | +import { DatePicker } from '@mui/x-date-pickers'; |
| 5 | +import { Controller, useForm } from 'react-hook-form'; |
| 6 | +import * as yup from 'yup'; |
| 7 | +import LoadingIndicator from '../../../components/LoadingIndicator'; |
| 8 | +import NERFormModal from '../../../components/NERFormModal'; |
| 9 | +import ReactHookTextField from '../../../components/ReactHookTextField'; |
| 10 | +import { useToast } from '../../../hooks/toasts.hooks'; |
| 11 | +import { Reimbursement } from 'shared'; |
| 12 | +import { UseMutateAsyncFunction } from 'react-query'; |
| 13 | + |
| 14 | +const schema = yup.object().shape({ |
| 15 | + amount: yup |
| 16 | + .number() |
| 17 | + .required('Refund amount is required') |
| 18 | + .positive('Refund amount must be positive') |
| 19 | + .test('two decimals', 'Refund amount must have at most two decimal places', (value) => { |
| 20 | + // technically this allows trailing zeros, but that's fine |
| 21 | + if (!value) return false; |
| 22 | + |
| 23 | + const strValue = value.toString(); |
| 24 | + const parts = strValue.split('.'); |
| 25 | + if (parts.length === 1) return true; |
| 26 | + return parts[1].length <= 2; |
| 27 | + }) |
| 28 | + .typeError('Refund amount is required'), |
| 29 | + dateReceived: yup.date().required() |
| 30 | +}); |
| 31 | + |
| 32 | +interface RefundModalProps { |
| 33 | + showModal: boolean; |
| 34 | + handleClose: () => void; |
| 35 | + mutateAsync: UseMutateAsyncFunction< |
| 36 | + Reimbursement, |
| 37 | + Error, |
| 38 | + { |
| 39 | + amount: number; |
| 40 | + dateReceived: string; |
| 41 | + }, |
| 42 | + unknown |
| 43 | + >; |
| 44 | + isLoading: boolean; |
| 45 | + defaultValues?: RefundModalInputs; |
| 46 | + title: string; |
| 47 | +} |
| 48 | + |
| 49 | +export interface RefundModalInputs { |
| 50 | + amount: string; // this allows us to display default value with 2 decimal places - the type is enforced and casted via the input field and form schema |
| 51 | + dateReceived: Date; |
| 52 | +} |
| 53 | + |
| 54 | +const RefundModal: React.FC<RefundModalProps> = ({ |
| 55 | + showModal: modalShow, |
| 56 | + handleClose, |
| 57 | + mutateAsync, |
| 58 | + defaultValues, |
| 59 | + isLoading, |
| 60 | + title |
| 61 | +}: RefundModalProps) => { |
| 62 | + const toast = useToast(); |
| 63 | + |
| 64 | + const { |
| 65 | + handleSubmit, |
| 66 | + control, |
| 67 | + formState: { errors, isValid }, |
| 68 | + watch, |
| 69 | + reset |
| 70 | + } = useForm({ |
| 71 | + resolver: yupResolver(schema), |
| 72 | + defaultValues: defaultValues ?? { |
| 73 | + amount: '0', |
| 74 | + dateReceived: new Date() |
| 75 | + }, |
| 76 | + mode: 'onChange' |
| 77 | + }); |
| 78 | + |
| 79 | + const handleConfirm = async (data: { amount: number; dateReceived: Date }) => { |
| 80 | + try { |
| 81 | + await mutateAsync({ |
| 82 | + amount: Math.round(data.amount * 100), |
| 83 | + dateReceived: data.dateReceived.toISOString() |
| 84 | + }); |
| 85 | + toast.success(defaultValues ? 'Account credit updated successfully' : 'New account credit reported successfully'); |
| 86 | + handleClose(); |
| 87 | + } catch (error: unknown) { |
| 88 | + if (error instanceof Error) { |
| 89 | + toast.error(error.message); |
| 90 | + } |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + return ( |
| 95 | + <NERFormModal |
| 96 | + open={modalShow} |
| 97 | + onHide={handleClose} |
| 98 | + title={title} |
| 99 | + reset={reset} |
| 100 | + handleUseFormSubmit={handleSubmit} |
| 101 | + onFormSubmit={handleConfirm} |
| 102 | + formId="reimbursement-form" |
| 103 | + disabled={!isValid || (defaultValues && defaultValues.amount === watch('amount'))} |
| 104 | + > |
| 105 | + {isLoading ? ( |
| 106 | + <LoadingIndicator /> |
| 107 | + ) : ( |
| 108 | + <FormControl> |
| 109 | + <FormLabel>Amount</FormLabel> |
| 110 | + <ReactHookTextField |
| 111 | + name="amount" |
| 112 | + type="number" |
| 113 | + control={control} |
| 114 | + sx={{ width: 1 }} |
| 115 | + startAdornment={<AttachMoneyIcon />} |
| 116 | + errorMessage={errors.amount} |
| 117 | + /> |
| 118 | + |
| 119 | + <FormLabel sx={{ paddingTop: 2 }}>Date Received</FormLabel> |
| 120 | + <Controller |
| 121 | + name="dateReceived" |
| 122 | + control={control} |
| 123 | + rules={{ required: true }} |
| 124 | + render={({ field: { onChange, value } }) => ( |
| 125 | + <DatePicker |
| 126 | + format="yyyy-MM-dd" |
| 127 | + onChange={(e) => onChange(e ?? new Date())} |
| 128 | + className={'padding: 10'} |
| 129 | + value={value} |
| 130 | + slotProps={{ textField: { autoComplete: 'off' } }} |
| 131 | + /> |
| 132 | + )} |
| 133 | + /> |
| 134 | + </FormControl> |
| 135 | + )} |
| 136 | + </NERFormModal> |
| 137 | + ); |
| 138 | +}; |
| 139 | + |
| 140 | +export default RefundModal; |
0 commit comments