;
changelog: {id: string};
}>) {
+ const [_state, formAction] = useActionState(action, {});
return (
+ );
+};
diff --git a/apps/changelog/src/client/components/forms/editChangelogForm.tsx b/apps/changelog/src/client/components/forms/editChangelogForm.tsx
new file mode 100644
index 0000000000000..64265d13521f5
--- /dev/null
+++ b/apps/changelog/src/client/components/forms/editChangelogForm.tsx
@@ -0,0 +1,76 @@
+'use client';
+
+import {editChangelog} from '@/server/actions/changelog';
+import {TitleSlug} from '@/client/components/titleSlug';
+import {FileUpload} from '@/client/components/fileUpload';
+import {Select} from '@/client/components/ui/Select';
+import {ForwardRefEditor} from '@/client/components/forwardRefEditor';
+import {Button} from '@/client/components/ui/Button';
+import {Fragment, Suspense, useActionState} from 'react';
+import {Changelog, Category} from '@prisma/client';
+import Link from 'next/link';
+
+export const EditChangelogForm = ({
+ changelog,
+ categories,
+}: {
+ changelog: Changelog;
+ categories: Category[];
+}) => {
+ const [_state, formAction] = useActionState(editChangelog, {});
+ return (
+
+ );
+};
diff --git a/apps/changelog/src/server/actions/changelog.ts b/apps/changelog/src/server/actions/changelog.ts
index 249cdb4ba22fc..0ef1b199e3766 100644
--- a/apps/changelog/src/server/actions/changelog.ts
+++ b/apps/changelog/src/server/actions/changelog.ts
@@ -1,154 +1,180 @@
-"use server";
-
-import { revalidatePath, revalidateTag } from "next/cache";
-import { redirect } from "next/navigation";
-import { getServerSession } from "next-auth/next";
-import { prismaClient } from "../prisma-client";
-import { authOptions } from "../authOptions";
-
-export async function unpublishChangelog(formData: FormData) {
+'use server';
+
+import {revalidatePath, revalidateTag} from 'next/cache';
+import {redirect} from 'next/navigation';
+import {getServerSession} from 'next-auth/next';
+import {prismaClient} from '../prisma-client';
+import {authOptions} from '../authOptions';
+import {ServerActionPayloadInterface} from './serverActionPayload.interface';
+
+const unauthorizedPayload: ServerActionPayloadInterface = {
+ success: false,
+ message: 'Unauthorized',
+};
+
+export async function unpublishChangelog(
+ _currentState: ServerActionPayloadInterface,
+ formData: FormData
+): Promise {
const session = await getServerSession(authOptions);
if (!session) {
- return { message: "Unauthorized" };
+ return unauthorizedPayload;
}
- const id = formData.get("id") as string;
+ const id = formData.get('id') as string;
try {
await prismaClient.changelog.update({
- where: { id },
- data: { published: false, publishedAt: null },
+ where: {id},
+ data: {published: false, publishedAt: null},
});
} catch (error) {
// eslint-disable-next-line no-console
- console.error("DELETE ACTION ERROR:", error);
- return { message: "Unable to unpublish changelog" };
+ console.error('DELETE ACTION ERROR:', error);
+ return {message: 'Unable to unpublish changelog', success: false};
}
- revalidateTag("changelogs");
- revalidateTag("changelog-detail");
- return revalidatePath(`/changelog/_admin`);
+ revalidateTag('changelogs');
+ revalidateTag('changelog-detail');
+ revalidatePath(`/changelog/_admin`);
+ return {success: true};
}
-export async function publishChangelog(formData: FormData) {
+export async function publishChangelog(
+ _currentState: ServerActionPayloadInterface,
+ formData: FormData
+): Promise {
const session = await getServerSession();
if (!session) {
- return { message: "Unauthorized" };
+ return unauthorizedPayload;
}
- const id = formData.get("id") as string;
+ const id = formData.get('id') as string;
try {
await prismaClient.changelog.update({
- where: { id },
- data: { published: true, publishedAt: new Date().toISOString() },
+ where: {id},
+ data: {published: true, publishedAt: new Date().toISOString()},
});
} catch (error) {
// eslint-disable-next-line no-console
- console.error("DELETE ACTION ERROR:", error);
- return { message: "Unable to publish changelog" };
+ console.error('DELETE ACTION ERROR:', error);
+ return {message: 'Unable to publish changelog', success: false};
}
- revalidateTag("changelogs");
- revalidateTag("changelog-detail");
- return revalidatePath(`/changelog/_admin`);
+ revalidateTag('changelogs');
+ revalidateTag('changelog-detail');
+ revalidatePath(`/changelog/_admin`);
+ return {success: true};
}
-export async function createChangelog(formData: FormData) {
+export async function createChangelog(
+ _currentState: ServerActionPayloadInterface,
+ formData: FormData
+): Promise {
const session = await getServerSession(authOptions);
if (!session) {
- return { message: "Unauthorized" };
+ return unauthorizedPayload;
}
- const categories = formData.getAll("categories");
+ console.log({formData});
+ const categories = formData.getAll('categories');
await prismaClient.category.createMany({
- data: categories.map((category) => ({ name: category as string })),
+ data: categories.map(category => ({name: category as string})),
skipDuplicates: true,
});
- const connect = categories.map((category) => {
- return { name: category as string };
+ const connect = categories.map(category => {
+ return {name: category as string};
});
if (session.user?.email == null) {
- throw new Error("Invariant: Users must have emails");
+ throw new Error('Invariant: Users must have emails');
}
const user = await prismaClient.user.findUnique({
- where: { email: session.user.email },
+ where: {email: session.user.email},
});
const data = {
- title: formData.get("title") as string,
- content: formData.get("content") as string,
- summary: formData.get("summary") as string,
- image: formData.get("image") as string,
- slug: formData.get("slug") as string,
- author: user ? { connect: { id: user.id } } : undefined,
- categories: formData.get("categories") !== "" ? { connect } : {},
+ title: formData.get('title') as string,
+ content: formData.get('content') as string,
+ summary: formData.get('summary') as string,
+ image: formData.get('image') as string,
+ slug: formData.get('slug') as string,
+ author: user ? {connect: {id: user.id}} : undefined,
+ categories: formData.get('categories') !== '' ? {connect} : {},
};
- await prismaClient.changelog.create({ data });
+ await prismaClient.changelog.create({data});
return redirect(`/changelog/_admin`);
}
-export async function editChangelog(formData: FormData) {
+export async function editChangelog(
+ _currentState: ServerActionPayloadInterface,
+ formData: FormData
+): Promise {
const session = await getServerSession(authOptions);
if (!session) {
- return { message: "Unauthorized" };
+ return unauthorizedPayload;
}
- const id = formData.get("id") as string;
- const categories = formData.getAll("categories");
+ const id = formData.get('id') as string;
+ const categories = formData.getAll('categories');
await prismaClient.category.createMany({
- data: categories.map((category) => ({ name: category as string })),
+ data: categories.map(category => ({name: category as string})),
skipDuplicates: true,
});
- const connect = categories.map((category) => {
- return { name: category as string };
+ const connect = categories.map(category => {
+ return {name: category as string};
});
try {
const data = {
- title: formData.get("title") as string,
- content: formData.get("content") as string,
- summary: formData.get("summary") as string,
- image: formData.get("image") as string,
- slug: formData.get("slug") as string,
- categories:
- formData.get("categories") !== "" ? { set: [...connect] } : { set: [] },
+ title: formData.get('title') as string,
+ content: formData.get('content') as string,
+ summary: formData.get('summary') as string,
+ image: formData.get('image') as string,
+ slug: formData.get('slug') as string,
+ categories: formData.get('categories') !== '' ? {set: [...connect]} : {set: []},
};
await prismaClient.changelog.update({
- where: { id },
+ where: {id},
data,
});
- } catch (error) {
+ } catch (error: any) {
// eslint-disable-next-line no-console
- console.error("EDIT ACTION ERROR:", error);
- return { message: error };
+ console.error('EDIT ACTION ERROR:', error);
+ return {message: (error as Error).message, success: false};
}
- revalidateTag("changelogs");
- revalidateTag("changelog-detail");
+ revalidateTag('changelogs');
+ revalidateTag('changelog-detail');
return redirect(`/changelog/_admin`);
}
-export async function deleteChangelog(formData: FormData) {
+export async function deleteChangelog(
+ _currentState: ServerActionPayloadInterface,
+ formData: FormData
+): Promise {
const session = await getServerSession(authOptions);
if (!session) {
- return { message: "Unauthorized" };
+ return unauthorizedPayload;
}
- const id = formData.get("id") as string;
+ const id = formData.get('id') as string;
try {
await prismaClient.changelog.delete({
- where: { id },
+ where: {id},
});
} catch (error) {
// eslint-disable-next-line no-console
- console.error("DELETE ACTION ERROR:", error);
- return { message: "Unable to delete changelog" };
+ console.error('DELETE ACTION ERROR:', error);
+ return {message: 'Unable to delete changelog', success: false};
}
- revalidateTag("changelogs");
- revalidateTag("changelog-detail");
- return revalidatePath(`/changelog/_admin`);
+ revalidateTag('changelogs');
+ revalidateTag('changelog-detail');
+ revalidatePath(`/changelog/_admin`);
+ return {
+ success: true,
+ };
}
diff --git a/apps/changelog/src/server/actions/serverActionPayload.interface.ts b/apps/changelog/src/server/actions/serverActionPayload.interface.ts
new file mode 100644
index 0000000000000..3622ef4eda8d8
--- /dev/null
+++ b/apps/changelog/src/server/actions/serverActionPayload.interface.ts
@@ -0,0 +1,4 @@
+export interface ServerActionPayloadInterface {
+ message?: string;
+ success?: boolean;
+}