123 lines
3.5 KiB
TypeScript
123 lines
3.5 KiB
TypeScript
'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 (
|
|
<button
|
|
type="button"
|
|
role="radio"
|
|
aria-checked={selected}
|
|
onClick={onSelect}
|
|
className={cx(
|
|
'cursor-pointer rounded-xl p-3 text-left transition-colors',
|
|
selected ? 'bg-accent-tint shadow-chip-set' : 'bg-surface shadow-chip hover:bg-surface-2',
|
|
)}
|
|
>
|
|
<span className="block text-[13px] font-semibold">{title}</span>
|
|
<span className="mt-0.5 block text-xs text-muted">{hint}</span>
|
|
</button>
|
|
)
|
|
}
|
|
|
|
export function VerifyForm({ capaId }: Props) {
|
|
const router = useRouter()
|
|
const [verdict, setVerdict] = useState<Verdict | null>(null)
|
|
const [reopenReason, setReopenReason] = useState('')
|
|
const [saving, setSaving] = useState(false)
|
|
const [error, setError] = useState<string | null>(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 (
|
|
<form onSubmit={handleSubmit} className="space-y-4 rounded-xl bg-surface p-5 shadow-card">
|
|
<div>
|
|
<h3 className="text-sm font-semibold">Verify this action</h3>
|
|
<p className="mt-0.5 text-xs text-muted">Confirm the corrective action fixed the root cause, or send it back.</p>
|
|
</div>
|
|
|
|
<div role="radiogroup" aria-label="Verification verdict" className="grid gap-3 sm:grid-cols-2">
|
|
<Choice
|
|
selected={verdict === 'verified'}
|
|
onSelect={() => setVerdict('verified')}
|
|
title="Verified, effective"
|
|
hint="Root cause addressed. The action is marked verified."
|
|
/>
|
|
<Choice
|
|
selected={verdict === 'reopened'}
|
|
onSelect={() => setVerdict('reopened')}
|
|
title="Reopen, ineffective"
|
|
hint="Returns to the owner with a required reason."
|
|
/>
|
|
</div>
|
|
|
|
{verdict === 'reopened' && (
|
|
<Field label="Why is the action ineffective? *" htmlFor="reopen-reason">
|
|
<Textarea
|
|
id="reopen-reason"
|
|
value={reopenReason}
|
|
onChange={e => setReopenReason(e.target.value)}
|
|
rows={2}
|
|
required
|
|
placeholder="Describe why the corrective action did not resolve the root cause…"
|
|
/>
|
|
</Field>
|
|
)}
|
|
|
|
{error && (
|
|
<p role="alert" className="rounded-md bg-st-red-bg px-3 py-2 text-[13px] text-st-red-fg">
|
|
{error}
|
|
</p>
|
|
)}
|
|
|
|
{verdict && (
|
|
<Button type="submit" disabled={saving}>
|
|
{saving ? 'Saving…' : verdict === 'verified' ? 'Confirm verification' : 'Reopen action'}
|
|
</Button>
|
|
)}
|
|
</form>
|
|
)
|
|
}
|