From c553f9bfff309c833b0ff718d663409af4a23637 Mon Sep 17 00:00:00 2001 From: MeowcaTheoRange Date: Wed, 24 Apr 2024 13:07:27 -0500 Subject: [PATCH] It's finally over --- .../content/[contentid]/components/form.tsx | 31 +++++ .../delete/content/[contentid]/page.tsx | 67 ++++++++++ .../delete/jam/[jamid]/components/form.tsx | 31 +++++ .../jams/(manip)/delete/jam/[jamid]/page.tsx | 67 ++++++++++ .../[judgementid]/components/form.tsx | 31 +++++ .../delete/judgement/[judgementid]/page.tsx | 67 ++++++++++ .../content/[contentid]/components/form.tsx | 87 +++++++++++++ .../(manip)/edit/content/[contentid]/page.tsx | 67 ++++++++++ .../edit/jam/[jamid]/components/form.tsx | 115 ++++++++++++++++++ .../jams/(manip)/edit/jam/[jamid]/page.tsx | 67 ++++++++++ .../[judgementid]/components/form.tsx | 62 ++++++++++ .../edit/judgement/[judgementid]/page.tsx | 67 ++++++++++ .../new/content/[jamid]/components/form.tsx | 102 ++++++++++++---- .../jams/(manip)/new/content/[jamid]/page.tsx | 18 +-- .../jams/(manip)/new/jam/components/form.tsx | 113 +++++++++++++++++ src/app/jams/(manip)/new/jam/page.tsx | 48 ++++++++ .../judgement/[contentid]/components/form.tsx | 65 ++++++++++ .../new/judgement/[contentid]/page.tsx | 60 +++++++++ .../[content]/judgements/[judgement]/route.ts | 16 +-- src/app/jams/api/content/[content]/route.ts | 8 ++ src/app/jams/api/jams/[jam]/route.ts | 9 ++ src/app/jams/api/jams/route.ts | 24 ++-- src/app/jams/content/[content]/page.tsx | 15 ++- src/app/jams/jam/[jam]/page.tsx | 2 +- src/app/jams/page.tsx | 2 +- src/app/jams/user/[user]/page.tsx | 2 +- src/lib/mastoauth/mastoauth.ts | 4 +- src/styles/style.css | 10 ++ 28 files changed, 1197 insertions(+), 60 deletions(-) create mode 100644 src/app/jams/(manip)/delete/content/[contentid]/components/form.tsx create mode 100644 src/app/jams/(manip)/delete/content/[contentid]/page.tsx create mode 100644 src/app/jams/(manip)/delete/jam/[jamid]/components/form.tsx create mode 100644 src/app/jams/(manip)/delete/jam/[jamid]/page.tsx create mode 100644 src/app/jams/(manip)/delete/judgement/[judgementid]/components/form.tsx create mode 100644 src/app/jams/(manip)/delete/judgement/[judgementid]/page.tsx create mode 100644 src/app/jams/(manip)/edit/content/[contentid]/components/form.tsx create mode 100644 src/app/jams/(manip)/edit/jam/[jamid]/components/form.tsx create mode 100644 src/app/jams/(manip)/edit/judgement/[judgementid]/components/form.tsx create mode 100644 src/app/jams/(manip)/new/jam/components/form.tsx create mode 100644 src/app/jams/(manip)/new/judgement/[contentid]/components/form.tsx diff --git a/src/app/jams/(manip)/delete/content/[contentid]/components/form.tsx b/src/app/jams/(manip)/delete/content/[contentid]/components/form.tsx new file mode 100644 index 0000000..e474a1c --- /dev/null +++ b/src/app/jams/(manip)/delete/content/[contentid]/components/form.tsx @@ -0,0 +1,31 @@ +'use client'; +import React from "react"; +import { useFormik } from "formik"; +import { useRouter } from "next/navigation"; + +export function Form({contentID}:{contentID:string}) { + const router = useRouter(); + const formik = useFormik({ + initialValues: {}, + onSubmit: async (values) => { + const submitRequest = await fetch(`/jams/api/content/${contentID}`, { + method: "DELETE" + }); + + if (submitRequest.ok) { + router.push(`/jams/`); + } else { + formik.setSubmitting(false); + } + }, + }); + + return ( +
+
+

