import { useState, useEffect } from "react" import { Link, useParams, useNavigate } from "react-router" import { AlertTriangle, ArrowLeft, BarChart3, Calendar, FileText, Lock, MessageSquare, Pencil, Trash2, } from "lucide-react" import { Navbar } from "@/components/shared/navbar" import { Footer } from "@/components/shared/footer" import { FormButton } from "@/components/shared/form-button" import { QuestionPreview } from "@/components/forms/question-preview" import { getFormById, deleteForm } from "@/lib/api" import { useAuth } from "@/app/context/auth-context" import { useToast } from "@/app/context/toast-context" import type { FormDetail } from "@/lib/types" export default function FormPreviewPage() { const { id } = useParams() const navigate = useNavigate() const { user } = useAuth() const { success, error: showError } = useToast() const [form, setForm] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [showDeleteConfirm, setShowDeleteConfirm] = useState(false) const [deleting, setDeleting] = useState(false) const [confirmText, setConfirmText] = useState("") const [showEditWarning, setShowEditWarning] = useState(false) const CONFIRM_PHRASE = "Aku suka femboy jadi hapus form ini" useEffect(() => { if (!id) return async function fetchForm() { try { const data = await getFormById(id!) setForm(data) } catch (err) { if (err instanceof Response && err.status === 404) { throw err } setError("Failed to load form. Please try again.") } finally { setLoading(false) } } fetchForm() }, [id]) async function handleDelete() { if (!id) return setDeleting(true) try { await deleteForm(id) success("Form deleted successfully.") navigate("/forms") } catch { showError("Failed to delete form.") setShowDeleteConfirm(false) } finally { setDeleting(false) } } if (loading) { return (

Loading...

) } if (error || !form) { return (

{error ?? "Form not found"}

Back to forms
) } return (
Back to forms
Read-only preview

{form.title}

{form.description}

{form.questions.length} questions {form.response_count} responses Created {new Date(form.created_at).toLocaleDateString()} Updated {new Date(form.updated_at).toLocaleDateString()}
{user && user.id === form.user_id && (
Responses {form.response_count > 0 ? ( setShowEditWarning(true)} > Edit ) : ( Edit )} { setConfirmText("") setShowDeleteConfirm(true) }} className="text-destructive hover:bg-destructive/10 hover:text-destructive" > Delete
)}
{form.questions.map((question, index) => (
))}

This is a read-only preview. Click below to fill out this form.

Fill Out Form
Back to all forms
{showDeleteConfirm && (
{form.response_count > 0 ? ( <>

Delete Form with Responses

This form has {form.response_count} response{form.response_count > 1 ? "s" : ""}. Deleting it will permanently remove all responses. This action cannot be undone.

To confirm, type {CONFIRM_PHRASE} below:

setConfirmText(e.target.value)} placeholder="Type the phrase above" className="mt-2 w-full rounded-lg border border-input bg-background px-3.5 py-2.5 text-sm text-foreground placeholder:text-muted-foreground outline-none transition-colors focus:border-primary focus:ring-2 focus:ring-primary/20" autoFocus />
setShowDeleteConfirm(false)} disabled={deleting} > Cancel {deleting ? "Deleting..." : "Delete Forever"}
) : ( <>

Delete Form

Are you sure you want to delete this form? This action cannot be undone.

setShowDeleteConfirm(false)} disabled={deleting} > Cancel {deleting ? "Deleting..." : "Delete"}
)}
)} {showEditWarning && (

Cannot Edit Form

This form already has {form.response_count} response{form.response_count > 1 ? "s" : ""}. Editing a form with existing responses is not allowed to preserve data integrity.

View Responses setShowEditWarning(false)} > Close
)}
) }