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