+ +
+
+ ); +} \ No newline at end of file diff --git a/src/app/jams/(manip)/delete/content/[contentid]/page.tsx b/src/app/jams/(manip)/delete/content/[contentid]/page.tsx new file mode 100644 index 0000000..04f48b9 --- /dev/null +++ b/src/app/jams/(manip)/delete/content/[contentid]/page.tsx @@ -0,0 +1,67 @@ + +import { Conditional, ConditionalNull } from "@/components/utility/Conditional"; +import { MainLayout } from "@/layout/MainLayout/MainLayout"; +import { db } from "@/lib/mastoauth/kysely"; +import { JSONContentTable, JSONJamTable } from "@/lib/mastoauth/realtypes"; +import { cookies } from "next/headers"; +import { notFound, redirect } from "next/navigation"; +import { Form } from "./components/form"; + +export default async function Home({ + params +}: { + params: { + contentid: string + } +}) { + const cookieStore = cookies(); + const token = cookieStore.get('token')?.value; + let existingUser; + if (token != null) { + let existingToken = await db + .selectFrom('tokens') + .where('tokens.id', '=', token) + .select('owner') + .executeTakeFirst(); + + if (existingToken != null) { + existingUser = await db + .selectFrom('users') + .where('users.id', '=', existingToken.owner) + .selectAll() + .executeTakeFirst(); + } + } + + if (existingUser == null) return redirect("/jams/"); + + const contentid = params.contentid; + + if (contentid == null) return notFound(); + + // It's a JSONJamTable. I don't know why TS hates `number` => `string` conversion. + let editingContent = await db + .selectFrom('content') + .where('content.id', '=', contentid) + .selectAll() + .executeTakeFirst() as unknown as JSONContentTable; + + if (editingContent == null) return notFound(); + + if (!existingUser.admin && editingContent.author_id != existingUser.id) return ( + +

Can't delete this submission

+

You do not own this submission

+
+ ); + + return ( + +

Deleting {editingContent.name}

+ +

Logged in as {existingUser?.username}@{existingUser?.instance}

+
+
+ + ) +} \ No newline at end of file diff --git a/src/app/jams/(manip)/delete/jam/[jamid]/components/form.tsx b/src/app/jams/(manip)/delete/jam/[jamid]/components/form.tsx new file mode 100644 index 0000000..aac4aeb --- /dev/null +++ b/src/app/jams/(manip)/delete/jam/[jamid]/components/form.tsx @@ -0,0 +1,31 @@ +'use client'; +import React from "react"; +import { useFormik } from "formik"; +import { useRouter } from "next/navigation"; + +export function Form({jamID}:{jamID:string}) { + const router = useRouter(); + const formik = useFormik({ + initialValues: {}, + onSubmit: async (values) => { + const submitRequest = await fetch(`/jams/api/jams/${jamID}`, { + method: "DELETE" + }); + + if (submitRequest.ok) { + router.push(`/jams/`); + } else { + formik.setSubmitting(false); + } + }, + }); + + return ( + +
+

+ +
+
+ ); +} \ No newline at end of file diff --git a/src/app/jams/(manip)/delete/jam/[jamid]/page.tsx b/src/app/jams/(manip)/delete/jam/[jamid]/page.tsx new file mode 100644 index 0000000..c270bb7 --- /dev/null +++ b/src/app/jams/(manip)/delete/jam/[jamid]/page.tsx @@ -0,0 +1,67 @@ + +import { Conditional, ConditionalNull } from "@/components/utility/Conditional"; +import { MainLayout } from "@/layout/MainLayout/MainLayout"; +import { db } from "@/lib/mastoauth/kysely"; +import { JSONContentTable, JSONJamTable } from "@/lib/mastoauth/realtypes"; +import { cookies } from "next/headers"; +import { notFound, redirect } from "next/navigation"; +import { Form } from "./components/form"; + +export default async function Home({ + params +}: { + params: { + jamid: string + } +}) { + const cookieStore = cookies(); + const token = cookieStore.get('token')?.value; + let existingUser; + if (token != null) { + let existingToken = await db + .selectFrom('tokens') + .where('tokens.id', '=', token) + .select('owner') + .executeTakeFirst(); + + if (existingToken != null) { + existingUser = await db + .selectFrom('users') + .where('users.id', '=', existingToken.owner) + .selectAll() + .executeTakeFirst(); + } + } + + if (existingUser == null) return redirect("/jams/"); + + const jamid = params.jamid; + + if (jamid == null) return notFound(); + + // It's a JSONJamTable. I don't know why TS hates `number` => `string` conversion. + let editingJam = await db + .selectFrom('jams') + .where('jams.id', '=', jamid) + .selectAll() + .executeTakeFirst() as unknown as JSONJamTable; + + if (editingJam == null) return notFound(); + + if (!existingUser.admin && editingJam.author_id != existingUser.id) return ( + +

Can't delete this Jam

+

You do not own this Jam

+
+ ); + + return ( + +

Deleting {editingJam.name}

+ +

Logged in as {existingUser?.username}@{existingUser?.instance}

+
+
+ + ) +} \ No newline at end of file diff --git a/src/app/jams/(manip)/delete/judgement/[judgementid]/components/form.tsx b/src/app/jams/(manip)/delete/judgement/[judgementid]/components/form.tsx new file mode 100644 index 0000000..2d8a759 --- /dev/null +++ b/src/app/jams/(manip)/delete/judgement/[judgementid]/components/form.tsx @@ -0,0 +1,31 @@ +'use client'; +import React from "react"; +import { useFormik } from "formik"; +import { useRouter } from "next/navigation"; + +export function Form({judgementID, contentID}:{judgementID:string, contentID:string}) { + const router = useRouter(); + const formik = useFormik({ + initialValues: {}, + onSubmit: async (values) => { + const submitRequest = await fetch(`/jams/api/content/${contentID}/judgements/${judgementID}`, { + method: "DELETE" + }); + + if (submitRequest.ok) { + router.push(`/jams/`); + } else { + formik.setSubmitting(false); + } + }, + }); + + return ( + +
+

+ +
+
+ ); +} \ No newline at end of file diff --git a/src/app/jams/(manip)/delete/judgement/[judgementid]/page.tsx b/src/app/jams/(manip)/delete/judgement/[judgementid]/page.tsx new file mode 100644 index 0000000..530ff3e --- /dev/null +++ b/src/app/jams/(manip)/delete/judgement/[judgementid]/page.tsx @@ -0,0 +1,67 @@ + +import { Conditional, ConditionalNull } from "@/components/utility/Conditional"; +import { MainLayout } from "@/layout/MainLayout/MainLayout"; +import { db } from "@/lib/mastoauth/kysely"; +import { JSONContentTable, JSONJamTable, JSONJudgementTable } from "@/lib/mastoauth/realtypes"; +import { cookies } from "next/headers"; +import { notFound, redirect } from "next/navigation"; +import { Form } from "./components/form"; + +export default async function Home({ + params +}: { + params: { + judgementid: string + } +}) { + const cookieStore = cookies(); + const token = cookieStore.get('token')?.value; + let existingUser; + if (token != null) { + let existingToken = await db + .selectFrom('tokens') + .where('tokens.id', '=', token) + .select('owner') + .executeTakeFirst(); + + if (existingToken != null) { + existingUser = await db + .selectFrom('users') + .where('users.id', '=', existingToken.owner) + .selectAll() + .executeTakeFirst(); + } + } + + if (existingUser == null) return redirect("/jams/"); + + const judgementid = params.judgementid; + + if (judgementid == null) return notFound(); + + // It's a JSONJamTable. I don't know why TS hates `number` => `string` conversion. + let editingJudgement = await db + .selectFrom('judgements') + .where('judgements.id', '=', judgementid) + .selectAll() + .executeTakeFirst() as unknown as JSONJudgementTable; + + if (editingJudgement == null) return notFound(); + + if (!existingUser.admin && editingJudgement.author_id != existingUser.id) return ( + +

Can't delete this judgement

+

You did not publish this judgement

+
+ ); + + return ( + +

Deleting "{editingJudgement.content}"

+ +

Logged in as {existingUser?.username}@{existingUser?.instance}

+
+
+ + ) +} \ No newline at end of file diff --git a/src/app/jams/(manip)/edit/content/[contentid]/components/form.tsx b/src/app/jams/(manip)/edit/content/[contentid]/components/form.tsx new file mode 100644 index 0000000..6fb6f98 --- /dev/null +++ b/src/app/jams/(manip)/edit/content/[contentid]/components/form.tsx @@ -0,0 +1,87 @@ +'use client'; +import React from "react"; +import { ErrorMessage, useFormik } from "formik"; +import { useRouter } from "next/navigation"; +import { ConditionalNull } from "@/components/utility/Conditional"; +import { JSONContentTable } from "@/lib/mastoauth/realtypes"; + +export function Form({contentID, preset}:{contentID:string, preset:Partial}) { + const router = useRouter(); + const formik = useFormik({ + initialValues: preset, + onSubmit: async (values) => { + const submitRequest = await fetch(`/jams/api/content/${contentID}`, { + method: "PATCH", + body: JSON.stringify(values) + }); + + if (submitRequest.ok) { + router.push(`/jams/content/${contentID}/`); + } else { + formik.setSubmitting(false); + if (submitRequest.status == 400) { + let errors:string[]|null = null; + try { + errors = await submitRequest.json(); + } catch (err) { }; // No body. Skill issue + if (errors == null) return; + let transformedErrors:{[key:string]:string} = {}; + errors.forEach((string) => transformedErrors[string] = "Something's wrong here"); + formik.setErrors(transformedErrors); + } else { + formik.setErrors({ + name: "Something went wrong..." + }); + } + } + }, + }); + + return ( + +
+

+ + +

Error: {formik.errors.name}

+
+
+
+

+