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}

+
+
+
+

+