-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into auth-n-auth
- Loading branch information
Showing
15 changed files
with
789 additions
and
290 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.