diff --git a/src/components/Tool/FixEmailAddresses/FixEmailAddressPerson/FixEmailAddressPerson.tsx b/src/components/Tool/FixEmailAddresses/FixEmailAddressPerson/FixEmailAddressPerson.tsx index bc9b3382e..c2c06fab3 100644 --- a/src/components/Tool/FixEmailAddresses/FixEmailAddressPerson/FixEmailAddressPerson.tsx +++ b/src/components/Tool/FixEmailAddresses/FixEmailAddressPerson/FixEmailAddressPerson.tsx @@ -1,4 +1,4 @@ -import React, { Fragment, useMemo } from 'react'; +import React, { Fragment, ReactElement, useMemo } from 'react'; import { mdiCheckboxMarkedCircle, mdiDelete, mdiLock } from '@mdi/js'; import { Icon } from '@mdi/react'; import StarIcon from '@mui/icons-material/Star'; @@ -11,6 +11,7 @@ import { CardContent, CardHeader, FormControl, + FormHelperText, Grid, Hidden, Link, @@ -20,15 +21,18 @@ import { Typography, } from '@mui/material'; import clsx from 'clsx'; +import { Formik } from 'formik'; import { DateTime } from 'luxon'; import { useSnackbar } from 'notistack'; import { useTranslation } from 'react-i18next'; import { makeStyles } from 'tss-react/mui'; +import * as yup from 'yup'; import { SetContactFocus } from 'pages/accountLists/[accountListId]/tools/useToolsHelper'; import { useUpdateEmailAddressesMutation } from 'src/components/Tool/FixEmailAddresses/FixEmailAddresses.generated'; import { Confirmation } from 'src/components/common/Modal/Confirmation/Confirmation'; import useGetAppSettings from 'src/hooks/useGetAppSettings'; import { useLocale } from 'src/hooks/useLocale'; +import i18n from 'src/lib/i18n'; import { dateFormatShort } from 'src/lib/intlFormat'; import theme from 'src/theme'; import EmailValidationForm from '../EmailValidationForm'; @@ -100,7 +104,7 @@ export interface FixEmailAddressPersonProps { handleChange: ( personId: string, numberIndex: number, - event: React.ChangeEvent, + newEmail: string, ) => void; handleChangePrimary: (personId: string, emailIndex: number) => void; handleSingleConfirm: ( @@ -127,6 +131,13 @@ interface EmailToDelete { email: Email; } +const validationSchema = yup.object({ + newEmail: yup + .string() + .email(i18n.t('Invalid Email Address Format')) + .required(i18n.t('Please enter a valid email address')), +}); + export const FixEmailAddressPerson: React.FC = ({ person, dataState, @@ -308,61 +319,36 @@ export const FixEmailAddressPerson: React.FC = ({ {emails.map((email, index) => ( - - - - - - - {t('Source')}: - - - - {`${email.source} (${dateFormatShort( - DateTime.fromISO(email.updatedAt), - locale, - )})`} - - - - - - - - - {email.isPrimary ? ( - <> - - - {t('Source')}: - - - - handleChangePrimary(id, index) - } - /> - - ) : ( - <> + { + await handleChange(id, index, values.newEmail); + }} + > + {({ + values: { newEmail }, + setFieldValue, + handleSubmit, + errors, + }): ReactElement => ( + + + + = ({ {t('Source')}: - - - handleChangePrimary(id, index) - } - /> - - - )} - - - - - - - , - ) => handleChange(id, index, event)} - value={email.email} - disabled={email.source !== appName} - /> - - - {email.source === appName ? ( + + {`${email.source} (${dateFormatShort( + DateTime.fromISO(email.updatedAt), + locale, + )})`} + + + + + - handleDeleteEmailOpen({ id, email }) - } + justifyContent="center" className={classes.paddingX} > - - - + + {email.isPrimary ? ( + <> + + + {t('Source')}: + + + + handleChangePrimary(id, index) + } + /> + + ) : ( + <> + + + {t('Source')}: + + + + + handleChangePrimary(id, index) + } + /> + + + )} + - ) : ( + + - + + { + setFieldValue('newEmail', e.target.value); + handleSubmit(); + }} + disabled={email.source !== appName} + /> + + {errors.newEmail && errors.newEmail} + + + + {email.source === appName ? ( + + handleDeleteEmailOpen({ id, email }) + } + className={classes.paddingX} + > + + + + + ) : ( + + + + )} - )} - - - + + + )} + ))} - = ({ const handleChange = ( personId: string, numberIndex: number, - event: React.ChangeEvent, + newEmail: string, ): void => { const temp = { ...dataState }; - dataState[personId].emailAddresses[numberIndex].email = event.target.value; + dataState[personId].emailAddresses[numberIndex].email = newEmail; setDataState(temp); };