All files / src/features/vaccines/components VaccineApplicationDetailsFields.tsx

100% Statements 6/6
62.5% Branches 5/8
100% Functions 3/3
100% Lines 6/6

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                                                          37x 37x 37x   37x               37x                                                                                                             37x                                          
import { Controller, type Control, type FieldErrors, type Path, type UseFormRegister } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
 
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Textarea } from '@/components/ui/textarea'
import { DatePickerField } from '@/shared/components/DatePickerField'
import { fieldErrorKey } from '@/shared/utils/zod-error'
 
import type { VaccineApplicationDetailsInput } from '../api/vaccines.schemas'
import { VaccineProofField } from './VaccineProofField'
 
interface VaccineApplicationDetailsFieldsProps<T extends VaccineApplicationDetailsInput> {
  register: UseFormRegister<T>
  control: Control<T>
  errors: FieldErrors<T>
  onProcessingChange?: (busy: boolean) => void
}
 
// Generic over T so the same field set can be reused by ApplyVaccineDialog's
// ApplyVaccineInput and the register wizard's step 2 (both extend
// VaccineApplicationDetailsInput with the exact same field names/types) —
// the `as Path<T>` casts below are safe because of that bound, not a shortcut.
export function VaccineApplicationDetailsFields<T extends VaccineApplicationDetailsInput>({
  register,
  control,
  errors,
  onProcessingChange,
}: VaccineApplicationDetailsFieldsProps<T>) {
  const { t } = useTranslation()
  const dateErrorKey = fieldErrorKey(errors.applicationDate as never)
  const photoUrlErrorKey = fieldErrorKey(errors.photoUrl as never)
 
  return (
    <div className="space-y-4">
      <div>
        <Label htmlFor="applicationDate">{t('vaccines.applicationDetails.dateLabel')}</Label>
        <Controller
          control={control}
          name={'applicationDate' as Path<T>}
          render={({ field }) => (
            <DatePickerField
              id="applicationDate"
              value={field.value as string}
              onValueChange={field.onChange}
              ref={field.ref}
              onBlur={field.onBlur}
              className="mt-2"
              aria-invalid={!!errors.applicationDate}
              aria-describedby={dateErrorKey ? 'applicationDate-error' : undefined}
            />
          )}
        />
        {dateErrorKey && (
          <p id="applicationDate-error" className="text-destructive mt-1 text-sm">
            {t(dateErrorKey)}
          </p>
        )}
      </div>
 
      <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
        <div>
          <Label htmlFor="batchNumber">{t('vaccines.applicationDetails.batchNumberLabel')}</Label>
          <Input
            id="batchNumber"
            className="mt-2"
            placeholder={t('vaccines.applicationDetails.batchNumberPlaceholder')}
            {...register('batchNumber' as Path<T>)}
          />
        </div>
        <div>
          <Label htmlFor="location">{t('vaccines.applicationDetails.locationLabel')}</Label>
          <Input
            id="location"
            className="mt-2"
            placeholder={t('vaccines.applicationDetails.locationPlaceholder')}
            {...register('location' as Path<T>)}
          />
        </div>
      </div>
 
      <div>
        <Label htmlFor="professional">{t('vaccines.applicationDetails.professionalLabel')}</Label>
        <Input
          id="professional"
          className="mt-2"
          placeholder={t('vaccines.applicationDetails.professionalPlaceholder')}
          {...register('professional' as Path<T>)}
        />
      </div>
 
      <div>
        <Controller
          control={control}
          name={'photoUrl' as Path<T>}
          render={({ field }) => (
            <VaccineProofField
              value={(field.value as string) ?? ''}
              onChange={field.onChange}
              onProcessingChange={onProcessingChange}
            />
          )}
        />
        {photoUrlErrorKey && (
          <p id="photoUrl-error" className="text-destructive mt-1 text-sm">
            {t(photoUrlErrorKey)}
          </p>
        )}
      </div>
 
      <div>
        <Label htmlFor="notes">{t('vaccines.applicationDetails.notesLabel')}</Label>
        <Textarea id="notes" rows={3} className="mt-2" {...register('notes' as Path<T>)} />
      </div>
    </div>
  )
}