[057453c] | 1 | import { useCallback } from 'react';
|
---|
| 2 | import { useForm } from 'react-hook-form';
|
---|
| 3 | import { zodResolver } from '@hookform/resolvers/zod';
|
---|
| 4 | // @mui
|
---|
| 5 | import LoadingButton from '@mui/lab/LoadingButton';
|
---|
| 6 | import Card from '@mui/material/Card';
|
---|
| 7 | import Stack from '@mui/material/Stack';
|
---|
| 8 | import Grid from '@mui/material/Unstable_Grid2';
|
---|
| 9 | // routes
|
---|
| 10 | import { paths } from 'src/routes/paths';
|
---|
| 11 | import { useRouter } from 'src/routes/hooks';
|
---|
| 12 | // types
|
---|
| 13 | import { NewEmployee, newEmployeeSchema } from 'src/schemas';
|
---|
| 14 | // components
|
---|
| 15 | import { useSnackbar } from 'src/components/snackbar';
|
---|
| 16 | import FormProvider, { RHFTextField, RHFUploadAvatar } from 'src/components/hook-form';
|
---|
| 17 | import { createEmployee } from 'src/api/employee';
|
---|
| 18 |
|
---|
| 19 | export default function EmployeeNewEditForm() {
|
---|
| 20 | const router = useRouter();
|
---|
| 21 | const { enqueueSnackbar } = useSnackbar();
|
---|
| 22 |
|
---|
| 23 | const methods = useForm<NewEmployee>({
|
---|
| 24 | resolver: zodResolver(newEmployeeSchema),
|
---|
| 25 | defaultValues: {
|
---|
| 26 | name: '',
|
---|
| 27 | email: '',
|
---|
| 28 | status: 'active' as const,
|
---|
| 29 | project: '',
|
---|
| 30 | iban: '',
|
---|
| 31 | photo: '',
|
---|
| 32 | cv: '',
|
---|
| 33 | },
|
---|
| 34 | });
|
---|
| 35 |
|
---|
| 36 | const {
|
---|
| 37 | handleSubmit,
|
---|
| 38 | formState: { isSubmitting },
|
---|
| 39 | } = methods;
|
---|
| 40 |
|
---|
| 41 | const onSubmit = handleSubmit(async (data) => {
|
---|
| 42 | try {
|
---|
| 43 | await createEmployee(data);
|
---|
| 44 | enqueueSnackbar('Create success!');
|
---|
| 45 | router.push(paths.dashboard.employee.list);
|
---|
| 46 | } catch (error) {
|
---|
| 47 | console.error(error);
|
---|
| 48 | }
|
---|
| 49 | });
|
---|
| 50 |
|
---|
| 51 | return (
|
---|
| 52 | <FormProvider methods={methods} onSubmit={onSubmit}>
|
---|
| 53 | <Grid container spacing={3}>
|
---|
| 54 | <Grid xs={12} md={8}>
|
---|
| 55 | <Card sx={{ p: 3 }}>
|
---|
| 56 | <Stack spacing={3}>
|
---|
| 57 | <RHFTextField name="name" label="Full Name" />
|
---|
| 58 | <RHFTextField name="email" label="Email Address" />
|
---|
| 59 | <RHFTextField name="project" label="Project" />
|
---|
| 60 | <RHFTextField name="iban" label="IBAN" />
|
---|
| 61 | <RHFTextField name="cv" label="CV Link" />
|
---|
| 62 | </Stack>
|
---|
| 63 |
|
---|
| 64 | <Stack alignItems="flex-end" sx={{ mt: 3 }}>
|
---|
| 65 | <LoadingButton type="submit" variant="contained" loading={isSubmitting}>
|
---|
| 66 | Create Employee
|
---|
| 67 | </LoadingButton>
|
---|
| 68 | </Stack>
|
---|
| 69 | </Card>
|
---|
| 70 | </Grid>
|
---|
| 71 | </Grid>
|
---|
| 72 | </FormProvider>
|
---|
| 73 | );
|
---|
| 74 | }
|
---|