diff --git a/routes/[slug]/_middleware.ts b/routes/[slug]/_middleware.ts index d152bce..00ac250 100644 --- a/routes/[slug]/_middleware.ts +++ b/routes/[slug]/_middleware.ts @@ -5,10 +5,9 @@ import db from "../../utils/db/mod.ts"; import type { RootState } from "../_middleware.ts"; import type { Form } from "../../utils/db/mod.ts"; import type { User } from "../../utils/discord/user.ts"; -import type { FormSpec } from "../../utils/form/types.ts"; export type FormState = RootState & { - form: Form; + form: Form; user?: User; }; diff --git a/routes/[slug]/index.tsx b/routes/[slug]/index.tsx index 65da49c..49ef730 100644 --- a/routes/[slug]/index.tsx +++ b/routes/[slug]/index.tsx @@ -91,7 +91,7 @@ export const handler: Handlers = { const answers = parseFormData(formData, ctx.state.form); if (Object.keys(answers.issues).length > 0) { const errorData = encodeBase64Url(encode(answers)); - const firstError = ctx.state.form.questions?.questions + const firstError = ctx.state.form.questions?._ ?.find((question) => question.name in answers.issues)?.name; const headers = new Headers({ Location: @@ -249,7 +249,7 @@ export default function FormPage({ data, state }: PageProps) { > {data.completed && } {state.form.questions && - state.form.questions.questions.map((question: Question) => + state.form.questions._.map((question: Question) => question.type === "text" ? ( questions.value = [...questions.value, question]; diff --git a/routes/admin/forms/[form_id]/_middleware.ts b/routes/admin/forms/[form_id]/_middleware.ts index 849ea2c..4eb5ae3 100644 --- a/routes/admin/forms/[form_id]/_middleware.ts +++ b/routes/admin/forms/[form_id]/_middleware.ts @@ -4,10 +4,9 @@ import db from "../../../../utils/db/mod.ts"; import type { AdminState } from "../../_middleware.ts"; import type { Form } from "../../../../utils/db/mod.ts"; -import type { FormSpec } from "../../../../utils/form/types.ts"; export type AdminFormState = AdminState & { - form: Form; + form: Form; }; const form: MiddlewareHandler = async (_req, ctx) => { diff --git a/routes/admin/forms/[form_id]/index.tsx b/routes/admin/forms/[form_id]/index.tsx index ef5ee13..7b6eb06 100644 --- a/routes/admin/forms/[form_id]/index.tsx +++ b/routes/admin/forms/[form_id]/index.tsx @@ -61,7 +61,7 @@ export default defineRoute((_req, { state }) => { label="Description" value={state.form.description ?? undefined} /> - + (async (_req, { state }) => { - const questions = state.form.questions?.questions ?? []; + const questions = state.form.questions?._ ?? []; const responses = await db.responses.find({}, { where: (response, { eq }) => eq(response.form, state.form.id), }); diff --git a/utils/db/mod.ts b/utils/db/mod.ts index d62e8c6..c4300cd 100644 --- a/utils/db/mod.ts +++ b/utils/db/mod.ts @@ -2,15 +2,13 @@ import { setupRepository } from "./repository.ts"; import * as schema from "./schema.ts"; import sql from "./sql.ts"; -import type { FormSpec } from "../form/types.ts"; - export * from "./schema.ts"; export default { instances: setupRepository(schema.Instance), authSessions: setupRepository(schema.AuthSession), sessions: setupRepository(schema.Session), - forms: setupRepository(schema.Form), + forms: setupRepository(schema.Form), responses: setupRepository(schema.FormResponse), sql, }; diff --git a/utils/db/schema.ts b/utils/db/schema.ts index f3c16c3..3e3e912 100644 --- a/utils/db/schema.ts +++ b/utils/db/schema.ts @@ -1,6 +1,6 @@ import { column, Entity, table } from "./decorators.ts"; -import type { SerializableValue } from "./decorators.ts"; +import type { Question } from "../form/types.ts"; @table("instances") export class Instance extends Entity { @@ -115,7 +115,7 @@ export class Session extends Entity { } @table("forms") -export class Form extends Entity { +export class Form extends Entity { @column("id") id: string; @column("instance") @@ -129,7 +129,7 @@ export class Form extends Entity { @column("description") description?: string | null; @column("questions") - questions?: T | null; + questions?: { _: Question[] } | null; @column("success_message") successMessage?: string | null; @column("submitter_role") @@ -141,7 +141,7 @@ export class Form extends Entity { slug: string, active: boolean, description?: string, - questions?: T, + questions?: Question[], successMessage?: string, submitterRole?: bigint, ) { @@ -152,7 +152,7 @@ export class Form extends Entity { this.slug = slug; this.active = active; this.description = description; - this.questions = questions; + this.questions = questions && { _: questions }; this.successMessage = successMessage; this.submitterRole = submitterRole; } diff --git a/utils/form/parse.ts b/utils/form/parse.ts index 53d574a..cbe8fdc 100644 --- a/utils/form/parse.ts +++ b/utils/form/parse.ts @@ -1,6 +1,6 @@ import { toSnowflake } from "../discord/snowflake.ts"; -import type { FormSpec, Question, ValidationIssue } from "./types.ts"; +import type { Question, ValidationIssue } from "./types.ts"; import type { EntityProps } from "../db/decorators.ts"; import type { Form } from "../db/mod.ts"; @@ -88,7 +88,7 @@ function mapMaybe( export function parseEditorFormData( data: FormData, -): Omit>, "id" | "instance"> { +): Omit, "id" | "instance"> { const questions: Question[] = []; const formData = walkFormData(data); for (let [key, value] of Object.entries(formData.question ?? {}).sort()) { @@ -122,7 +122,7 @@ export function parseEditorFormData( slug: isString("slug", data.get("slug")), active: isString("active", data.get("active") ?? "off") === "on", description: mapMaybe(isString, "description", data.get("description")), - questions: { version: "v1", questions }, + questions: { _: questions }, successMessage: mapMaybe( isString, "success_message", @@ -138,7 +138,7 @@ export function parseEditorFormData( export function parseFormData( data: FormData, - form: Form, + form: Form, ): { answers: Record; issues: Record; @@ -148,7 +148,7 @@ export function parseFormData( if (form.questions) { const values = isAttrs("question", walkFormData(data).question); - for (const question of form.questions.questions) { + for (const question of form.questions._) { const name = `question.${question.name}`; if (question.type === "text") { answers[question.name] = diff --git a/utils/form/types.ts b/utils/form/types.ts index 45d0c17..497ad62 100644 --- a/utils/form/types.ts +++ b/utils/form/types.ts @@ -16,9 +16,4 @@ export type CheckboxQuestion = { export type Question = TextQuestion | CheckboxQuestion; -export type FormSpec = { - version: string; - questions: Question[]; -}; - export type ValidationIssue = "required";