135 lines
4.0 KiB
TypeScript
135 lines
4.0 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Field, Input, Select, Textarea } from '@/components/ui/field'
|
|
|
|
interface User {
|
|
id: string
|
|
name: string
|
|
department: string
|
|
}
|
|
|
|
interface Props {
|
|
incidentId: string
|
|
}
|
|
|
|
export function CapaForm({ incidentId }: Props) {
|
|
const router = useRouter()
|
|
const [users, setUsers] = useState<User[]>([])
|
|
const [description, setDescription] = useState('')
|
|
const [ownerId, setOwnerId] = useState('')
|
|
const [department, setDepartment] = useState('')
|
|
const [dueDate, setDueDate] = useState('')
|
|
const [priority, setPriority] = useState<'low' | 'med' | 'high'>('med')
|
|
const [rootCauseRef, setRootCauseRef] = useState('')
|
|
const [saving, setSaving] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
useEffect(() => {
|
|
fetch('/api/users')
|
|
.then(r => r.json())
|
|
.then(data => { if (Array.isArray(data)) setUsers(data) })
|
|
.catch(() => {})
|
|
}, [])
|
|
|
|
function handleOwnerChange(id: string) {
|
|
setOwnerId(id)
|
|
const u = users.find(u => u.id === id)
|
|
if (u) setDepartment(u.department)
|
|
}
|
|
|
|
async function handleSubmit(e: React.FormEvent) {
|
|
e.preventDefault()
|
|
if (!ownerId || !description || !dueDate) {
|
|
setError('Description, owner, and due date are required')
|
|
return
|
|
}
|
|
setSaving(true)
|
|
setError(null)
|
|
const res = await fetch('/api/capa', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
incident_id: incidentId,
|
|
description,
|
|
owner_user_id: ownerId,
|
|
department,
|
|
due_date: dueDate,
|
|
priority,
|
|
root_cause_ref: rootCauseRef || null,
|
|
}),
|
|
})
|
|
if (!res.ok) {
|
|
const data = await res.json()
|
|
setError(data.error ?? 'Create failed')
|
|
setSaving(false)
|
|
return
|
|
}
|
|
router.push(`/hse/incidents/${incidentId}`)
|
|
router.refresh()
|
|
}
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<Field label="Action Description *" htmlFor="capa-description">
|
|
<Textarea
|
|
id="capa-description"
|
|
value={description}
|
|
onChange={e => setDescription(e.target.value)}
|
|
rows={3}
|
|
required
|
|
placeholder="What corrective action will be taken?"
|
|
/>
|
|
</Field>
|
|
|
|
<Field label="Root Cause Reference" htmlFor="capa-root-cause">
|
|
<Input
|
|
id="capa-root-cause"
|
|
type="text"
|
|
value={rootCauseRef}
|
|
onChange={e => setRootCauseRef(e.target.value)}
|
|
placeholder="e.g. Why #3 — inadequate training"
|
|
/>
|
|
</Field>
|
|
|
|
<Field label="Assigned To *" htmlFor="capa-owner">
|
|
<Select id="capa-owner" value={ownerId} onChange={e => handleOwnerChange(e.target.value)} required>
|
|
<option value="">Select owner…</option>
|
|
{users.map(u => (
|
|
<option key={u.id} value={u.id}>{u.name} ({u.department})</option>
|
|
))}
|
|
</Select>
|
|
</Field>
|
|
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<Field label="Due Date *" htmlFor="capa-due">
|
|
<Input id="capa-due" type="date" value={dueDate} onChange={e => setDueDate(e.target.value)} required />
|
|
</Field>
|
|
<Field label="Priority" htmlFor="capa-priority">
|
|
<Select
|
|
id="capa-priority"
|
|
value={priority}
|
|
onChange={e => setPriority(e.target.value as 'low' | 'med' | 'high')}
|
|
>
|
|
<option value="low">Low</option>
|
|
<option value="med">Medium</option>
|
|
<option value="high">High</option>
|
|
</Select>
|
|
</Field>
|
|
</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 type="submit" block disabled={saving}>
|
|
{saving ? 'Creating…' : 'Create CAPA Action'}
|
|
</Button>
|
|
</form>
|
|
)
|
|
}
|