Files
hackerspace.sh/app/f/[slug]/public-form-client.tsx
T
Moheeb Zara 03bb3a7466 feat(forms): builder UI, management pages, and public fill page (Phase 3)
- components/forms/form-renderer.tsx: one controlled renderer for all 8
  field types, shared by the builder preview and the public page
- components/forms/form-builder.tsx: easy builder — label-only field
  creation (keys auto-derived and de-duped at save), one-click type
  buttons, up/down reordering, inline options editor, live preview;
  edit mode adds publish/close/draft/delete and copy-public-link
- /forms directory, /forms/new, /forms/[id]/edit, /forms/[id]/results
  (CSV download). Server-gated by lib/forms-guard.ts (forms.manage via
  the same RPC the RLS uses; RLS remains the real boundary)
- public app/f/[slug] (outside the app group): getPublicForm serves
  only published public forms — members-only forms are never exposed
  there; waiver consent, anon email capture, signed-in gate for
  public_auth, thank-you state
- sidebar "Forms & waivers" link; sonner toasts (codebase convention)
- ARCHITECTURE + HANDOFF updated

285/285 tests, build clean, all four routes compiled. No deploy.
2026-05-16 15:03:48 -07:00

128 lines
3.7 KiB
TypeScript

'use client'
import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { FormRenderer } from '@/components/forms/form-renderer'
import { submitForm } from '@/lib/actions'
import type { FormField } from '@/lib/forms-schema'
export function PublicFormClient({
slug,
title,
description,
kind,
visibility,
legalText,
fields,
authed,
}: {
slug: string
title: string
description: string | null
kind: string
visibility: string
legalText: string | null
fields: FormField[]
authed: boolean
}) {
const [values, setValues] = useState<Record<string, unknown>>({})
const [email, setEmail] = useState('')
const [consent, setConsent] = useState(false)
const [submitting, setSubmitting] = useState(false)
const [error, setError] = useState<string | null>(null)
const [done, setDone] = useState(false)
const needsSignIn = visibility === 'public_auth' && !authed
const isWaiver = kind === 'waiver'
const showEmail = visibility === 'public_anon'
async function submit() {
setError(null)
setSubmitting(true)
const res = await submitForm({
slug,
answers: values,
email: showEmail && email ? email : undefined,
consent: isWaiver ? consent : undefined,
})
setSubmitting(false)
if ('error' in res && res.error) {
setError(res.error)
return
}
setDone(true)
}
if (done) {
return (
<div className="rounded-lg border p-8 text-center">
<h1 className="text-xl font-semibold">Thank you</h1>
<p className="mt-2 text-sm text-muted-foreground">Your response has been recorded.</p>
</div>
)
}
return (
<div className="space-y-6">
<div>
<h1 className="text-2xl font-semibold">{title}</h1>
{description && <p className="mt-1 text-muted-foreground">{description}</p>}
</div>
{needsSignIn ? (
<div className="rounded-lg border bg-muted/40 p-6 text-center">
<p className="text-sm">You need to be signed in to submit this form.</p>
<Button asChild className="mt-3">
<a href={`/login?next=/f/${slug}`}>Sign in</a>
</Button>
</div>
) : (
<>
{isWaiver && legalText && (
<div className="max-h-72 overflow-y-auto whitespace-pre-wrap rounded-lg border bg-muted/30 p-4 text-sm">
{legalText}
</div>
)}
<FormRenderer fields={fields} values={values} onChange={(k, v) => setValues(p => ({ ...p, [k]: v }))} />
{showEmail && (
<div className="space-y-1.5">
<Label htmlFor="pf-email">Your email (optional)</Label>
<Input
id="pf-email"
type="email"
value={email}
onChange={e => setEmail(e.target.value)}
/>
<p className="text-xs text-muted-foreground">
Used to link this to your account if you join later.
</p>
</div>
)}
{isWaiver && (
<label className="flex items-start gap-2 text-sm">
<input
type="checkbox"
checked={consent}
onChange={e => setConsent(e.target.checked)}
className="mt-1"
/>
I have read and agree to the terms above.
</label>
)}
{error && <p className="text-sm text-destructive">{error}</p>}
<Button onClick={submit} disabled={submitting}>
{submitting ? 'Submitting…' : 'Submit'}
</Button>
</>
)}
</div>
)
}