Fix manual dispatch em 18 formulários de /campanha que usavam <form action={submitAction}>: campos não controlados voltavam ao defaultValue após qualquer action assentada (sucesso ou erro) do React 19.
- WizardStepFormChrome alterado uma vez (cobre 3 wizard steps)
- 15 forms recebem dispatch manual arquivo a arquivo
- AdvisorPasswordResetButton (2 micro-forms sem campo digitável) ficou como está
- Unit spec novo campaignWizardStepFormChrome.unit.spec.tsx
- CHANGELOG-AGENTS.md cleaned (98 duplicates removed)
Also fixes:
- campaignPageActor writable gate only blocks advisors (was blocking coordinators too)
- campaignLeaderships e2e stability (waitForLoadState before attribute assertion)
119 lines
4 KiB
TypeScript
119 lines
4 KiB
TypeScript
'use client'
|
|
|
|
import { startTransition, useActionState, type FormEvent } from 'react'
|
|
|
|
import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage'
|
|
import {
|
|
RelationMultiSelect,
|
|
type RelationOption,
|
|
} from '@/components/campaign/shared/RelationMultiSelect'
|
|
import { Button } from '@/components/ui/button'
|
|
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 { organizationKindLabels, organizationKinds } from '@/lib/schemas/organization'
|
|
import type { CampaignFormActionState } from '@/utilities/campaignFormActionError'
|
|
import { fieldError } from '@/utilities/campaignFormFields'
|
|
|
|
type OrganizationFormProps = {
|
|
municipalityOptions: RelationOption[]
|
|
formAction: (
|
|
state: CampaignFormActionState,
|
|
formData: FormData,
|
|
) => Promise<CampaignFormActionState>
|
|
initial?: {
|
|
id: number
|
|
kind: string
|
|
notes: string | null
|
|
municipalityIDs: number[]
|
|
}
|
|
initialName?: string
|
|
}
|
|
|
|
export const OrganizationForm = ({
|
|
municipalityOptions,
|
|
formAction,
|
|
initial,
|
|
initialName,
|
|
}: OrganizationFormProps) => {
|
|
const [state, submitAction, isPending] = useActionState(formAction, {})
|
|
const isEdit = Boolean(initial)
|
|
|
|
// C140 — manual dispatch (no `action={submitAction}`): React 19 resets
|
|
// uncontrolled fields after any settled form action, wiping typed values
|
|
// on a validation error — the page stays visible, so the wipe showed.
|
|
const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
|
|
event.preventDefault()
|
|
startTransition(() => submitAction(new FormData(event.currentTarget)))
|
|
}
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit} className="flex max-w-2xl flex-col gap-4">
|
|
{initial ? <input type="hidden" name="organizationId" value={initial.id} /> : null}
|
|
{isEdit ? (
|
|
<p className="text-sm text-muted-foreground">
|
|
O nome da organização não pode ser alterado após a criação.
|
|
</p>
|
|
) : (
|
|
<Field>
|
|
<FieldLabel htmlFor="organization-name">Nome</FieldLabel>
|
|
<Input
|
|
id="organization-name"
|
|
name="name"
|
|
required
|
|
minLength={2}
|
|
maxLength={160}
|
|
defaultValue={initialName}
|
|
className="min-h-11"
|
|
/>
|
|
{fieldError(state.fieldErrors, 'name') ? (
|
|
<FieldError>{fieldError(state.fieldErrors, 'name')}</FieldError>
|
|
) : null}
|
|
</Field>
|
|
)}
|
|
<Field>
|
|
<FieldLabel htmlFor="organization-kind">Tipo</FieldLabel>
|
|
<NativeSelect
|
|
id="organization-kind"
|
|
name="kind"
|
|
defaultValue={initial?.kind ?? 'sindicato'}
|
|
className="min-h-11 w-full sm:w-64"
|
|
>
|
|
{organizationKinds.map((kind) => (
|
|
<NativeSelectOption key={kind} value={kind}>
|
|
{organizationKindLabels[kind]}
|
|
</NativeSelectOption>
|
|
))}
|
|
</NativeSelect>
|
|
</Field>
|
|
|
|
<RelationMultiSelect
|
|
name="municipalities"
|
|
label="Municípios de atuação"
|
|
options={municipalityOptions}
|
|
initialSelectedIDs={initial?.municipalityIDs ?? []}
|
|
error={fieldError(state.fieldErrors, 'municipalities')}
|
|
placeholder="Adicionar município…"
|
|
/>
|
|
|
|
<Field>
|
|
<FieldLabel htmlFor="organization-notes">Observações</FieldLabel>
|
|
<Textarea
|
|
id="organization-notes"
|
|
name="notes"
|
|
rows={4}
|
|
maxLength={4000}
|
|
defaultValue={initial?.notes ?? undefined}
|
|
/>
|
|
</Field>
|
|
|
|
<CampaignFormActionMessage state={state} />
|
|
<Button type="submit" disabled={isPending} className="min-h-11 self-start">
|
|
{isPending ? <Spinner data-icon="inline-start" aria-hidden="true" /> : null}
|
|
{isEdit ? 'Salvar organização' : 'Cadastrar organização'}
|
|
</Button>
|
|
</form>
|
|
)
|
|
}
|