import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import type { BillingType, CreateFirstGroup, WeekDay } from '@gruperly/shared' import { CreateFirstGroupSchema } from '@gruperly/shared' import { ArrowLeft, Check, Loader2 } from 'lucide-react' import { cn } from '../../lib/utils' import { Button, Input, Label } from '../../components/ui' import { BILLING_TYPES, WEEK_DAY_CHIPS } from '../onboarding/constants' const defaultValues: CreateFirstGroup = { name: '', days: [], time: '09:00', capacity: 1, price: 0, billingType: 'MONTHLY', dueDay: 1, } type GroupFormProps = { heading: string description: string submitLabel: string isSubmitting: boolean errorMessage?: string | null onSubmit: (values: CreateFirstGroup) => void onBack?: () => void } export function GroupForm({ heading, description, submitLabel, isSubmitting, errorMessage, onSubmit, onBack, }: GroupFormProps) { const { register, handleSubmit, watch, setValue, formState: { errors }, } = useForm({ resolver: zodResolver(CreateFirstGroupSchema), defaultValues, mode: 'onTouched', }) const days = watch('days') const billingType = watch('billingType') const dueDay = watch('dueDay') const toggleDay = (day: WeekDay) => { const next = days.includes(day) ? days.filter((d) => d !== day) : [...days, day] setValue('days', next, { shouldValidate: true }) } const selectBillingType = (type: BillingType) => { setValue('billingType', type, { shouldValidate: true }) } return (

{heading}

{description}

{errors.name ?

{errors.name.message}

: null}
{WEEK_DAY_CHIPS.map(({ value, label }) => { const isActive = days.includes(value) return ( ) })}
{errors.days ?

{errors.days.message}

: null}
{errors.time ?

{errors.time.message}

: null}
{errors.capacity ? (

{errors.capacity.message}

) : null}
$
{errors.price ? (

{errors.price.message}

) : null}
{BILLING_TYPES.map(({ value, label, hint }) => { const isActive = billingType === value return ( ) })}

Los cobros vencerán el día {isFinite(dueDay) && dueDay ? dueDay : '1'} de cada mes.

{errors.dueDay ? (

{errors.dueDay.message}

) : null}
{errorMessage ? (

{errorMessage}

) : null}
{onBack ? ( ) : null}
) }