Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
feat: add root tag explorer page
Browse files Browse the repository at this point in the history
  • Loading branch information
ziteh committed Sep 17, 2024
1 parent 3eeac32 commit 3b681f1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
20 changes: 20 additions & 0 deletions app/api/tags/root/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NextResponse } from "next/server";
import { prisma } from "@/app/lib/config/prisma";
import { StatusCodes } from "http-status-codes";

export async function GET(_request: Request) {
try {
// Get all root tags (with no parent)
const tags = await prisma.tag.findMany({
where: { parent: null },
});

return NextResponse.json(tags);
} catch (error) {
console.error("Error fetching root tags:", error);
return NextResponse.json(
{ error: "Error fetching root tags" },
{ status: StatusCodes.INTERNAL_SERVER_ERROR }
);
}
}
11 changes: 8 additions & 3 deletions app/components/appLayout/topbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ export default function Topbar(props: Props) {
setTag(data.self);
};

const onBreadcrumbNav = (id: number) => {
const onBreadcrumbNav = (id?: number) => {
if (id === undefined) {
router.push(`/explorer`);
return;
}

updateSelectedTagId(id);
router.push(`/explorer/${id}`);
};
Expand All @@ -114,7 +119,7 @@ export default function Topbar(props: Props) {
<Link
underline="none"
color="inherit"
onClick={() => onBreadcrumbNav(1)}
onClick={() => onBreadcrumbNav()}
>
Hie
</Link>
Expand All @@ -124,7 +129,7 @@ export default function Topbar(props: Props) {
<Link
underline="none"
color="inherit"
onClick={() => onBreadcrumbNav(1)}
onClick={() => onBreadcrumbNav()}
>
<HomeIcon fontSize="small" />
</Link>
Expand Down
52 changes: 52 additions & 0 deletions app/explorer/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use client";

import { useState, useEffect } from "react";
import { Breadcrumbs, Button, Link, Typography } from "@mui/material";
import { Tag, TagRelationChain, SimpleTag } from "@/app/lib/types";
import LocalOfferIcon from "@mui/icons-material/LocalOffer";
import { useTagTreeState } from "@/app/store/tagTree";
import { useRouter } from "next/navigation";

export default function Page() {
const [rootTags, setRootTags] = useState<Tag[]>([]);
const { updateSelectedTagId } = useTagTreeState();

const router = useRouter();

useEffect(() => {
updateTag();
}, []);

const updateTag = async () => {
const response = await fetch(`/api/tags/root`, {
method: "GET",
headers: { "Content-Type": "application/json" },
});

if (!response.ok) {
return;
}

const data: Tag[] = await response.json();
setRootTags(data);
};

const onChildClick = (id: number) => {
updateSelectedTagId(id);
router.push(`/explorer/${id}`);
};

return (
<div>
{rootTags.map((c, i) => (
<Button
key={i}
startIcon={<LocalOfferIcon />}
onClick={() => onChildClick(c.id)}
>
{c.name}
</Button>
))}
</div>
);
}

0 comments on commit 3b681f1

Please sign in to comment.