Skip to content

Commit e5c5078

Browse files
authored
Merge pull request #44 from sliitsesc/feat/7-setup-api-connection
feat: categoies dynamic page
2 parents 0bd5347 + 84a21a9 commit e5c5078

File tree

7 files changed

+38
-93
lines changed

7 files changed

+38
-93
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/[category]/page.tsx

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,33 @@
1-
import fs from "fs/promises";
2-
import path from "path";
1+
import { API_URL } from "@/config";
2+
import { API_ENDPOINTS } from "@/config/endpoints";
3+
import CardsSection from "../components/CardsSection";
34

45
export default async function Page({
56
params,
67
}: {
78
params: { category: string };
89
}) {
9-
const categorySlug = params.category;
10+
const categorySlug = (await params).category;
11+
const data = await fetch(
12+
`${API_URL}${API_ENDPOINTS.CATEGORIES}?populate=subcategories&filters[slug][$eq]=${categorySlug}`
13+
);
1014

11-
// Correct path to the categories.json file
12-
const filePath = path.join(process.cwd(), "src", "data", "categories.json");
15+
const categoryData = await data.json();
1316

14-
try {
15-
// Read and parse the JSON file
16-
const fileData = await fs.readFile(filePath, "utf-8");
17-
const parsedData = JSON.parse(fileData);
18-
19-
// Access the `category` array inside the parsed JSON
20-
const categories = parsedData.category;
21-
22-
// Log the parsed data for debugging
23-
console.log("Categories Data:", categories);
24-
25-
// Validate that categories is an array
26-
if (!Array.isArray(categories)) {
27-
throw new Error("Parsed 'category' is not an array");
28-
}
29-
30-
// Find the category with the matching slug
31-
const category = categories.find((cat) => cat.slug === categorySlug);
32-
33-
// Handle the case where no matching slug is found
34-
if (!category) {
35-
return (
36-
<section className="flex flex-col items-center justify-center min-h-[60vh] bg-gradient-to-b from-blue-100 to-white text-center px-4">
37-
<h1 className="text-4xl font-bold text-gray-800 mb-4">
38-
Category Not Found
39-
</h1>
40-
{/* <p className="text-lg text-gray-600 max-w-2xl">
41-
The category you are looking for does not exist.
42-
</p> */}
43-
</section>
44-
);
45-
}
46-
47-
// Return the title and description
48-
return (
49-
<section className="flex flex-col items-center justify-center h-[400px] bg-gradient-to-b from-blue-100 to-white text-center px-4">
50-
<h1 className="text-4xl font-bold text-gray-800 mb-4">
51-
{category.title}
52-
</h1>
53-
<p className="text-lg text-gray-600 max-w-2xl">
54-
{category.description}
55-
</p>
56-
</section>
57-
);
58-
} catch (error) {
59-
// Handle errors (e.g., file not found, invalid JSON)
60-
console.error("Error reading or parsing categories.json:", error);
61-
return (
62-
<section className="flex flex-col items-center justify-center min-h-[60vh] bg-gradient-to-b from-blue-100 to-white text-center px-4">
17+
return (
18+
<>
19+
<section className="flex flex-col items-center justify-center h-[400px] bg-gradient-to-r from-blue-100 to-white text-center px-4">
6320
<h1 className="text-4xl font-bold text-gray-800 mb-4">
64-
Error Loading Category Data
21+
{categoryData?.data[0]?.name}
6522
</h1>
6623
<p className="text-lg text-gray-600 max-w-2xl">
67-
There was an error while loading the category data. Please try again
68-
later.
24+
{/* {category.description} */}
6925
</p>
7026
</section>
71-
);
72-
}
27+
<CardsSection
28+
categorySlug={categoryData?.data[0]?.slug}
29+
subcategories={categoryData?.data[0]?.subcategories}
30+
/>
31+
</>
32+
);
7333
}
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
import React from "react";
2-
// import { ArrowRightIcon } from "@heroicons/react/24/outline";
3-
4-
const CardsSection = () => {
5-
const cardTitles = [
6-
"Treatment Info",
7-
"Symptoms",
8-
"Precautions",
9-
"Diet and Nutrition",
10-
"Side Effects",
11-
"Other",
12-
];
2+
import { ArrowRightIcon } from "@heroicons/react/24/outline";
3+
import Link from "next/link";
134

5+
const CardsSection = (props) => {
6+
console.log(props);
147
return (
158
<section className="bg-white py-16">
169
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4 sm:gap-10 px-4 sm:px-0 max-w-2xl mx-auto">
17-
{cardTitles.map((title, index) => (
18-
<div
10+
{props.subcategories?.map((item, index) => (
11+
<Link
12+
href={`/${props.categorySlug}/${item.slug}`}
1913
key={index}
2014
className="group relative border-[#DBEBFB] border sm:border-2 bg-white rounded-xl sm:rounded-lg shadow-sm
2115
p-4 sm:p-6 sm:pt-4 sm:pb-16 w-full sm:w-72 cursor-pointer transition-all duration-300
2216
hover:bg-[#2F7CC4] mx-auto">
2317
<h2 className="text-gray-700 font-semibold text-xl sm:text-2xl group-hover:text-white transition-colors duration-300">
24-
{title}
18+
{item.name}
2519
</h2>
2620
<div className="absolute right-4 sm:right-6 top-1/2 -translate-y-1/2 sm:top-auto sm:bottom-4 sm:translate-y-0">
2721
<div className="bg-blue-500 p-2 rounded-lg arrow-button transition-all duration-300 group-hover:bg-white">
28-
{/* <ArrowRightIcon className="h-5 w-5 text-white group-hover:text-[#2F7CC4] transition-colors duration-300" /> */}
22+
<ArrowRightIcon className="h-5 w-5 text-white group-hover:text-[#2F7CC4] transition-colors duration-300" />
2923
</div>
3024
</div>
31-
</div>
25+
</Link>
3226
))}
3327
</div>
3428
</section>
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const Footer = () => {
1717
};
1818

1919
return (
20-
<section className="bg-gradient-to-b to-blue-100 from-white py-16 text-black">
20+
<section className="bg-gradient-to-b to-blue-100 from-white py-16 text-black border-t-[1px] border-[#DBEBFB]">
2121
{/* Mobile View */}
2222
<div className="md:hidden flex flex-col mx-auto px-5">
2323
{/* RheumaConnect Brand Name */}
@@ -28,8 +28,7 @@ const Footer = () => {
2828
{/* Link 1 Dropdown */}
2929
<button
3030
onClick={() => toggleDropdown("link1")}
31-
className="bg-white rounded-lg shadow-md px-5 py-3 text-left text-black mb-3 flex justify-between items-center"
32-
>
31+
className="bg-white rounded-lg shadow-md px-5 py-3 text-left text-black mb-3 flex justify-between items-center">
3332
Link 1
3433
{isOpen.link1 ? (
3534
<ChevronUpIcon className="h-6 w-6 text-gray-500" />
@@ -38,7 +37,7 @@ const Footer = () => {
3837
)}
3938
</button>
4039
{isOpen.link1 && (
41-
<ul className="bg-white p-3 rounded-lg">
40+
<ul className="bg-white p-3 rounded-lg mb-4">
4241
<li>
4342
<a href="#">Tincidunt amet</a>
4443
</li>
@@ -60,8 +59,7 @@ const Footer = () => {
6059
{/* Link 2 Dropdown */}
6160
<button
6261
onClick={() => toggleDropdown("link2")}
63-
className="bg-white rounded-lg shadow-md px-5 py-3 text-left text-black mb-3 flex justify-between items-center"
64-
>
62+
className="bg-white rounded-lg shadow-md px-5 py-3 text-left text-black mb-3 flex justify-between items-center">
6563
Link 2
6664
{isOpen.link2 ? (
6765
<ChevronUpIcon className="h-6 w-6 text-gray-500" />
@@ -92,8 +90,7 @@ const Footer = () => {
9290
{/* Contact Dropdown */}
9391
<button
9492
onClick={() => toggleDropdown("contact")}
95-
className="bg-white rounded-lg shadow-md px-5 py-3 text-left text-black mb-3 flex justify-between items-center"
96-
>
93+
className="bg-white rounded-lg shadow-md px-5 py-3 text-left text-black mb-3 flex justify-between items-center">
9794
Contact
9895
{isOpen.contact ? (
9996
<ChevronUpIcon className="h-6 w-6 text-gray-500" />

src/app/components/MainCategorySection.jsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// // This is the MainCatSection component which is responsible for rendering the main categories section of the website. It uses the categoriesData.json file to get the data for the categories.
2-
// // Path of the data json file: src\app\data\MainCatSection\categoriesData.json
3-
41
import React from "react";
52
import Image from "next/image";
63
import categoriesData from "../data/MainCategorySection/categoriesData.json";
@@ -18,8 +15,7 @@ const MainCategorySection = () => {
1815
{categoriesData.map((category) => (
1916
<div
2017
key={category.id}
21-
className="p-8 cursor-pointer border-2 border-transparent hover:border-[#DBEBFB] rounded-xl transition duration-500"
22-
>
18+
className="p-8 cursor-pointer border-2 border-transparent hover:border-[#DBEBFB] rounded-xl transition duration-500">
2319
<div className="relative max-w-[274px] max-h-[103px] mx-auto aspect-[274/103]">
2420
<Image
2521
src={category.image}

src/app/layout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "@fontsource/dm-sans";
55
import { API_ENDPOINTS } from "@/config/endpoints";
66
import { API_URL } from "@/config";
77
import { DM_Sans } from "next/font/google";
8+
import Footer from "./components/Footer";
89

910
const dmSans = DM_Sans({
1011
subsets: ["latin"],
@@ -26,11 +27,11 @@ export default async function RootLayout({
2627
const navbarData = await data.json();
2728

2829
return (
29-
3030
<html lang="en" className={dmSans.variable}>
3131
<body className="antialiased font-sans">
3232
<Navbar data={navbarData?.data} />
3333
<main>{children}</main>
34+
<Footer />
3435
</body>
3536
</html>
3637
);

src/app/page.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import HeroSection from "./components/HeroSection";
2-
import CardsSection from "./components/CardsSection";
32
import AZCards from "@/app/components/AZCards";
43
import translations from "@/app/data/translations.json";
54
import { Translations } from "@/app/types";
65
// import ContactCard from "./components/ContactCard";
76
import MainCategorySection from "./components/MainCategorySection";
8-
import Footer from "./components/Footer";
97
const translationsData: Translations = translations;
108

119
// fetch data from API
@@ -14,12 +12,10 @@ export default async function Home() {
1412
return (
1513
<div>
1614
<HeroSection selectedLang="en" />
17-
<CardsSection />
1815
{/* <ContactCard /> */}
1916
<MainCategorySection />
2017
{/* <ContactCard /> */}
2118
<AZCards azCards={translationsData["en"]["az-cards"]} />
22-
<Footer />
2319
</div>
2420
);
2521
}

0 commit comments

Comments
 (0)