index.js
4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { useRouter } from "next/router";
import Breadcrumb from "@/components/Common/Breadcrumb";
import HeadTitle from "@/components/Common/HeadTitle";
import InnerDetailsSubCategory from "@/components/Collection/InnerDetailsSubCategory";
import InnerBannerproduct from "@/components/Collection/InnerBannerproduct";
import AboutInfo from "@/components/Collection/AboutInfo";
import Video from "@/components/Collection/Video";
import Gallery from "@/components/Collection/Gallery";
import Catalogues from "@/container/Home/Catalogues";
import { Contact } from "@/container/Home/Contact";
import CompanyOverview from "@/components/Collection/CompanyOverview";
import TechnicalDetails from "@/components/Collection/TechnicalDetails";
import Explore from "@/components/Collection/Explore";
import {
getCollectionDetailCategoryData,
} from "@/services/collectionDetailCategoryApi";
import { getCollectionSubCategoryData } from "@/services/collectionSubCategoryApi";
import { getCataloguesBySlug } from "@/services/cataloguesApi";
const SubCategoryOrProductPage = ({
type,
products,
productData,
activeSubCategory,
cataloguesData,
}) => {
const router = useRouter();
const { category, subCategory } = router.query;
if (!router.isReady) return null;
/* ======================================================
🟢 IF PRODUCT (2 LEVEL PRODUCT PAGE)
====================================================== */
if (type === "product") {
const subCategoryData = productData.collection_sub_category;
const breadcrumbData = [
{ href: "/collections", label: "Collections" },
{
href: `/collections/${category}`,
label: category.replace(/-/g, " "),
},
{
href: router.asPath,
label: productData.title,
},
];
return (
<>
<Breadcrumb breadcrumbData={breadcrumbData} />
<InnerBannerproduct productData={productData} />
<AboutInfo productData={productData?.aboutInfo} />
{productData?.isDoorAndPartitionsLayouts && <CompanyOverview />}
<Video productData={productData?.video} />
{productData?.isDoorAndPartitionsLayouts && <TechnicalDetails />}
<Gallery productData={productData?.gallery} />
{productData?.isDoorAndPartitionsLayouts && <Explore />}
{!productData?.isDoorAndPartitionsLayouts && (
<Catalogues cataloguesData={cataloguesData} />
)}
<Contact />
</>
);
}
/* ======================================================
🔵 IF SUBCATEGORY (NORMAL 2 LEVEL SUBCATEGORY PAGE)
====================================================== */
if (!products || products.length === 0) {
return <h1>Sub Category not found</h1>;
}
const subCategoryData = products[0].collection_sub_category;
const breadcrumbData = [
{ href: "/", label: "Home" },
{ href: "/collections", label: "Collections" },
{
href: `/collections/${category}`,
label: category.replace(/-/g, " "),
},
{
href: router.asPath,
label: subCategoryData.title,
},
];
return (
<>
<Breadcrumb breadcrumbData={breadcrumbData} />
<HeadTitle categoryData={activeSubCategory} />
<InnerDetailsSubCategory products={products} />
</>
);
};
export default SubCategoryOrProductPage;
/* ======================================================
✅ SERVER SIDE LOGIC
====================================================== */
export async function getServerSideProps({ params }) {
try {
const { subCategory } = params;
/* 1️⃣ Check if this slug is a PRODUCT */
const productCheck = await getCollectionDetailCategoryData(subCategory);
if (productCheck && productCheck.length > 0) {
const cataloguesData = await getCataloguesBySlug();
return {
props: {
type: "product",
productData: productCheck[0],
cataloguesData,
},
};
}
/* 2️⃣ Otherwise treat as SUBCATEGORY */
const categoriesSub = await getCollectionSubCategoryData();
const allProducts = await getCollectionDetailCategoryData();
const products = allProducts.filter(
(item) => item.collection_sub_category?.slug === subCategory
);
const activeSubCategory =
categoriesSub.find((item) => item.slug === subCategory) || null;
return {
props: {
type: "subcategory",
products,
activeSubCategory,
},
};
} catch (error) {
console.error(error);
return {
notFound: true,
};
}
}