import { Grid } from '@mui/material'; import { FC, ReactNode, useMemo } from 'react'; import { v4 as uuidv4 } from 'uuid'; import ActionGroup from '../ActionGroup'; import api from '../../lib/api'; import FormSummary from '../FormSummary'; import handleAPIError from '../../lib/handleAPIError'; import MessageGroup from '../MessageGroup'; import OutlinedInputWithLabel from '../OutlinedInputWithLabel'; import mailServerListSchema from './schema'; import SelectWithLabel from '../SelectWithLabel'; import useFormikUtils from '../../hooks/useFormikUtils'; import UncontrolledInput from '../UncontrolledInput'; const AddMailServerForm: FC = (props) => { const { localhostDomain = '', mailServerUuid, previousFormikValues, tools, } = props; const msUuid = useMemo( () => mailServerUuid ?? uuidv4(), [mailServerUuid], ); const { disableAutocomplete, disabledSubmit, formik, formikErrors, handleChange, } = useFormikUtils({ initialValues: previousFormikValues ?? { [msUuid]: { address: '', authentication: 'none', heloDomain: localhostDomain, port: 587, security: 'none', uuid: msUuid, }, }, onSubmit: (values, { setSubmitting }) => { const { [msUuid]: mailServer } = values; let actionProceedText: string = 'Add'; let errorMessage: ReactNode = <>Failed to add mail server.; let method: 'post' | 'put' = 'post'; let successMessage = <>Mail server added.; let titleText: string = 'Add mail server with the following?'; let url = '/mail-server'; if (previousFormikValues) { actionProceedText = 'Update'; errorMessage = <>Failed to update mail server.; method = 'put'; successMessage = <>Mail server updated.; titleText = `Update ${mailServer.address}:${mailServer.port} with the following?`; url += `/${msUuid}`; } const { confirmPassword, uuid, ...rest } = mailServer; tools.confirm.prepare({ actionProceedText, content: , onCancelAppend: () => setSubmitting(false), onProceedAppend: () => { tools.confirm.loading(true); api[method](url, mailServer) .then(() => { tools.confirm.finish('Success', { children: successMessage }); tools[method === 'post' ? 'add' : 'edit'].open(false); }) .catch((error) => { const emsg = handleAPIError(error); emsg.children = ( <> {errorMessage} {emsg.children} ); tools.confirm.finish('Error', emsg); setSubmitting(false); }); }, titleText, }); tools.confirm.open(true); }, validationSchema: mailServerListSchema, }); const addressChain = useMemo(() => `${msUuid}.address`, [msUuid]); const authenticationChain = useMemo( () => `${msUuid}.authentication`, [msUuid], ); const confirmPasswordChain = useMemo( () => `${msUuid}.confirmPassword`, [msUuid], ); const heloDomainChain = useMemo( () => `${msUuid}.heloDomain`, [msUuid], ); const passwordChain = useMemo(() => `${msUuid}.password`, [msUuid]); const portChain = useMemo(() => `${msUuid}.port`, [msUuid]); const securityChain = useMemo(() => `${msUuid}.security`, [msUuid]); const usernameChain = useMemo(() => `${msUuid}.username`, [msUuid]); return ( { event.preventDefault(); formik.submitForm(); }} container columns={{ xs: 1, sm: 2 }} spacing="1em" > } /> } /> } /> } /> } /> } /> } /> } /> ); }; export default AddMailServerForm;