Files
erika/app/form/[id]/page.tsx
T

58 lines
1.6 KiB
TypeScript

import { db } from "@/db";
import { FormFillerClient } from "../client";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { notFound } from "next/navigation";
export const dynamic = "force-dynamic";
export default async function FormsSubmitPage(
props: {
params: Promise<{ id: string }>;
}
) {
const params = await props.params;
const session = await getServerSession(authOptions);
const discordId = (session?.user as { discordId?: string })?.discordId;
const form = await db.query.forms.findFirst({
where: (f, { eq }) => eq(f.id, params.id),
});
if (!form) {
return notFound();
}
const questionsList = await db.query.questions.findMany({
where: (q, { eq }) => eq(q.formId, form.id),
orderBy: (q, { asc }) => [asc(q.displayOrder)],
});
let initialAnswers: Record<string, string> = {};
if (discordId) {
const existingSubmission = await db.query.submissions.findFirst({
where: (s, { eq, and }) =>
and(eq(s.formId, form.id), eq(s.userDiscordId, discordId)),
});
if (existingSubmission) {
const answersList = await db.query.answers.findMany({
where: (a, { eq }) => eq(a.submissionId, existingSubmission.id),
});
for (const a of answersList) {
initialAnswers[a.questionId] = a.value;
}
}
}
return (
<div className="max-w-2xl mx-auto p-4 md:p-6 space-y-6">
<FormFillerClient
form={form}
questions={questionsList}
initialAnswers={initialAnswers}
/>
</div>
);
}