Skip to content

Commit 10687e7

Browse files
committed
render categories on resource page and read from json file
1 parent 4a76db6 commit 10687e7

5 files changed

Lines changed: 116 additions & 0 deletions

File tree

src/data/resources/css.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"pageName": "CSS Resources",
3+
"entries": [
4+
{
5+
"title": "CSS Official Docs",
6+
"url": "https://developer.mozilla.org/en-US/docs/Web/CSS",
7+
"tags": ["css", "official docs"]
8+
},
9+
{
10+
"title": "Tailwind Official Docs",
11+
"url": "https://tailwindcss.com",
12+
"tags": ["css", "official docs", "tailwind"]
13+
},
14+
{
15+
"title": "CSS Flexbox Layout Guide",
16+
"url": "https://css-tricks.com/snippets/css/a-guide-to-flexbox/",
17+
"tags": ["css", "cheatsheet"]
18+
}
19+
]
20+
}

src/main.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import "./index.css";
55
import Layout from "./components/Layout";
66
import Home from "./routes/Home";
77
import About from "./routes/About";
8+
import Resources from "./routes/Resources";
9+
import ResourcePage from "./routes/Resources/ResourcePage";
810

911
const root = document.getElementById("root");
1012

@@ -16,6 +18,8 @@ if (root) {
1618
<Route element={<Layout />}>
1719
<Route index element={<Home />} />
1820
<Route path="/about" element={<About />} />
21+
<Route path="/resources" element={<Resources />} />
22+
<Route path="/resources/:category" element={<ResourcePage />} />
1923
</Route>
2024
</Routes>
2125
</BrowserRouter>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Link } from "react-router";
2+
3+
interface Props {
4+
name: string;
5+
slug: string;
6+
}
7+
8+
const CategoryCard = ({ name, slug }: Props) => {
9+
return (
10+
<>
11+
<Link
12+
to={slug}
13+
className="my-4 min-w-[50%] rounded-lg border-2 border-primary px-6 py-12 text-center text-2xl shadow-lg shadow-primary hover:bg-primary hover:text-accent hover:shadow-2xl hover:shadow-primary"
14+
>
15+
{name}
16+
</Link>
17+
</>
18+
);
19+
};
20+
21+
export default CategoryCard;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { useEffect, useState } from "react";
2+
import { useParams } from "react-router";
3+
4+
interface JsonData {
5+
pageName: string;
6+
entries: Entry[];
7+
}
8+
9+
interface Entry {
10+
title: string;
11+
url: string;
12+
tags: string[];
13+
}
14+
15+
const ResourcePage = () => {
16+
const { category } = useParams();
17+
const [data, setData] = useState<JsonData | null>(null);
18+
19+
useEffect(() => {
20+
const fetchData = async () => {
21+
if (category) {
22+
const dataObject = (await import(
23+
`../../data/resources/${category}.json`
24+
)) as { default: JsonData };
25+
return dataObject.default;
26+
} else {
27+
return { pageName: "", entries: [] };
28+
}
29+
};
30+
31+
fetchData().then(
32+
(json) => {
33+
setData(json);
34+
},
35+
() => {
36+
setData({ pageName: "", entries: [] });
37+
},
38+
);
39+
}, [category]);
40+
41+
if (data) {
42+
return (
43+
<>
44+
<section className="h-2/3 bg-background">
45+
<h3 className="text-xl text-accent">{data.pageName}</h3>
46+
{data.entries.map((e) => e.title)}
47+
</section>
48+
</>
49+
);
50+
}
51+
};
52+
export default ResourcePage;

src/routes/Resources/index.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import CategoryCard from "./CategoryCard";
2+
3+
const Resources = () => (
4+
<main className="flex h-full w-full flex-col">
5+
<section className="flex h-full flex-col justify-center">
6+
<h3 className="my-12 text-center text-3xl text-secondary">Resources</h3>
7+
<div className="grid w-full grid-cols-3 place-items-center gap-y-6 bg-background">
8+
<CategoryCard name="HTML" slug="html" />
9+
<CategoryCard name="CSS" slug="css" />
10+
<CategoryCard name="JavaScript" slug="javascript" />
11+
<CategoryCard name="Python" slug="python" />
12+
<CategoryCard name="Java" slug="java" />
13+
<CategoryCard name="C++" slug="c-plus-plus" />
14+
</div>
15+
</section>
16+
</main>
17+
);
18+
19+
export default Resources;

0 commit comments

Comments
 (0)