'use client' import { PlusIcon, Trash2Icon } from 'lucide-react' import { useId, useState } from 'react' import { Button } from '@/components/ui/button' import { Field, FieldDescription, FieldError, FieldLabel } from '@/components/ui/field' import { Input } from '@/components/ui/input' import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select' import { Textarea } from '@/components/ui/textarea' import { MAX_ACTIVITY_DEMAND_DRAFTS } from '@/lib/schemas/activity' import { campaignDemandKindLabels, campaignDemandKinds } from '@/lib/schemas/campaignDemand' type DemandFieldValue = { key: string title: string kind: (typeof campaignDemandKinds)[number] description: string } let demandKeySequence = 0 const nextDemandKey = (): string => { demandKeySequence += 1 return `demand-${demandKeySequence}` } const serializeDemands = (demands: DemandFieldValue[]) => JSON.stringify( demands .filter((demand) => demand.title.trim()) .map((demand) => ({ title: demand.title.trim(), kind: demand.kind, ...(demand.description.trim() ? { description: demand.description.trim() } : {}), })), ) export const ActivityDemandFields = ({ error }: { error?: string }) => { const fieldId = useId() const [demands, setDemands] = useState([]) const addDemand = () => { setDemands((current) => [ ...current, { key: nextDemandKey(), title: '', kind: 'material', description: '' }, ]) } const updateDemand = (key: string, patch: Partial) => { setDemands((current) => current.map((demand) => (demand.key === key ? { ...demand, ...patch } : demand)), ) } const removeDemand = (key: string) => { setDemands((current) => current.filter((demand) => demand.key !== key)) } return ( Demandas vinculadas Registre necessidades que surgem desta atividade. Município e atividade serão vinculados automaticamente.
{demands.map((demand, index) => (

Demanda {index + 1}

updateDemand(demand.key, { title: event.target.value })} required /> updateDemand(demand.key, { kind: event.target.value as DemandFieldValue['kind'], }) } className="min-h-11 w-full" > {campaignDemandKinds.map((kind) => ( {campaignDemandKindLabels[kind]} ))}