From 90f158542ab48461b50d64f7445570406254edce Mon Sep 17 00:00:00 2001 From: Janis Date: Tue, 27 Dec 2022 04:47:07 +0100 Subject: [PATCH] add category --- app/Nav.tsx | 17 +++- app/articles/DynamicCategoryGrid.tsx | 25 ----- .../[categoryName]/[articleName]/head.tsx | 2 +- .../[categoryName]/[articleName]/page.tsx | 8 +- app/articles/page.tsx | 27 +++++- app/layout.tsx | 10 +- styles/CategoryList.module.scss | 95 ++++++++++++++++++ styles/DynamicCategoryGrid.module.scss | 96 ------------------- 8 files changed, 142 insertions(+), 138 deletions(-) delete mode 100644 app/articles/DynamicCategoryGrid.tsx delete mode 100644 styles/DynamicCategoryGrid.module.scss diff --git a/app/Nav.tsx b/app/Nav.tsx index 7a56316..e53a9d2 100644 --- a/app/Nav.tsx +++ b/app/Nav.tsx @@ -39,7 +39,7 @@ function toggleTheme() { }, 150); } -export default function Nav() { +export default function Nav({ categories }) { useEffect(() => { if (localStorage.getItem("theme") == "dark") { switchTheme("dark"); @@ -57,16 +57,23 @@ export default function Nav() { alt="Nav bar logo" />
- +
Categories
- {" "} All - Tutorials + {categories?.map((cat, i) => { + { + return ( + + {cat?.name} + + ); + } + })}
- +
diff --git a/app/articles/DynamicCategoryGrid.tsx b/app/articles/DynamicCategoryGrid.tsx deleted file mode 100644 index 3ea223a..0000000 --- a/app/articles/DynamicCategoryGrid.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import React from "react"; -import styles from "../../styles/DynamicCategoryGrid.module.scss"; -import Link from "next/link"; -export default function DynamicCategoryGrid({ categories }) { - return ( -
- {categories.map((cat, i) => { - { - return ( -
- -
- - - -
- {cat.name} - -
- ); - } - })} -
- ); -} diff --git a/app/articles/[categoryName]/[articleName]/head.tsx b/app/articles/[categoryName]/[articleName]/head.tsx index 3e0671c..a75deda 100644 --- a/app/articles/[categoryName]/[articleName]/head.tsx +++ b/app/articles/[categoryName]/[articleName]/head.tsx @@ -10,7 +10,7 @@ export default async function Head({ const article: Article = await GetArticle(articleName); return ( <> - {article.title} + {article?.title} ); diff --git a/app/articles/[categoryName]/[articleName]/page.tsx b/app/articles/[categoryName]/[articleName]/page.tsx index 9e0a4ef..b1cf088 100644 --- a/app/articles/[categoryName]/[articleName]/page.tsx +++ b/app/articles/[categoryName]/[articleName]/page.tsx @@ -10,7 +10,7 @@ export async function GetContentTableEntries( article: Article ): Promise { const entries = await prisma.contentTableEntry.findMany({ - where: { article: article }, + where: { articleId: article?.id ?? 1 }, orderBy: { orderIndex: "asc" }, }); @@ -19,7 +19,7 @@ export async function GetContentTableEntries( export async function GetArticle(articleName: string) { const article = await prisma.article.findUnique({ - where: { name: articleName.toLowerCase() }, + where: { name: articleName.toLowerCase() ?? "" }, }); return article; @@ -39,7 +39,7 @@ export default async function Tutorial({ }) { const articleName: string = params.articleName; const article: Article = await GetArticle(articleName); - const markdown: string = article.markdown; + const markdown: string = article?.markdown ?? ""; const contentTableEntries: ContentTableEntry[] = await GetContentTableEntries( article ); @@ -49,7 +49,7 @@ export default async function Tutorial({
-

{article.title}

+

{article?.title}

{ return await prisma.category.findMany(); } -const DynamicCategoryGrid = dynamic(() => import("./DynamicCategoryGrid"), { - loading: () =>

Loading...

, -}); - export default async function CategoryList() { const categories = await GetCategories(); @@ -20,7 +16,28 @@ export default async function CategoryList() {

Overview

- +
+ {categories.map((cat, i) => { + return ( +
+ +
+ + + +
+ {cat.name} + +
+ ); + })} +
); diff --git a/app/layout.tsx b/app/layout.tsx index e5d0794..d31ccdc 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -3,8 +3,14 @@ import "../styles/colorVariables.scss"; import "../styles/variables.scss"; import Nav from "./Nav"; import Footer from "./Footer"; +import { Category } from "@prisma/client"; +import prisma from "../lib/prisma"; -export default function RootLayout({ +export async function GetCategories(): Promise { + return await prisma.category.findMany(); +} + +export default async function RootLayout({ children, }: { children: React.ReactNode; @@ -15,7 +21,7 @@ export default function RootLayout({
-
{children}