teqo-backup/src/components/campaign/demand/DemandDescriptionEditor.tsx
Francisco Solla 9467e0f292 fix(campaign): C140 — manual dispatch in 18 forms prevents React 19 form reset on error
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)
2026-08-23 17:54:19 -03:00

125 lines
4.4 KiB
TypeScript

'use client'
import { useRouter } from 'next/navigation'
import { startTransition, useActionState, useState, type FormEvent } from 'react'
import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage'
import { useCampaignFormSuccessToast } from '@/components/campaign/shared/useCampaignFormSuccessToast'
import { Button } from '@/components/ui/button'
import { Field, FieldError, FieldLabel } from '@/components/ui/field'
import { Spinner } from '@/components/ui/Spinner'
import { Textarea } from '@/components/ui/textarea'
import {
CAMPAIGN_DEMAND_BODY_LABEL,
CAMPAIGN_DEMAND_BODY_MAX_LENGTH,
} from '@/lib/schemas/campaignDemand'
import type { CampaignFormActionState } from '@/utilities/campaignFormActionError'
type DemandDescriptionEditorProps = {
demandId: number
initialDescription: string
formAction: (
state: CampaignFormActionState,
formData: FormData,
) => Promise<CampaignFormActionState>
/** C142 — read-only presentation (advisor with Edição `somente_leitura`): the description renders with no edit affordance. */
readOnly?: boolean
}
/**
* Inline edit of the single demand free-text field (B195): saving re-derives
* the AI title server-side (previous title kept on AI failure) and the
* canonical slug never changes — the page refreshes to the new title.
*/
export const DemandDescriptionEditor = ({
demandId,
initialDescription,
formAction,
readOnly = false,
}: DemandDescriptionEditorProps) => {
const router = useRouter()
const [editing, setEditing] = useState(false)
const [state, submitAction, isPending] = useActionState(formAction, {})
useCampaignFormSuccessToast(state, () => {
setEditing(false)
router.refresh()
})
if (!editing) {
return (
<section aria-label="Descrição da demanda" className="rounded-xl border p-4">
<div className="flex items-start justify-between gap-2">
<p className="whitespace-pre-wrap text-sm">{initialDescription}</p>
{readOnly ? null : (
<Button type="button" variant="ghost" onClick={() => setEditing(true)}>
{initialDescription ? 'Editar' : 'Adicionar descrição'}
</Button>
)}
</div>
</section>
)
}
// C140 — manual dispatch (no `action={submitAction}`): React 19 resets
// uncontrolled fields after any settled form action, wiping typed values
// on a validation error — the editor stays open, so the wipe showed.
const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault()
startTransition(() => submitAction(new FormData(event.currentTarget)))
}
return (
<section aria-label="Editar descrição da demanda" className="rounded-xl border p-4">
<form
onSubmit={handleSubmit}
className="flex flex-col gap-4"
aria-busy={isPending || undefined}
data-pending={isPending ? '' : undefined}
>
<input type="hidden" name="demandId" value={demandId} />
<Field>
<FieldLabel htmlFor="demand-description-edit">{CAMPAIGN_DEMAND_BODY_LABEL}</FieldLabel>
<Textarea
id="demand-description-edit"
name="description"
defaultValue={initialDescription}
rows={5}
minLength={2}
maxLength={CAMPAIGN_DEMAND_BODY_MAX_LENGTH}
required
disabled={isPending}
className="min-h-28"
/>
{state.fieldErrors?.description ? (
<FieldError>{state.fieldErrors.description}</FieldError>
) : null}
</Field>
{state.status !== 'success' ? <CampaignFormActionMessage state={state} /> : null}
<div className="flex justify-end gap-2">
<Button
type="button"
variant="outline"
disabled={isPending}
onClick={() => setEditing(false)}
>
Cancelar
</Button>
<Button type="submit" disabled={isPending} className="min-h-11">
{isPending ? (
<>
<Spinner data-icon="inline-start" aria-hidden="true" />
Salvando…
</>
) : (
'Salvar'
)}
</Button>
</div>
<div aria-live="polite" className="sr-only">
{isPending ? 'Salvando descrição.' : null}
</div>
</form>
</section>
)
}