import * as React from 'react'; import { useNavigate } from 'react-router'; import useNotifications from '../hooks/useNotifications/useNotifications'; import { createOne as createEmployee, validate as validateEmployee, type Employee, } from '../data/employees'; import EmployeeForm, { type FormFieldValue, type EmployeeFormState, } from './EmployeeForm'; import PageContainer from './PageContainer'; const INITIAL_FORM_VALUES: Partial = { 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: Partial) => { setFormState((previousState) => ({ ...previousState, values: newFormValues, })); }, [], ); const setFormErrors = React.useCallback( (newFormErrors: Partial) => { setFormState((previousState) => ({ ...previousState, errors: newFormErrors, })); }, [], ); const handleFormFieldChange = React.useCallback( (name: keyof EmployeeFormState['values'], value: FormFieldValue) => { const validateField = async (values: Partial) => { 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 as Omit); notifications.show('Employee created successfully.', { severity: 'success', autoHideDuration: 3000, }); navigate('/employees'); } catch (createError) { notifications.show( `Failed to create employee. Reason: ${(createError as Error).message}`, { severity: 'error', autoHideDuration: 3000, }, ); throw createError; } }, [formValues, navigate, notifications, setFormErrors]); return ( ); }