This commit is contained in:
Janis
2023-02-02 00:00:44 +01:00
parent 585e36e4b9
commit 2f340537d4
14 changed files with 77 additions and 24 deletions

View File

@@ -74,7 +74,6 @@ export default function AdminArticlesEditorPage({ params }: { params: { articleI
async function updateArticle() {
console.log("Update article");
const payload: UpdateArticle = {
id: params.articleId,
title: titleRef.current.value,
introduction: introductionRef.current.value,
markdown: markdown,
@@ -83,7 +82,7 @@ export default function AdminArticlesEditorPage({ params }: { params: { articleI
};
console.log(payload);
await fetch("/api/articles/", {
await fetch(`/api/articles/${params.articleId.toString()}`, {
method: "PUT",
headers: {
Accept: "application/json",

View File

@@ -44,7 +44,6 @@ export default function AdminCategoriesEditor({ params }: { params: { categoryId
async function updateCategory() {
console.log("Update category");
const payload: UpdateCategory = {
id: params.categoryId,
title: titleRef.current.value,
color: colorRef.current.value,
svg: {
@@ -54,7 +53,7 @@ export default function AdminCategoriesEditor({ params }: { params: { categoryId
};
console.log(payload);
await fetch("/api/categories/", {
await fetch(`/api/categories/${params.categoryId.toString()}`, {
method: "PUT",
headers: {
Accept: "application/json",

View File

@@ -5,6 +5,8 @@ import Link from "next/link";
import Footer from "../components/Footer";
import Nav from "../components/Nav";
import { FetchManager } from "../manager/fetchManager";
import AdminNav from "../components/AdminNav";
import AdminControl from "../components/AdminControl";
export default async function RootLayout({ children }: { children: React.ReactNode }) {
return (
@@ -12,11 +14,10 @@ export default async function RootLayout({ children }: { children: React.ReactNo
<head></head>
<body className="body">
<div>
<Link href={"/admin"}> Admin</Link>
</div>
<header>
<Nav categories={await FetchManager.Category.list()} />
<AdminNav />
<AdminControl />
</header>
<main>{children}</main>
<Footer />

View File

@@ -0,0 +1,11 @@
import React from "react";
import styles from "../styles/modules/AdminControl.module.scss";
export default function AdminControl() {
return (
<div className={styles.adminControl}>
<button className="danger">Delete this article</button>
<button className="warning">Edit this article</button>
</div>
);
}

15
components/AdminNav.tsx Normal file
View File

@@ -0,0 +1,15 @@
import React from "react";
import Link from "next/link";
import styles from "../styles/modules/AdminNav.module.scss";
function AdminNav() {
return (
<div className={styles.adminNav}>
<Link href={"/admin"}>Admin</Link>
<Link href={"/admin/articles/editor/0"}>New article</Link>
<Link href={"/admin/categories/editor/0"}>New category</Link>
</div>
);
}
export default AdminNav;

4
package-lock.json generated
View File

@@ -9,7 +9,7 @@
"version": "0.2.0",
"dependencies": {
"@next/font": "13.0.7",
"@prisma/client": "^4.8.0",
"@prisma/client": "^4.9.0",
"@types/express": "^4.17.15",
"@types/marked": "^4.0.8",
"@types/react": "18.0.26",
@@ -42,7 +42,7 @@
"@fec/remark-a11y-emoji": "^3.1.0",
"@types/node": "^18.11.17",
"@types/prismjs": "^1.26.0",
"prisma": "^4.8.0"
"prisma": "^4.9.0"
}
},
"node_modules/@ampproject/remapping": {

View File

@@ -13,7 +13,7 @@
},
"dependencies": {
"@next/font": "13.0.7",
"@prisma/client": "^4.8.0",
"@prisma/client": "^4.9.0",
"@types/express": "^4.17.15",
"@types/marked": "^4.0.8",
"@types/react": "18.0.26",
@@ -46,6 +46,6 @@
"@fec/remark-a11y-emoji": "^3.1.0",
"@types/node": "^18.11.17",
"@types/prismjs": "^1.26.0",
"prisma": "^4.8.0"
"prisma": "^4.9.0"
}
}

View File

@@ -35,6 +35,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
res.status(500).json(error);
});
} else if (req.method == "PUT") {//* PUT
console.log("PUT")
const data: UpdateArticle = req.body;
if (!isValidText(data.title)) {
@@ -53,22 +54,23 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
const newArticle: Prisma.ArticleUncheckedUpdateInput = {
title: data.title,
name: formatTextToUrlName(data.title),
introduction: data.introduction,
title: data.title ?? undefined,
name: formatTextToUrlName(data.title) ?? undefined,
introduction: data.introduction ?? undefined,
categoryId: data.categoryId.toString(),
contentTable: data.contentTable,
markdown: data.markdown,
imageId: data.imageId.toString(),
categoryId: data.categoryId?.toString() ?? undefined,
contentTable: data.contentTable ?? undefined,
markdown: data.markdown ?? undefined,
imageId: data.imageId?.toString() ?? undefined,
}
console.log(newArticle)
await prisma.article.update({ data: newArticle, where: { id: articleId }, include: { category: true } })
.then(
(data) => {
res.json({ success: true, data: data });
},
(errorReason) => {
console.log(errorReason)
if (errorReason.code === "P2002") {
res.json({ target: errorReason.meta.target[0], error: "Already exists" });
}

View File

@@ -72,6 +72,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
data.name = formatTextToUrlName(data.title);
data.categoryId = data.categoryId.toString();
prisma.article
.create({ data: data, include: { category: true } })
.then(

View File

@@ -22,7 +22,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
code: "404",
message: "No article with this name found!",
};
res.status(404).send(JSON.stringify(error));
res.status(404).json(error);
}
})
.catch((err) => {
@@ -31,6 +31,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
code: "500",
message: err,
};
res.status(500).send(JSON.stringify(error));
res.status(500).json(error);
});
}

View File

@@ -17,8 +17,8 @@ button {
border-color: var(--color-accent);
}
&.error {
border-color: var(--color-error);
&.danger {
border-color: var(--color-danger);
}
&.success {
border-color: var(--color-success);

View File

@@ -0,0 +1,12 @@
@import "../variables.scss";
.adminControl {
border-bottom: 2px solid var(--color-danger);
padding: 10px;
display: flex;
justify-content: center;
align-items: center;
column-gap: 50px;
margin-top: -10px;
margin-bottom: 10px;
}

View File

@@ -0,0 +1,12 @@
@import "../variables.scss";
.adminNav {
border-top: 2px solid var(--color-danger);
padding: 10px;
display: flex;
justify-content: center;
align-items: center;
column-gap: 50px;
margin-top: -10px;
margin-bottom: 10px;
}

View File

@@ -11,7 +11,7 @@
--color-font-muted: #929292;
--color-shadow-nav: #00000033;
--color-error: #cf000f;
--color-danger: #cf000f;
--color-success: #009944;
--color-info: #63c0df;
--color-warning: #f0541e;