diff --git a/app/(protected)/hse/capa/[id]/page.tsx b/app/(protected)/hse/capa/[id]/page.tsx index e086cb4..2589752 100644 --- a/app/(protected)/hse/capa/[id]/page.tsx +++ b/app/(protected)/hse/capa/[id]/page.tsx @@ -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' && } {isHse && status === 'verified' && } - {(capa.ownerNotes || capa.completedAt) && ( + {(capa.ownerNotes || capa.completedAt || photos.length > 0) && ( Completion report {capa.completedAt && ( @@ -113,6 +120,18 @@ export default async function CapaDetailPage({ params }: Props) { {capa.ownerNotes && ( {capa.ownerNotes} )} + {photos.length > 0 && ( + + {photos.map(p => ( + + + {/* eslint-disable-next-line @next/next/no-img-element */} + + + + ))} + + )} )} diff --git a/components/capa/capa-owner-actions.tsx b/components/capa/capa-owner-actions.tsx index e2d33ed..4e70872 100644 --- a/components/capa/capa-owner-actions.tsx +++ b/components/capa/capa-owner-actions.tsx @@ -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 ( - updateStatus('pending_verification')} disabled={loading}> - {loading ? '…' : 'Submit for Verification'} - - ) + return } return null diff --git a/components/capa/submit-verification-form.tsx b/components/capa/submit-verification-form.tsx new file mode 100644 index 0000000..fe041a5 --- /dev/null +++ b/components/capa/submit-verification-form.tsx @@ -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([]) + const [loading, setLoading] = useState(false) + const [error, setError] = useState(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 ( + + + Proof of completion + Describe what was done and attach 1 to {MAX_CAPA_PHOTOS} photos as evidence. + + + + setNote(e.target.value)} + rows={3} + placeholder="What action was carried out?" + disabled={loading} + /> + + + + = MAX_CAPA_PHOTOS} + onChange={e => { addFiles(e.target.files); e.target.value = '' }} + className="block w-full text-[13px] text-ink" + /> + + + {files.length > 0 && ( + + {files.map((f, i) => ( + + {f.name} + setFiles(prev => prev.filter((_, j) => j !== i))} + disabled={loading} + className="text-muted hover:text-ink" + > + + + + ))} + + )} + + {error && {error}} + + + {loading ? 'Submitting…' : 'Submit for Verification'} + + + ) +} diff --git a/tests/components/capa/submit-verification-form.test.tsx b/tests/components/capa/submit-verification-form.test.tsx new file mode 100644 index 0000000..08a6f08 --- /dev/null +++ b/tests/components/capa/submit-verification-form.test.tsx @@ -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() + 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() + }) +})
{capa.ownerNotes}
Describe what was done and attach 1 to {MAX_CAPA_PHOTOS} photos as evidence.
{error}