Files
ims/components/capa/close-capa-button.tsx
T

50 lines
1.5 KiB
TypeScript

'use client'
import { useState } from 'react'
import { useRouter } from 'next/navigation'
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="border border-gray-200 rounded-xl p-5 space-y-3 bg-green-50">
<h3 className="text-sm font-semibold text-gray-800">Close CAPA</h3>
<p className="text-sm text-gray-600">
This CAPA has been verified as effective. Close it to mark it fully resolved.
</p>
{error && <p className="text-sm text-red-600">{error}</p>}
<button
onClick={handleClose}
disabled={loading}
className="w-full bg-green-700 text-white rounded-lg py-2 text-sm font-semibold hover:bg-green-800 disabled:opacity-50"
>
{loading ? 'Closing…' : 'Close CAPA'}
</button>
</div>
)
}