All files / src/components/public/Registration PersonalDetailsStep.tsx

100% Statements 29/29
69.23% Branches 18/26
100% Functions 13/13
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245                                                                                                    1x   136x   136x   136x                   136x                         136x   14x 14x 14x 14x   14x     1x 1x 1x 1x                 136x                             136x                                               136x                                                   136x                                                 136x                   14x                                   136x                                               1x  
/**
 * PersonalDetailsStep Component (Story 4.1.5 - Task 5)
 *
 * Step 1 of registration wizard - collects personal information.
 * Uses react-hook-form with zod validation for robust form handling.
 */
 
import { useMemo } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { useImperativeHandle, forwardRef } from 'react';
import { useTranslation } from 'react-i18next';
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from '@/components/public/ui/form';
import { Input } from '@/components/public/ui/input';
import { CompanyAutocomplete } from './CompanyAutocomplete';
import type { CreateRegistrationRequest } from '@/types/event.types';
 
export interface PersonalDetailsStepProps {
  /** Current form data */
  formData: CreateRegistrationRequest;
  /** Form data update handler */
  setFormData: (
    data:
      | CreateRegistrationRequest
      | ((prev: CreateRegistrationRequest) => CreateRegistrationRequest)
  ) => void;
}
 
export interface PersonalDetailsStepRef {
  validateAndSync: () => Promise<boolean>;
  /** Pre-fill fields with provided values (e.g. from authenticated user profile) */
  prefill: (
    values: Partial<Pick<CreateRegistrationRequest, 'firstName' | 'lastName' | 'email'>>
  ) => void;
}
 
/**
 * Step 1: Personal Details Form
 *
 * Collects firstName, lastName, email, company, and role.
 * Form is self-contained and only syncs to parent when validated.
 */
export const PersonalDetailsStep = forwardRef<PersonalDetailsStepRef, PersonalDetailsStepProps>(
  ({ formData, setFormData }, ref) => {
    const { t } = useTranslation('registration');
 
    const personalDetailsSchema = useMemo(
      () =>
        z.object({
          firstName: z.string().min(1, t('personalDetails.validation.firstNameRequired')),
          lastName: z.string().min(1, t('personalDetails.validation.lastNameRequired')),
          email: z.string().email(t('personalDetails.validation.invalidEmail')),
          company: z.string().min(1, t('personalDetails.validation.companyRequired')),
          role: z.string().min(1, t('personalDetails.validation.roleRequired')),
        }),
      [t]
    );
 
    const form = useForm({
      resolver: zodResolver(personalDetailsSchema),
      mode: 'onBlur', // Only validate on blur, not on change
      defaultValues: {
        firstName: formData.firstName || '',
        lastName: formData.lastName || '',
        email: formData.email || '',
        company: formData.company || '',
        role: formData.role || '',
      },
    });
 
    // Expose validation and prefill functions to parent
    useImperativeHandle(ref, () => ({
      validateAndSync: async () => {
        const isValid = await form.trigger();
        Eif (isValid) {
          const values = form.getValues();
          setFormData((prev) => ({ ...prev, ...values }));
        }
        return isValid;
      },
      prefill: (values) => {
        Eif (values.firstName) form.setValue('firstName', values.firstName);
        Eif (values.lastName) form.setValue('lastName', values.lastName);
        Eif (values.email) form.setValue('email', values.email);
        setFormData((prev) => ({
          ...prev,
          ...(values.firstName ? { firstName: values.firstName } : {}),
          ...(values.lastName ? { lastName: values.lastName } : {}),
          ...(values.email ? { email: values.email } : {}),
        }));
      },
    }));
 
    return (
      <div className="space-y-6">
        <div>
          <h2 className="text-2xl font-light text-zinc-100 mb-2">{t('personalDetails.title')}</h2>
          <p className="text-sm text-zinc-400">{t('personalDetails.subtitle')}</p>
        </div>
 
        <Form {...form}>
          <div className="space-y-4">
            {/* First Name and Last Name - Side by Side */}
            <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
              <FormField
                control={form.control}
                name="firstName"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel className="text-zinc-300">
                      {t('personalDetails.fields.firstName')}
                    </FormLabel>
                    <FormControl>
                      <Input
                        {...field}
                        data-testid="registration-first-name-input"
                        className="bg-zinc-900 border-zinc-800 text-zinc-100"
                        placeholder={t('personalDetails.placeholders.firstName')}
                      />
                    </FormControl>
                    <FormMessage
                      data-testid="registration-first-name-error"
                      className="text-red-400"
                    />
                  </FormItem>
                )}
              />
 
              <FormField
                control={form.control}
                name="lastName"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel className="text-zinc-300">
                      {t('personalDetails.fields.lastName')}
                    </FormLabel>
                    <FormControl>
                      <Input
                        {...field}
                        data-testid="registration-last-name-input"
                        className="bg-zinc-900 border-zinc-800 text-zinc-100"
                        placeholder={t('personalDetails.placeholders.lastName')}
                      />
                    </FormControl>
                    <FormMessage
                      data-testid="registration-last-name-error"
                      className="text-red-400"
                    />
                  </FormItem>
                )}
              />
            </div>
 
            {/* Email */}
            <FormField
              control={form.control}
              name="email"
              render={({ field }) => (
                <FormItem>
                  <FormLabel className="text-zinc-300">
                    {t('personalDetails.fields.emailAddress')}
                  </FormLabel>
                  <FormControl>
                    <Input
                      {...field}
                      type="email"
                      data-testid="registration-email-input"
                      className="bg-zinc-900 border-zinc-800 text-zinc-100"
                      placeholder={t('personalDetails.placeholders.email')}
                    />
                  </FormControl>
                  <p className="text-xs text-zinc-500">{t('personalDetails.fields.emailHelper')}</p>
                  <FormMessage data-testid="registration-email-error" className="text-red-400" />
                </FormItem>
              )}
            />
 
            {/* Company and Role - Side by Side */}
            <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
              <FormField
                control={form.control}
                name="company"
                render={({ field, fieldState }) => (
                  <FormItem>
                    <FormLabel className="text-zinc-300">
                      {t('personalDetails.fields.company')}
                    </FormLabel>
                    <FormControl>
                      <CompanyAutocomplete
                        value={field.value}
                        onCompanySelect={(selection) => {
                          // Registration stores the display name; the backend resolves
                          // (get-or-create) it to a company on submit.
                          field.onChange(selection?.displayName ?? '');
                        }}
                        error={fieldState.error?.message}
                        placeholder={t('personalDetails.placeholders.company')}
                      />
                    </FormControl>
                    <FormMessage
                      data-testid="registration-company-error"
                      className="text-red-400"
                    />
                  </FormItem>
                )}
              />
 
              <FormField
                control={form.control}
                name="role"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel className="text-zinc-300">
                      {t('personalDetails.fields.role')}
                    </FormLabel>
                    <FormControl>
                      <Input
                        {...field}
                        data-testid="registration-role-input"
                        className="bg-zinc-900 border-zinc-800 text-zinc-100"
                        placeholder={t('personalDetails.placeholders.role')}
                      />
                    </FormControl>
                    <FormMessage data-testid="registration-role-error" className="text-red-400" />
                  </FormItem>
                )}
              />
            </div>
          </div>
        </Form>
      </div>
    );
  }
);
 
PersonalDetailsStep.displayName = 'PersonalDetailsStep';