Skip to content

Commit

Permalink
Merge branch 'main' into auth-n-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
SteakFisher authored Jun 14, 2024
2 parents 8576890 + 6ad43e4 commit 76d4219
Show file tree
Hide file tree
Showing 15 changed files with 789 additions and 290 deletions.
19 changes: 9 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 52 additions & 6 deletions src/actions/FBValidateAction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,57 @@
"use server";

import { FBValidate } from "@/functions/FBValidation";
import FormItem from "@/interfaces/FormItem";
import { db, increment } from "@/helpers/drizzleTurso";
import FBFormObject from "@/interfaces/FormItemsObject";
import { eq } from "drizzle-orm";
import { redirect } from "next/navigation";
import { v4 as uuid } from "uuid";
import { formsTable } from "../../drizzle/schema";

export async function FBValidateAction(formItems: FormItem[]) {
return FBValidate(formItems);
// localStorage.setItem("formItemsObject", JSON.stringify(formItems));
// after saving
// redirect(`/form/${3}/edit`);
export async function FBValidateAction(
formObject: FBFormObject,
type: "edit" | "new",
) {
const errorObj = await FBValidate(formObject);
if (errorObj.message) return errorObj;

if (type === "edit") {
// store if present and permissions
const res = await db
.update(formsTable)
.set({
formJson: formObject.formItems,
version: increment(formsTable.version),
})
.where(eq(formsTable.formId, formObject.formId))
.returning({ formId: formsTable.formId });
if (res.length === 0) return { message: "Form not found", id: "0" };
return errorObj;

} else if (type === "new") {
// store if not present
while ((await checkFormId(formObject.formId)).length !== 0) {
formObject.formId = uuid();
}
await db
.insert(formsTable)
.values({
userId: "TestUserId",
formId: formObject.formId,
formJson: formObject.formItems,
version: 1,
})
.onConflictDoNothing();
// in future batch as much requests as possible, including form json for edit
redirect(`/form/${formObject.formId}/edit`);
}

return errorObj;
}

async function checkFormId(id: string) {
return db
.select({ formId: formsTable.formId })
.from(formsTable)
.where(eq(formsTable.formId, id));
}
28 changes: 28 additions & 0 deletions src/app/form/[formId]/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import FormBuilderWrapper from "@/components/FormBuilderWrapper";
import { db } from "@/helpers/drizzleTurso";
import FBFormObject from "@/interfaces/FormItemsObject";
import { and, eq } from "drizzle-orm";
import { formsTable } from "../../../../../drizzle/schema";

async function Page({ params }: { params: { formId: string } }) {
const res = await db
.select()
.from(formsTable)
.where(
and(
eq(formsTable.formId, params.formId),
eq(formsTable.userId, "TestUserId"),
),
);
if (res.length === 0) {
return <div>Form not found</div>;
}
const formObject: FBFormObject = {
formId: res[0].formId,
formItems: res[0].formJson,
};

return <FormBuilderWrapper formObject={formObject} type="edit" />;
}

export default Page;
File renamed without changes.
Loading

0 comments on commit 76d4219

Please sign in to comment.