Advertisement
Guest User

Untitled

a guest
Nov 17th, 2023
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { useRouter } from 'next/router';
  2. import React from 'react';
  3. import { RegisterFormStepOne, RegisterFormStepTwo } from '~/components/auth/register-form/steps';
  4. import { Alert, AlertDescription, AlertTitle } from '~/components/ui/Alert';
  5. import { type RegisterDto } from '~/config/form/auth/register-dto';
  6. import { api } from '~/utils/api';
  7.  
  8. const TOTAL_STEPS = 2;
  9.  
  10. const getMaxStep = (currentStep: number) => {
  11.     return Math.min(currentStep + 1, TOTAL_STEPS - 1);
  12. };
  13.  
  14. export const RegisterForm = () => {
  15.     const router = useRouter();
  16.  
  17.     const [wasDataMutated, setWasDataMutated] = React.useState(false);
  18.  
  19.     const [data, setData] = React.useState<RegisterDto>({
  20.         companyAddress: '',
  21.         companyCity: '',
  22.         companyEmail: '',
  23.         companyName: '',
  24.         companyPhone: '',
  25.         companyPostalCode: '',
  26.         email: '',
  27.         firstName: '',
  28.         gender: 'male',
  29.         lastName: '',
  30.         password: '',
  31.         passwordConfirmation: '',
  32.     });
  33.  
  34.     const registrationMutation = api.auth.register.useMutation();
  35.  
  36.     const currentStep = React.useMemo(() => {
  37.         if (!wasDataMutated) {
  38.             return 0;
  39.         }
  40.  
  41.         if (router.query.step) {
  42.             return getMaxStep(Number.parseInt(router.query.step as string, 10));
  43.         }
  44.  
  45.         return 0;
  46.     }, [router.query.step, wasDataMutated]);
  47.  
  48.     const onGoToNextStep = (stepData: Partial<RegisterDto>) => {
  49.         setWasDataMutated(true);
  50.         setData(oldData => ({
  51.             ...oldData,
  52.             ...stepData,
  53.         }));
  54.     };
  55.  
  56.     const onGoToPreviousStep: React.MouseEventHandler<HTMLButtonElement> = (event) => {
  57.         event.preventDefault();
  58.  
  59.         void router.back();
  60.     };
  61.  
  62.     React.useEffect(() => {
  63.         if (!wasDataMutated) {
  64.             const resetSteps = async () => await router.replace(router.pathname);
  65.  
  66.             void resetSteps();
  67.  
  68.             return;
  69.         }
  70.  
  71.         const routePushOrSubmit = async () => {
  72.             if (currentStep + 1 === TOTAL_STEPS) {
  73.                 await registrationMutation.mutateAsync(data);
  74.  
  75.                 return;
  76.             }
  77.  
  78.  
  79.             await router.push({
  80.                 pathname: router.pathname,
  81.                 query: {
  82.                     step: getMaxStep(currentStep + 1),
  83.                 },
  84.             });
  85.         };
  86.         void routePushOrSubmit();
  87.     }, [data]);
  88.  
  89.     const CurrentStepComponent = React.useMemo(() => {
  90.         if (currentStep === 0) {
  91.             return RegisterFormStepOne;
  92.         }
  93.  
  94.         return RegisterFormStepTwo;
  95.     }, [currentStep]);
  96.  
  97.     return (
  98.         <>
  99.             {registrationMutation.error?.message &&
  100.                 <Alert variant="destructive">
  101.                     <AlertTitle>
  102.                         Er is iets misgegaan
  103.                     </AlertTitle>
  104.                     <AlertDescription>
  105.                         {registrationMutation.error?.message}
  106.                     </AlertDescription>
  107.                 </Alert>
  108.             }
  109.             <div className="text-center">
  110.                 <p className="font-medium">
  111.                     Stap {currentStep + 1} van {TOTAL_STEPS}
  112.                 </p>
  113.                 <p className="text-sm">
  114.                     {currentStep === 0 ? 'Persoonsgegevens' : 'Instelling'}
  115.                 </p>
  116.             </div>
  117.             <CurrentStepComponent
  118.                 onGoToNextStep={onGoToNextStep}
  119.                 onGoToPreviousStep={onGoToPreviousStep}
  120.                 isLoading={registrationMutation.isLoading}
  121.                 data={data}
  122.             />
  123.         </>
  124.     );
  125. };
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement