'use client' import { SubmitButton } from '@/components/submit-button' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from '@/components/ui/card' import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form' import { HoverCard, HoverCardContent, HoverCardTrigger, } from '@/components/ui/hover-card' import { Input } from '@/components/ui/input' import { getGroup } from '@/lib/api' import { GroupFormValues, groupFormSchema } from '@/lib/schemas' import { zodResolver } from '@hookform/resolvers/zod' import { Save, Trash2 } from 'lucide-react' import { useFieldArray, useForm } from 'react-hook-form' export type Props = { group?: NonNullable>> onSubmit: (groupFormValues: GroupFormValues) => Promise protectedParticipantIds?: string[] } export function GroupForm({ group, onSubmit, protectedParticipantIds = [], }: Props) { const form = useForm({ resolver: zodResolver(groupFormSchema), defaultValues: group ? { name: group.name, currency: group.currency, participants: group.participants, } : { name: '', currency: '', participants: [{ name: 'John' }, { name: 'Jane' }, { name: 'Jack' }], }, }) const { fields, append, remove } = useFieldArray({ control: form.control, name: 'participants', keyName: 'key', }) return (
{ await onSubmit(values) })} > Group information ( Group name Enter a name for your group. )} /> ( Currency symbol We’ll use it to display amounts. )} /> Participants Enter the name for each participant
    {fields.map((item, index) => (
  • ( Participant #{index + 1}
    {item.id && protectedParticipantIds.includes(item.id) ? ( This participant is part of expenses, and can not be removed. ) : ( )}
    )} />
  • ))}
{group ? <>Save : <> Create}
) }