Skip to content

Commit

Permalink
simplify form questions field
Browse files Browse the repository at this point in the history
  • Loading branch information
jbellerb committed Apr 13, 2024
1 parent 4ade3a6 commit 794b914
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 32 deletions.
3 changes: 1 addition & 2 deletions routes/[slug]/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormSpec>;
form: Form;
user?: User;
};

Expand Down
4 changes: 2 additions & 2 deletions routes/[slug]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const handler: Handlers<Data, State> = {
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:
Expand Down Expand Up @@ -249,7 +249,7 @@ export default function FormPage({ data, state }: PageProps<Data, State>) {
>
{data.completed && <ResubmitWarning />}
{state.form.questions &&
state.form.questions.questions.map((question: Question) =>
state.form.questions._.map((question: Question) =>
question.type === "text"
? (
<FormTextQuestion
Expand Down
9 changes: 3 additions & 6 deletions routes/admin/forms/[form_id]/(_islands)/QuestionEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import Select from "../../../../../components/Select.tsx";
import TextInput from "../../../../../components/TextInput.tsx";
import classnames from "../../../../../utils/classnames.ts";

import type { FormSpec, Question } from "../../../../../utils/form/types.ts";
import type { Question } from "../../../../../utils/form/types.ts";

export type Props = {
questions?: FormSpec;
questions?: Question[];
};

export default function QuestionEditor(props: Props) {
if (props.questions && props.questions.version !== "v1") {
throw new Error("unsupported question format version");
}
const questions = useSignal(props.questions?.questions ?? []);
const questions = useSignal(props.questions ?? []);

const addQuestion = (question: Question) =>
questions.value = [...questions.value, question];
Expand Down
3 changes: 1 addition & 2 deletions routes/admin/forms/[form_id]/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormSpec>;
form: Form;
};

const form: MiddlewareHandler<AdminFormState> = async (_req, ctx) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/admin/forms/[form_id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default defineRoute<State>((_req, { state }) => {
label="Description"
value={state.form.description ?? undefined}
/>
<QuestionEditor questions={state.form.questions ?? undefined} />
<QuestionEditor questions={state.form.questions?._} />
<GrowableTextArea
name="success_message"
label="Success Message"
Expand Down
2 changes: 1 addition & 1 deletion routes/admin/forms/[form_id]/results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import db from "../../../../utils/db/mod.ts";
import { fromSnowflake } from "../../../../utils/discord/snowflake.ts";

export default defineRoute<State>(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),
});
Expand Down
4 changes: 1 addition & 3 deletions utils/db/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormSpec>),
forms: setupRepository(schema.Form),
responses: setupRepository(schema.FormResponse),
sql,
};
10 changes: 5 additions & 5 deletions utils/db/schema.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -115,7 +115,7 @@ export class Session extends Entity {
}

@table("forms")
export class Form<T extends SerializableValue> extends Entity {
export class Form extends Entity {
@column("id")
id: string;
@column("instance")
Expand All @@ -129,7 +129,7 @@ export class Form<T extends SerializableValue> extends Entity {
@column("description")
description?: string | null;
@column("questions")
questions?: T | null;
questions?: { _: Question[] } | null;
@column("success_message")
successMessage?: string | null;
@column("submitter_role")
Expand All @@ -141,7 +141,7 @@ export class Form<T extends SerializableValue> extends Entity {
slug: string,
active: boolean,
description?: string,
questions?: T,
questions?: Question[],
successMessage?: string,
submitterRole?: bigint,
) {
Expand All @@ -152,7 +152,7 @@ export class Form<T extends SerializableValue> extends Entity {
this.slug = slug;
this.active = active;
this.description = description;
this.questions = questions;
this.questions = questions && { _: questions };
this.successMessage = successMessage;
this.submitterRole = submitterRole;
}
Expand Down
10 changes: 5 additions & 5 deletions utils/form/parse.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -88,7 +88,7 @@ function mapMaybe<T, R>(

export function parseEditorFormData(
data: FormData,
): Omit<EntityProps<Form<FormSpec>>, "id" | "instance"> {
): Omit<EntityProps<Form>, "id" | "instance"> {
const questions: Question[] = [];
const formData = walkFormData(data);
for (let [key, value] of Object.entries(formData.question ?? {}).sort()) {
Expand Down Expand Up @@ -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",
Expand All @@ -138,7 +138,7 @@ export function parseEditorFormData(

export function parseFormData(
data: FormData,
form: Form<FormSpec>,
form: Form,
): {
answers: Record<string, string>;
issues: Record<string, ValidationIssue[]>;
Expand All @@ -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] =
Expand Down
5 changes: 0 additions & 5 deletions utils/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,4 @@ export type CheckboxQuestion = {

export type Question = TextQuestion | CheckboxQuestion;

export type FormSpec = {
version: string;
questions: Question[];
};

export type ValidationIssue = "required";

0 comments on commit 794b914

Please sign in to comment.