109 lines
2.9 KiB
JavaScript
109 lines
2.9 KiB
JavaScript
|
|
import * as React from 'react';
|
||
|
|
import { useNavigate } from 'react-router';
|
||
|
|
import useNotifications from '../hooks/useNotifications/useNotifications';
|
||
|
|
import {
|
||
|
|
createOne as createEmployee,
|
||
|
|
validate as validateEmployee,
|
||
|
|
} from '../data/employees';
|
||
|
|
import EmployeeForm from './EmployeeForm';
|
||
|
|
import PageContainer from './PageContainer';
|
||
|
|
|
||
|
|
const INITIAL_FORM_VALUES = {
|
||
|
|
role: 'Market',
|
||
|
|
isFullTime: true,
|
||
|
|
};
|
||
|
|
|
||
|
|
export default function EmployeeCreate() {
|
||
|
|
const navigate = useNavigate();
|
||
|
|
|
||
|
|
const notifications = useNotifications();
|
||
|
|
|
||
|
|
const [formState, setFormState] = React.useState(() => ({
|
||
|
|
values: INITIAL_FORM_VALUES,
|
||
|
|
errors: {},
|
||
|
|
}));
|
||
|
|
const formValues = formState.values;
|
||
|
|
const formErrors = formState.errors;
|
||
|
|
|
||
|
|
const setFormValues = React.useCallback((newFormValues) => {
|
||
|
|
setFormState((previousState) => ({
|
||
|
|
...previousState,
|
||
|
|
values: newFormValues,
|
||
|
|
}));
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
const setFormErrors = React.useCallback((newFormErrors) => {
|
||
|
|
setFormState((previousState) => ({
|
||
|
|
...previousState,
|
||
|
|
errors: newFormErrors,
|
||
|
|
}));
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
const handleFormFieldChange = React.useCallback(
|
||
|
|
(name, value) => {
|
||
|
|
const validateField = async (values) => {
|
||
|
|
const { issues } = validateEmployee(values);
|
||
|
|
setFormErrors({
|
||
|
|
...formErrors,
|
||
|
|
[name]: issues?.find((issue) => issue.path?.[0] === name)?.message,
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
const newFormValues = { ...formValues, [name]: value };
|
||
|
|
|
||
|
|
setFormValues(newFormValues);
|
||
|
|
validateField(newFormValues);
|
||
|
|
},
|
||
|
|
[formValues, formErrors, setFormErrors, setFormValues],
|
||
|
|
);
|
||
|
|
|
||
|
|
const handleFormReset = React.useCallback(() => {
|
||
|
|
setFormValues(INITIAL_FORM_VALUES);
|
||
|
|
}, [setFormValues]);
|
||
|
|
|
||
|
|
const handleFormSubmit = React.useCallback(async () => {
|
||
|
|
const { issues } = validateEmployee(formValues);
|
||
|
|
if (issues && issues.length > 0) {
|
||
|
|
setFormErrors(
|
||
|
|
Object.fromEntries(issues.map((issue) => [issue.path?.[0], issue.message])),
|
||
|
|
);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
setFormErrors({});
|
||
|
|
|
||
|
|
try {
|
||
|
|
await createEmployee(formValues);
|
||
|
|
notifications.show('Employee created successfully.', {
|
||
|
|
severity: 'success',
|
||
|
|
autoHideDuration: 3000,
|
||
|
|
});
|
||
|
|
|
||
|
|
navigate('/employees');
|
||
|
|
} catch (createError) {
|
||
|
|
notifications.show(
|
||
|
|
`Failed to create employee. Reason: ${createError.message}`,
|
||
|
|
{
|
||
|
|
severity: 'error',
|
||
|
|
autoHideDuration: 3000,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
throw createError;
|
||
|
|
}
|
||
|
|
}, [formValues, navigate, notifications, setFormErrors]);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<PageContainer
|
||
|
|
title="New Employee"
|
||
|
|
breadcrumbs={[{ title: 'Employees', path: '/employees' }, { title: 'New' }]}
|
||
|
|
>
|
||
|
|
<EmployeeForm
|
||
|
|
formState={formState}
|
||
|
|
onFieldChange={handleFormFieldChange}
|
||
|
|
onSubmit={handleFormSubmit}
|
||
|
|
onReset={handleFormReset}
|
||
|
|
submitButtonLabel="Create"
|
||
|
|
/>
|
||
|
|
</PageContainer>
|
||
|
|
);
|
||
|
|
}
|