diff --git a/src/components/client/support-form/SupportForm.tsx b/src/components/client/support-form/SupportForm.tsx index d622a3ffa..ca5ec2fbb 100644 --- a/src/components/client/support-form/SupportForm.tsx +++ b/src/components/client/support-form/SupportForm.tsx @@ -3,13 +3,11 @@ import { useMutation } from '@tanstack/react-query' import { useTranslation } from 'next-i18next' import React, { useState, useRef } from 'react' import { FormikHelpers, FormikProps } from 'formik' -import { Stepper, Step, StepLabel, StepConnector, Hidden, Grid, Box } from '@mui/material' - +import { Stepper, Step, StepLabel, StepConnector, Grid, Box } from '@mui/material' import { AlertStore } from 'stores/AlertStore' import { createSupportRequest } from 'service/support' import GenericForm from 'components/common/form/GenericForm' import { ApiErrors, isAxiosError, matchValidator } from 'service/apiErrors' - import Actions from './Actions' import Roles from './steps/Roles' import StepIcon from './StepperIcon' @@ -24,6 +22,7 @@ import { SupportRequestResponse, SupportRequestInput, } from './helpers/support-form.types' +import ValidationObserver from './ValidationObserver' const initialValues: SupportFormData = { person: { @@ -108,6 +107,7 @@ export default function SupportForm() { const formRef = useRef>(null) const [activeStep, setActiveStep] = useState(Steps.ROLES) const [failedStep, setFailedStep] = useState(Steps.NONE) + const [currentValidatedField, setCurrentValidatedField] = useState(null) const mutation = useMutation< AxiosResponse, @@ -222,21 +222,22 @@ export default function SupportForm() { onSubmit={handleSubmit} initialValues={initialValues} validationSchema={validationSchema[activeStep]} - innerRef={formRef}> - - }> - {steps.map((step, index) => ( - - - {t(step.label)} - - - ))} - - + innerRef={formRef} + validateOnBlur={false} + validateOnChange={currentValidatedField === activeStep}> + } + sx={{ display: { xs: 'none', md: 'flex' } }}> + {steps.map((step, index) => ( + + + {t(step.label)} + + + ))} + {isThankYouStep(activeStep, steps) ? ( steps[activeStep].component ) : ( @@ -258,6 +259,10 @@ export default function SupportForm() { )} + ) } diff --git a/src/components/client/support-form/ValidationObserver.tsx b/src/components/client/support-form/ValidationObserver.tsx new file mode 100644 index 000000000..e1ca0394b --- /dev/null +++ b/src/components/client/support-form/ValidationObserver.tsx @@ -0,0 +1,39 @@ +import { useFormikContext } from 'formik' +import { useEffect, useRef } from 'react' +import { Steps } from './helpers/support-form.types' + +const STEPS_VALIDATION_MAP = { + [Steps.ROLES]: ['roles'], + [Steps.QUESTIONS]: ['benefactor', 'partner', 'volunteer', 'associationMember', 'company'], + [Steps.PERSON]: ['person'], +} + +const ValidationObserver = ({ + setCurrentValidatedField, + activeStep, +}: { + setCurrentValidatedField: React.Dispatch> + activeStep: number +}) => { + const activeStepRef = useRef(0) + const { errors, submitCount } = useFormikContext() + + useEffect(() => { + if (activeStepRef.current !== activeStep) { + activeStepRef.current = activeStep + setCurrentValidatedField(null) + } + + const value = Object.entries(STEPS_VALIDATION_MAP).find(([, value]) => { + return value.includes(Object.keys(errors)?.[0]) + }) + + if (submitCount > 0 && value) { + setCurrentValidatedField(Number(value?.[0])) + } + }, [errors, submitCount, activeStep]) + + return null +} + +export default ValidationObserver