- Contact.phone (text único) → phones (array field Payload, subcampo value): fonte única; principal = phones[0]; beforeValidate normaliza, filtra vazios, rejeita inválido e duplicado DENTRO da ficha (C111 entre fichas preservado) - migration add_contact_phones_array: backfill phone → phones[0] (gen_random_uuid) antes do drop da coluna + índice em value (hand-edit documentado; snapshot .json gerado mantém as colunas C99/C100 já existentes) - primaryPhoneOf / phoneValuesOf / reorderWithPrimaryPhone em src/lib/phone.ts (ordem = prioridade; inline edit = seta o principal preservando o resto) - findOrCreateContactByPhone aceita phones[] (dedupe pelo primário; reuse faz append dos faltantes; locks em todos os números digitados) - sync conta→ficha (CampaignUser): phone da conta faz APPEND quando a ficha não tem (nunca reordena nem remove); tombstone LGPD substitui TODOS os telefones - convite redemption: telefone digitado vira principal, demais preservados - import CSV: preview sinaliza compartilhado também por número secundário; bulk insere contact_phones (JOIN nas leituras, uuid no insert) - busca por telefone (apoiadores, pickers, combobox) casa qualquer número - UI: PhonesFieldEditor compartilhado (repetível mascarado, remover, ↑/↓, modo inline com Salvar) nas 5 superfícies — liderança (form/ficha/lista), apoiador (form/ficha), dobradinha (ficha), assessor (ficha), wizard - UI pública (petição/WhatsApp) intocada: zod e forms ficam com phone; as ações mapeiam phone → phones[0] - testes: spec novo campaignMultiPhones (aceite completo), specs C111/C99 atualizados, e2e wizard/apoiador ajustados
156 lines
5.3 KiB
TypeScript
156 lines
5.3 KiB
TypeScript
'use client'
|
|
|
|
import { useActionState, useEffect, useRef } from 'react'
|
|
|
|
import {
|
|
createLeadershipWizardFormAction,
|
|
updateLeadershipWizardFormAction,
|
|
} from '@/app/(campaign)/campanha/(app)/acoes/wizardLeadershipFormActions'
|
|
import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage'
|
|
import { PhonesFieldEditor } from '@/components/campaign/shared/PhonesFieldEditor'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Checkbox } from '@/components/ui/Checkbox'
|
|
import { Field, FieldError, FieldLabel } from '@/components/ui/field'
|
|
import { Input } from '@/components/ui/input'
|
|
import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select'
|
|
import { Spinner } from '@/components/ui/Spinner'
|
|
import { Textarea } from '@/components/ui/textarea'
|
|
import { leadershipSupportStatuses } from '@/lib/schemas/leadership'
|
|
import type { WizardLeadershipTileViewModel } from '@/lib/wizardLeadershipContract'
|
|
import { fieldError } from '@/utilities/campaignFormFields'
|
|
import { supportStatusLabels } from '@/utilities/leadership/leadershipLabels'
|
|
|
|
type WizardLeadershipFormProps = {
|
|
municipalityId: number
|
|
municipalitySlug: string
|
|
leadership: WizardLeadershipTileViewModel | null
|
|
onSaved: () => void
|
|
onCancel: () => void
|
|
}
|
|
|
|
export const WizardLeadershipForm = ({
|
|
municipalityId,
|
|
municipalitySlug,
|
|
leadership,
|
|
onSaved,
|
|
onCancel,
|
|
}: WizardLeadershipFormProps) => {
|
|
const isCreate = leadership === null
|
|
const formAction = isCreate ? createLeadershipWizardFormAction : updateLeadershipWizardFormAction
|
|
const [state, submitAction, isPending] = useActionState(formAction, {})
|
|
const nameRef = useRef<HTMLInputElement>(null)
|
|
const savedRef = useRef(false)
|
|
|
|
useEffect(() => {
|
|
nameRef.current?.focus()
|
|
}, [leadership?.id])
|
|
|
|
useEffect(() => {
|
|
if (state.status === 'success' && !savedRef.current) {
|
|
savedRef.current = true
|
|
onSaved()
|
|
}
|
|
if (state.status !== 'success') {
|
|
savedRef.current = false
|
|
}
|
|
}, [onSaved, state.status])
|
|
|
|
return (
|
|
<form action={submitAction} className="flex flex-col gap-4" aria-busy={isPending}>
|
|
<input type="hidden" name="municipalityId" value={municipalityId} />
|
|
<input type="hidden" name="municipalitySlug" value={municipalitySlug} />
|
|
{!isCreate ? <input type="hidden" name="leadershipId" value={leadership.id} /> : null}
|
|
|
|
<Field>
|
|
<FieldLabel htmlFor="wizard-leadership-name">Nome</FieldLabel>
|
|
<Input
|
|
ref={nameRef}
|
|
id="wizard-leadership-name"
|
|
name="name"
|
|
minLength={2}
|
|
maxLength={120}
|
|
defaultValue={leadership?.name ?? ''}
|
|
className="min-h-11"
|
|
/>
|
|
{fieldError(state.fieldErrors, 'name') ? (
|
|
<FieldError>{fieldError(state.fieldErrors, 'name')}</FieldError>
|
|
) : null}
|
|
</Field>
|
|
|
|
<div className="grid gap-4 sm:grid-cols-2">
|
|
<PhonesFieldEditor
|
|
name="phones"
|
|
minRows={1}
|
|
label="Celulares (o primeiro é o principal)"
|
|
defaultValues={leadership?.phones ?? []}
|
|
error={fieldError(state.fieldErrors, 'phones')}
|
|
/>
|
|
<Field>
|
|
<FieldLabel htmlFor="wizard-leadership-email">E-mail</FieldLabel>
|
|
<Input
|
|
id="wizard-leadership-email"
|
|
name="email"
|
|
type="email"
|
|
defaultValue={leadership?.email ?? ''}
|
|
className="min-h-11"
|
|
/>
|
|
{fieldError(state.fieldErrors, 'email') ? (
|
|
<FieldError>{fieldError(state.fieldErrors, 'email')}</FieldError>
|
|
) : null}
|
|
</Field>
|
|
</div>
|
|
|
|
<Field>
|
|
<FieldLabel htmlFor="wizard-leadership-status">Status de apoio</FieldLabel>
|
|
<NativeSelect
|
|
id="wizard-leadership-status"
|
|
name="supportStatus"
|
|
defaultValue={leadership?.supportStatus ?? 'a_abordar'}
|
|
className="min-h-11 w-full"
|
|
>
|
|
{leadershipSupportStatuses.map((status) => (
|
|
<NativeSelectOption key={status} value={status}>
|
|
{supportStatusLabels[status]}
|
|
</NativeSelectOption>
|
|
))}
|
|
</NativeSelect>
|
|
</Field>
|
|
|
|
<Field>
|
|
<FieldLabel htmlFor="wizard-leadership-notes">Observação</FieldLabel>
|
|
<Textarea
|
|
id="wizard-leadership-notes"
|
|
name="notes"
|
|
rows={3}
|
|
maxLength={3000}
|
|
defaultValue={leadership?.notes ?? ''}
|
|
/>
|
|
</Field>
|
|
|
|
<Field orientation="horizontal" className="items-center gap-2">
|
|
<input type="hidden" name="exclusive" value="false" />
|
|
<Checkbox
|
|
id="wizard-leadership-exclusive"
|
|
name="exclusive"
|
|
value="true"
|
|
defaultChecked={leadership?.exclusive ?? true}
|
|
/>
|
|
<FieldLabel htmlFor="wizard-leadership-exclusive" className="font-normal">
|
|
Apoio exclusivo
|
|
</FieldLabel>
|
|
</Field>
|
|
|
|
{state.status !== 'success' ? <CampaignFormActionMessage state={state} /> : null}
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
<Button type="button" variant="outline" className="min-h-11" onClick={onCancel}>
|
|
Voltar
|
|
</Button>
|
|
<Button type="submit" disabled={isPending} className="min-h-11">
|
|
{isPending ? <Spinner data-icon="inline-start" aria-hidden="true" /> : null}
|
|
Salvar
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
)
|
|
}
|