feat(capa): proof form for owners and photo gallery for HSE
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,8 +5,8 @@ import Link from 'next/link'
|
||||
import { ArrowLeft } from 'lucide-react'
|
||||
import { asAdmin } from '@/lib/db/with-user'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { capaActions, incidents, users } from '@/lib/db/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { capaActions, evidenceFiles, incidents, users } from '@/lib/db/schema'
|
||||
import { eq, and, asc } from 'drizzle-orm'
|
||||
import { aliasedTable } from 'drizzle-orm'
|
||||
import { VerifyForm } from '@/components/capa/verify-form'
|
||||
import { CloseCapaButton } from '@/components/capa/close-capa-button'
|
||||
@@ -60,6 +60,13 @@ export default async function CapaDetailPage({ params }: Props) {
|
||||
|
||||
if (!capa) notFound()
|
||||
|
||||
const photos = await asAdmin(db =>
|
||||
db.select({ id: evidenceFiles.id, fileUrl: evidenceFiles.fileUrl, fileType: evidenceFiles.fileType })
|
||||
.from(evidenceFiles)
|
||||
.where(and(eq(evidenceFiles.capaId, id), eq(evidenceFiles.deleted, false)))
|
||||
.orderBy(asc(evidenceFiles.uploadedAt))
|
||||
)
|
||||
|
||||
const isHse = session && ['hse', 'admin'].includes(session.role)
|
||||
const status = capa.status
|
||||
const today = new Date().toISOString().slice(0, 10)
|
||||
@@ -102,7 +109,7 @@ export default async function CapaDetailPage({ params }: Props) {
|
||||
{isHse && status === 'pending_verification' && <VerifyForm capaId={id} />}
|
||||
{isHse && status === 'verified' && <CloseCapaButton capaId={id} />}
|
||||
|
||||
{(capa.ownerNotes || capa.completedAt) && (
|
||||
{(capa.ownerNotes || capa.completedAt || photos.length > 0) && (
|
||||
<section className="space-y-2 rounded-xl bg-surface p-5 shadow-card">
|
||||
<h2 className="text-sm font-semibold">Completion report</h2>
|
||||
{capa.completedAt && (
|
||||
@@ -113,6 +120,18 @@ export default async function CapaDetailPage({ params }: Props) {
|
||||
{capa.ownerNotes && (
|
||||
<p className="whitespace-pre-wrap text-[13px] text-ink-3">{capa.ownerNotes}</p>
|
||||
)}
|
||||
{photos.length > 0 && (
|
||||
<ul className="grid grid-cols-2 gap-2 sm:grid-cols-3">
|
||||
{photos.map(p => (
|
||||
<li key={p.id}>
|
||||
<a href={p.fileUrl} target="_blank" rel="noreferrer" className="block overflow-hidden rounded-md shadow-card">
|
||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||
<img src={p.fileUrl} alt="CAPA completion evidence" className="aspect-square w-full object-cover" />
|
||||
</a>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { SubmitVerificationForm } from '@/components/capa/submit-verification-form'
|
||||
|
||||
interface Props {
|
||||
capaId: string
|
||||
@@ -41,11 +42,7 @@ export function CapaOwnerActions({ capaId, currentStatus }: Props) {
|
||||
}
|
||||
|
||||
if (currentStatus === 'in_progress') {
|
||||
return (
|
||||
<Button onClick={() => updateStatus('pending_verification')} disabled={loading}>
|
||||
{loading ? '…' : 'Submit for Verification'}
|
||||
</Button>
|
||||
)
|
||||
return <SubmitVerificationForm capaId={capaId} />
|
||||
}
|
||||
|
||||
return null
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { X } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Field, Textarea } from '@/components/ui/field'
|
||||
import { MAX_CAPA_PHOTOS, validateCapaSubmission } from '@/lib/capa/submit-validation'
|
||||
|
||||
interface Props {
|
||||
capaId: string
|
||||
}
|
||||
|
||||
export function SubmitVerificationForm({ capaId }: Props) {
|
||||
const router = useRouter()
|
||||
const [note, setNote] = useState('')
|
||||
const [files, setFiles] = useState<File[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
const check = validateCapaSubmission(note, files)
|
||||
|
||||
function addFiles(list: FileList | null) {
|
||||
if (!list) return
|
||||
setFiles(prev => [...prev, ...Array.from(list)].slice(0, MAX_CAPA_PHOTOS))
|
||||
}
|
||||
|
||||
async function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault()
|
||||
if (!check.ok) return
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
try {
|
||||
const fd = new FormData()
|
||||
fd.append('note', check.note)
|
||||
files.forEach(f => fd.append('files', f))
|
||||
const res = await fetch(`/api/capa/${capaId}/submit`, { method: 'POST', body: fd })
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}))
|
||||
setError(Array.isArray(data.details) ? data.details.join('. ') : (data.error ?? 'Submit failed. Please try again.'))
|
||||
return
|
||||
}
|
||||
router.refresh()
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-3 rounded-xl bg-surface p-5 shadow-card">
|
||||
<div>
|
||||
<h2 className="text-sm font-semibold">Proof of completion</h2>
|
||||
<p className="text-xs text-muted">Describe what was done and attach 1 to {MAX_CAPA_PHOTOS} photos as evidence.</p>
|
||||
</div>
|
||||
|
||||
<Field label="Completion note" htmlFor={`note-${capaId}`}>
|
||||
<Textarea
|
||||
id={`note-${capaId}`}
|
||||
value={note}
|
||||
onChange={e => setNote(e.target.value)}
|
||||
rows={3}
|
||||
placeholder="What action was carried out?"
|
||||
disabled={loading}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="Photos" htmlFor={`photos-${capaId}`}>
|
||||
<input
|
||||
id={`photos-${capaId}`}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
multiple
|
||||
disabled={loading || files.length >= MAX_CAPA_PHOTOS}
|
||||
onChange={e => { addFiles(e.target.files); e.target.value = '' }}
|
||||
className="block w-full text-[13px] text-ink"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
{files.length > 0 && (
|
||||
<ul className="space-y-1">
|
||||
{files.map((f, i) => (
|
||||
<li key={`${f.name}-${i}`} className="flex items-center justify-between gap-2 text-[13px]">
|
||||
<span className="truncate">{f.name}</span>
|
||||
<button
|
||||
type="button"
|
||||
aria-label={`Remove ${f.name}`}
|
||||
onClick={() => setFiles(prev => prev.filter((_, j) => j !== i))}
|
||||
disabled={loading}
|
||||
className="text-muted hover:text-ink"
|
||||
>
|
||||
<X size={14} strokeWidth={2} aria-hidden />
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
{error && <p role="alert" className="text-xs text-danger">{error}</p>}
|
||||
|
||||
<Button type="submit" disabled={!check.ok || loading}>
|
||||
{loading ? 'Submitting…' : 'Submit for Verification'}
|
||||
</Button>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { render, screen, fireEvent } from '@testing-library/react'
|
||||
import { SubmitVerificationForm } from '@/components/capa/submit-verification-form'
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ refresh: vi.fn() }),
|
||||
}))
|
||||
|
||||
function button() {
|
||||
return screen.getByRole('button', { name: 'Submit for Verification' }) as HTMLButtonElement
|
||||
}
|
||||
|
||||
describe('SubmitVerificationForm', () => {
|
||||
it('is disabled until a note and a photo are provided', () => {
|
||||
const { container } = render(<SubmitVerificationForm capaId="abc" />)
|
||||
expect(button().disabled).toBe(true)
|
||||
|
||||
fireEvent.change(screen.getByLabelText('Completion note'), { target: { value: 'Rack fixed' } })
|
||||
expect(button().disabled).toBe(true)
|
||||
|
||||
const input = container.querySelector('input[type="file"]') as HTMLInputElement
|
||||
const file = new File(['x'], 'proof.jpg', { type: 'image/jpeg' })
|
||||
fireEvent.change(input, { target: { files: [file] } })
|
||||
expect(button().disabled).toBe(false)
|
||||
expect(screen.getByText('proof.jpg')).toBeTruthy()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user