[057453c] | 1 | import * as Yup from 'yup';
|
---|
| 2 | import { useMemo } from 'react';
|
---|
| 3 | import { useForm } from 'react-hook-form';
|
---|
| 4 | import { zodResolver } from '@hookform/resolvers/zod';
|
---|
| 5 | // @mui
|
---|
| 6 | import LoadingButton from '@mui/lab/LoadingButton';
|
---|
| 7 | import Box from '@mui/material/Box';
|
---|
| 8 | import Button from '@mui/material/Button';
|
---|
| 9 | import Dialog from '@mui/material/Dialog';
|
---|
| 10 | import MenuItem from '@mui/material/MenuItem';
|
---|
| 11 | import DialogTitle from '@mui/material/DialogTitle';
|
---|
| 12 | import DialogActions from '@mui/material/DialogActions';
|
---|
| 13 | import DialogContent from '@mui/material/DialogContent';
|
---|
| 14 | // types
|
---|
| 15 | import { Employee, employeeSchema } from 'src/schemas';
|
---|
| 16 | // components
|
---|
| 17 | import { useSnackbar } from 'src/components/snackbar';
|
---|
| 18 | import FormProvider, { RHFSelect, RHFTextField } from 'src/components/hook-form';
|
---|
| 19 |
|
---|
| 20 | // ----------------------------------------------------------------------
|
---|
| 21 |
|
---|
| 22 | type Props = {
|
---|
| 23 | open: boolean;
|
---|
| 24 | onClose: VoidFunction;
|
---|
| 25 | currentEmployee?: Employee;
|
---|
| 26 | };
|
---|
| 27 |
|
---|
| 28 | export const EMPLOYEE_STATUS_OPTIONS = [
|
---|
| 29 | { value: 'active', label: 'Active' },
|
---|
| 30 | { value: 'inactive', label: 'Inactive' },
|
---|
| 31 | ];
|
---|
| 32 |
|
---|
| 33 | export default function EmployeeQuickEditForm({ currentEmployee, open, onClose }: Props) {
|
---|
| 34 | const { enqueueSnackbar } = useSnackbar();
|
---|
| 35 |
|
---|
| 36 | const defaultValues = useMemo(
|
---|
| 37 | () => ({
|
---|
| 38 | name: currentEmployee?.name || '',
|
---|
| 39 | email: currentEmployee?.email || '',
|
---|
| 40 | status: currentEmployee?.status || 'active',
|
---|
| 41 | project: currentEmployee?.project || '',
|
---|
| 42 | iban: currentEmployee?.iban || '',
|
---|
| 43 | }),
|
---|
| 44 | [currentEmployee]
|
---|
| 45 | );
|
---|
| 46 |
|
---|
| 47 | const methods = useForm({
|
---|
| 48 | resolver: zodResolver(employeeSchema),
|
---|
| 49 | defaultValues,
|
---|
| 50 | });
|
---|
| 51 |
|
---|
| 52 | const {
|
---|
| 53 | reset,
|
---|
| 54 | handleSubmit,
|
---|
| 55 | formState: { isSubmitting },
|
---|
| 56 | } = methods;
|
---|
| 57 |
|
---|
| 58 | const onSubmit = handleSubmit(async (data) => {
|
---|
| 59 | try {
|
---|
| 60 | await new Promise((resolve) => setTimeout(resolve, 500));
|
---|
| 61 | reset();
|
---|
| 62 | onClose();
|
---|
| 63 | enqueueSnackbar('Update success!');
|
---|
| 64 | } catch (error) {
|
---|
| 65 | console.error(error);
|
---|
| 66 | }
|
---|
| 67 | });
|
---|
| 68 |
|
---|
| 69 | return (
|
---|
| 70 | <Dialog fullWidth maxWidth="xs" open={open} onClose={onClose}>
|
---|
| 71 | <FormProvider methods={methods} onSubmit={onSubmit}>
|
---|
| 72 | <DialogTitle>Quick Update</DialogTitle>
|
---|
| 73 |
|
---|
| 74 | <DialogContent>
|
---|
| 75 | <Box
|
---|
| 76 | rowGap={3}
|
---|
| 77 | columnGap={2}
|
---|
| 78 | display="grid"
|
---|
| 79 | gridTemplateColumns={{
|
---|
| 80 | xs: 'repeat(1, 1fr)',
|
---|
| 81 | }}
|
---|
| 82 | sx={{ pt: 3 }}
|
---|
| 83 | >
|
---|
| 84 | <RHFTextField name="name" label="Full Name" />
|
---|
| 85 | <RHFTextField name="email" label="Email Address" />
|
---|
| 86 | <RHFTextField name="project" label="Project" />
|
---|
| 87 | <RHFTextField name="iban" label="IBAN" />
|
---|
| 88 | <RHFSelect name="status" label="Status">
|
---|
| 89 | {EMPLOYEE_STATUS_OPTIONS.map((status) => (
|
---|
| 90 | <MenuItem key={status.value} value={status.value}>
|
---|
| 91 | {status.label}
|
---|
| 92 | </MenuItem>
|
---|
| 93 | ))}
|
---|
| 94 | </RHFSelect>
|
---|
| 95 | </Box>
|
---|
| 96 | </DialogContent>
|
---|
| 97 |
|
---|
| 98 | <DialogActions>
|
---|
| 99 | <Button variant="outlined" onClick={onClose}>
|
---|
| 100 | Cancel
|
---|
| 101 | </Button>
|
---|
| 102 |
|
---|
| 103 | <LoadingButton type="submit" variant="contained" loading={isSubmitting}>
|
---|
| 104 | Update
|
---|
| 105 | </LoadingButton>
|
---|
| 106 | </DialogActions>
|
---|
| 107 | </FormProvider>
|
---|
| 108 | </Dialog>
|
---|
| 109 | );
|
---|
| 110 | }
|
---|