53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
import { Button } from '@/components/ui/button'
|
|
|
|
interface Props {
|
|
capaId: string
|
|
}
|
|
|
|
export function CloseCapaButton({ capaId }: Props) {
|
|
const router = useRouter()
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
async function handleClose() {
|
|
if (!confirm('Close this CAPA? This marks it as fully resolved.')) return
|
|
setLoading(true)
|
|
setError(null)
|
|
const res = await fetch(`/api/capa/${capaId}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ status: 'closed' }),
|
|
})
|
|
if (!res.ok) {
|
|
setError('Failed to close CAPA. Please try again.')
|
|
setLoading(false)
|
|
return
|
|
}
|
|
router.push('/hse/capa')
|
|
router.refresh()
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-3 rounded-xl bg-surface p-5 shadow-card">
|
|
<div>
|
|
<h3 className="text-sm font-semibold">Close CAPA</h3>
|
|
<p className="mt-0.5 text-xs text-muted">
|
|
This CAPA has been verified as effective. Close it to mark it fully resolved.
|
|
</p>
|
|
</div>
|
|
{error && (
|
|
<p role="alert" className="rounded-md bg-st-red-bg px-3 py-2 text-[13px] text-st-red-fg">
|
|
{error}
|
|
</p>
|
|
)}
|
|
<Button onClick={handleClose} disabled={loading}>
|
|
{loading ? 'Closing…' : 'Close CAPA'}
|
|
</Button>
|
|
</div>
|
|
)
|
|
}
|