import { useState, useEffect } from 'react' import Layout from '@/components/layout/Layout' import { Card, EmptyState, Btn, Tag, Field, Textarea, showToast } from '@/components/ui' import { FormFieldList } from '@/components/forms/FieldRenderer' import { useApp } from '@/lib/context' const ISSUE_AREAS = [ { value: 'Production', icon: 'M9 22V12h6v10M3 9l9-7 9 7' }, { value: 'Goods-In', icon: 'M3 3h18v18H3zM3 9h18M9 21V9' }, { value: 'Warehouse', icon: 'M21 8V7l-3-4H6L3 7v1m18 0v12H3V8m18 0H3m9 4v4' }, { value: 'QA Lab', icon: 'M9 2v6l-5 8a2 2 0 002 3h12a2 2 0 002-3l-5-8V2M8.5 13h7' }, { value: 'Shipping', icon: 'M16 16V8a2 2 0 00-2-2H4a2 2 0 00-2 2v8a2 2 0 002 2h10a2 2 0 002-2zm0-3h2.5a2 2 0 011.6.8l1.4 1.87a2 2 0 01.4 1.2V16a1 1 0 01-1 1h-3z' }, { value: 'Other', icon: 'M12 5v.01M12 12v.01M12 19v.01' }, ] export default function FillPage() { const { user } = useApp() const [forms, setForms] = useState([]) const [loading, setLoading] = useState(true) const [selected, setSelected] = useState(null) const [answers, setAnswers] = useState>({}) const [submitting, setSubmitting] = useState(false) const [submitted, setSubmitted] = useState(false) // Report an issue const [pageTab, setPageTab] = useState<'forms' | 'report'>('forms') const [reportDesc, setReportDesc] = useState('') const [reportArea, setReportArea] = useState('') const [reportSubmitting, setReportSubmitting] = useState(false) const [reportRef, setReportRef] = useState('') useEffect(() => { loadForms() }, []) async function loadForms() { setLoading(true) const res = await fetch('/api/forms?status=ACTIVE') if (res.ok) { const { data } = await res.json() setForms(data || []) } setLoading(false) } function selectForm(form: any) { setSelected(form) setAnswers({}) setSubmitted(false) } async function submitForm() { const required = selected.fields.filter((f: any) => f.required) for (const f of required) { if (!answers[f.id]) { showToast(`"${f.label}" is required`, 'error'); return } } setSubmitting(true) const res = await fetch('/api/submissions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ formId: selected.id, data: answers }), }) setSubmitting(false) if (res.ok) { const { submissionCount } = await res.json() setSubmitted(true) showToast(`Submitted — you are contributor #${submissionCount}`) } else { showToast('Submission failed', 'error') } } function setAnswer(fieldId: string, value: any) { setAnswers(a => ({ ...a, [fieldId]: value })) } async function submitReport() { if (!reportDesc.trim()) { showToast('Please describe what you noticed', 'error'); return } if (!reportArea) { showToast('Please pick where', 'error'); return } setReportSubmitting(true) const res = await fetch('/api/ncrs', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ description: reportDesc, source: reportArea }), }) setReportSubmitting(false) if (res.ok) { const { data } = await res.json() setReportRef(data.ref) setReportDesc('') setReportArea('') } else { showToast('Could not submit report', 'error') } } function resetReport() { setReportRef('') setReportDesc('') setReportArea('') } return ( {!selected ? ( <>
{(['forms', 'report'] as const).map(t => ( ))}
{pageTab === 'forms' ? ( <>

First build forms

Select a form to fill out — your data helps set quality standards

{loading ? (
Loading…
) : forms.length === 0 ? ( ) : (
{forms.map((form: any) => (
selectForm(form)} style={{ background: 'white', border: '0.5px solid #eee', borderRadius: '12px', padding: '16px', cursor: 'pointer', transition: 'border-color 0.1s' }}>
Active
{form.name}
{form.product &&
{form.product}
}
{form.fields?.length || 0} fields · {form._count?.submissions || 0} submissions so far
Fill out →
))}
)} ) : (
{reportRef ? (
Reported — thanks
QC will review {reportRef}. You'll be notified automatically once a fix is confirmed — nothing else to do.
Report another
) : (

Report an issue

Notice something off? Let QC know — takes 30 seconds. No quality background needed.