'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { Button } from '@/components/ui/button' import { Field, Textarea } from '@/components/ui/field' import { cx } from '@/components/ui/cx' interface Props { capaId: string } type Verdict = 'verified' | 'reopened' function Choice({ selected, onSelect, title, hint, }: { selected: boolean onSelect: () => void title: string hint: string }) { return ( {title} {hint} ) } export function VerifyForm({ capaId }: Props) { const router = useRouter() const [verdict, setVerdict] = useState(null) const [reopenReason, setReopenReason] = useState('') const [saving, setSaving] = useState(false) const [error, setError] = useState(null) async function handleSubmit(e: React.FormEvent) { e.preventDefault() if (!verdict) return if (verdict === 'reopened' && !reopenReason.trim()) { setError('Reopen reason is required') return } setSaving(true) setError(null) const res = await fetch(`/api/capa/${capaId}/verify`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ verdict, reopen_reason: reopenReason || null }), }) if (!res.ok) { const data = await res.json() setError(data.error ?? 'Verification failed') setSaving(false) return } router.push('/hse/capa') router.refresh() } return ( Verify this action Confirm the corrective action fixed the root cause, or send it back. setVerdict('verified')} title="Verified, effective" hint="Root cause addressed. The action is marked verified." /> setVerdict('reopened')} title="Reopen, ineffective" hint="Returns to the owner with a required reason." /> {verdict === 'reopened' && ( setReopenReason(e.target.value)} rows={2} required placeholder="Describe why the corrective action did not resolve the root cause…" /> )} {error && ( {error} )} {verdict && ( {saving ? 'Saving…' : verdict === 'verified' ? 'Confirm verification' : 'Reopen action'} )} ) }
Confirm the corrective action fixed the root cause, or send it back.
{error}