All files / src/features/appointments/components AppointmentScheduleFields.tsx

100% Statements 6/6
80% Branches 8/10
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                                                    30x 30x 30x   30x                               26x                                                                                 30x                                                                                                
import { Controller, type Control, type FieldErrors, type UseFormRegister } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
 
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { DatePickerField } from '@/shared/components/DatePickerField'
import { SelectorCardGroup } from '@/shared/components/SelectorCardGroup'
import { CalendarIcon } from '@/shared/icons/calendar-icon'
import { CheckIcon } from '@/shared/icons/check-icon'
import { fieldErrorKey } from '@/shared/utils/zod-error'
 
import type { AppointmentFormInput } from '../api/appointments.schemas'
 
interface AppointmentScheduleFieldsProps {
  register: UseFormRegister<AppointmentFormInput>
  control: Control<AppointmentFormInput>
  errors: FieldErrors<AppointmentFormInput>
  hideIntent?: boolean
}
 
export function AppointmentScheduleFields({
  register,
  control,
  errors,
  hideIntent,
}: AppointmentScheduleFieldsProps) {
  const { t } = useTranslation()
  const dateErrorKey = fieldErrorKey(errors.date)
  const timeErrorKey = fieldErrorKey(errors.time)
 
  return (
    <div className="space-y-6">
      {/* Which of the two acts, asked here because this is where time acquires
          meaning — the same fields mean "book it" or "log it" depending only on
          this answer, and the validation flips direction with it.
 
          Asked rather than inferred. Reading a past date as "they must have
          meant to record it" would silently reinterpret the exact typo the
          scheduling rule exists to catch. */}
      {!hideIntent && (
        <div>
          <Label id="appointment-intent-label">{t('appointments.form.intentLabel')}</Label>
          <Controller
            control={control}
            name="status"
            render={({ field }) => (
              <SelectorCardGroup
                name="appointment-intent"
                value={field.value}
                onValueChange={field.onChange}
                layout="vertical"
                className="mt-2 [&>label]:p-3"
                options={[
                  {
                    value: 'SCHEDULED',
                    label: t('appointments.form.intentScheduled'),
                    description: t('appointments.form.intentScheduledHint'),
                    media: (
                      <span className="flex h-11 w-11 flex-shrink-0 items-center justify-center rounded-xl bg-violet-50 dark:bg-violet-950/40 text-violet-600 dark:text-violet-300">
                        <CalendarIcon className="h-5 w-5" />
                      </span>
                    ),
                  },
                  {
                    value: 'COMPLETED',
                    label: t('appointments.form.intentCompleted'),
                    description: t('appointments.form.intentCompletedHint'),
                    media: (
                      <span className="flex h-11 w-11 flex-shrink-0 items-center justify-center rounded-xl bg-emerald-50 dark:bg-emerald-950/40 text-emerald-700 dark:text-emerald-300">
                        <CheckIcon className="h-5 w-5" />
                      </span>
                    ),
                  },
                ]}
              />
            )}
          />
        </div>
      )}
 
      <div className="grid grid-cols-1 gap-6 md:grid-cols-2">
        <div>
          <Label htmlFor="date">{t('appointments.form.dateLabel')}</Label>
          <Controller
            control={control}
            name="date"
            render={({ field }) => (
              <DatePickerField
                id="date"
                value={field.value}
                onValueChange={field.onChange}
                ref={field.ref}
                onBlur={field.onBlur}
                aria-invalid={!!errors.date}
                aria-describedby={dateErrorKey ? 'date-error' : undefined}
                className="mt-2"
              />
            )}
          />
          {dateErrorKey && (
            <p id="date-error" className="text-destructive mt-1 text-sm">
              {t(dateErrorKey)}
            </p>
          )}
        </div>
        <div>
          <Label htmlFor="time">{t('appointments.form.timeLabel')}</Label>
          <Input
            id="time"
            type="time"
            aria-invalid={!!errors.time}
            aria-describedby={timeErrorKey ? 'time-error' : undefined}
            className="mt-2"
            {...register('time')}
          />
          {timeErrorKey && (
            <p id="time-error" className="text-destructive mt-1 text-sm">
              {t(timeErrorKey)}
            </p>
          )}
        </div>
      </div>
 
      <div>
        <Label htmlFor="reason">{t('appointments.form.reasonLabel')}</Label>
        <Input
          id="reason"
          placeholder={t('appointments.form.reasonPlaceholder')}
          className="mt-2"
          {...register('reason')}
        />
      </div>
    </div>
  )
}