"use client";
import SubmitToast from "@/app/components/ui/SubmitToast";
import { useMemo, useState } from "react";
type QuestionOption = { value: string; label: string };
type Question = {
id: string;
type: "rating" | "dropdown" | "scale" | "rating_labeled" | "checkbox_group" | "long_text";
label: string;
min?: number;
max?: number;
placeholder?: string;
options?: QuestionOption[];
hasOther?: boolean;
};
type Section = {
id: string;
title: string;
subtitle: string;
questions: Question[];
};
type FeedbackJson = {
title: string;
subtitle: string;
sections: Section[];
submit: { label: string; disclaimer: string };
};
function RadioScale({
name,
label,
min = 1,
max = 5,
value,
onChange,
}: {
name: string;
label: string;
min?: number;
max?: number;
value: string;
onChange: (v: string) => void;
}) {
const values = useMemo(() => {
const out: number[] = [];
for (let i = min; i <= max; i++) out.push(i);
return out;
}, [min, max]);
return (
{values.map((n) => (
))}
);
}
export default function FeedbackForm({ data }: { data: FeedbackJson }) {
const [form, setForm] = useState>({});
const [submitState, setSubmitState] = useState<"idle" | "sending" | "ok" | "error">("idle");
const updateField = (id: string, value: any) => {
setForm((s) => ({ ...s, [id]: value }));
};
const toggleCheckbox = (id: string, value: string) => {
setForm((s) => {
const current = s[id] || [];
return {
...s,
[id]: current.includes(value) ? current.filter((v: string) => v !== value) : [...current, value],
};
});
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setSubmitState("sending");
try {
const res = await fetch("/ipv6/api/submissions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
type: "feedback",
...form,
}),
});
if (!res.ok) throw new Error("Submission failed");
setSubmitState("ok");
} catch {
setSubmitState("error");
}
};
return (
);
}