Просмотр исходного кода

feat module 1,2,4 : change to dynamic navigation menu based on shopify collection - navbar

master
nadia 1 день назад
Родитель
Сommit
5a0f495409
1 измененных файлов: 17 добавлений и 7 удалений
  1. 17
    7
      src/components/Navbar/Navbar.jsx

+ 17
- 7
src/components/Navbar/Navbar.jsx Просмотреть файл

29
 
29
 
30
 import ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
30
 import ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
31
 import KeyboardArrowRightIcon from '@mui/icons-material/KeyboardArrowRight';
31
 import KeyboardArrowRightIcon from '@mui/icons-material/KeyboardArrowRight';
32
-import { NAV_MENU_STRUCTURE as SHARED_NAV_MENU_STRUCTURE } from "../../config/menuCollections";
32
+import {
33
+  NAV_MENU_STRUCTURE as SHARED_NAV_MENU_STRUCTURE,
34
+  buildNavMenuFromProducts,
35
+  normalizeTitle,
36
+} from "../../config/menuCollections";
33
 
37
 
34
 function calculateTotalQuantity(cartItems) {
38
 function calculateTotalQuantity(cartItems) {
35
   return cartItems.reduce((total, item) => total + item.quantity, 0);
39
   return cartItems.reduce((total, item) => total + item.quantity, 0);
84
   ],
88
   ],
85
 };
89
 };
86
 
90
 
87
-const normalizeTitle = (value = "") => value.trim().toUpperCase();
88
-
89
 const getProductTypeMenuLabel = (productType = "") => {
91
 const getProductTypeMenuLabel = (productType = "") => {
90
   return normalizeTitle(productType) === "BEAUTY" ? "Essentials" : productType;
92
   return normalizeTitle(productType) === "BEAUTY" ? "Essentials" : productType;
91
 };
93
 };
192
   const [activeMenu, setActiveMenu] = useState(null);
194
   const [activeMenu, setActiveMenu] = useState(null);
193
   const [activeGroup, setActiveGroup] = useState(null);
195
   const [activeGroup, setActiveGroup] = useState(null);
194
   const [dropdownTopOffset, setDropdownTopOffset] = useState(52);
196
   const [dropdownTopOffset, setDropdownTopOffset] = useState(52);
197
+  const dynamicNavMenu = products?.length > 0
198
+    ? buildNavMenuFromProducts(products)
199
+    : NAV_MENU_STRUCTURE;
195
 
200
 
196
   const [displayCollection, setDisplayCollection] = useState({
201
   const [displayCollection, setDisplayCollection] = useState({
197
     productType: null,
202
     productType: null,
394
 
399
 
395
   const findCollectionForMenuItem = (productType, item) => {
400
   const findCollectionForMenuItem = (productType, item) => {
396
     const targetTitles = (item?.titles || []).map(normalizeTitle);
401
     const targetTitles = (item?.titles || []).map(normalizeTitle);
402
+    const targetHandles = (item?.handles || []).map(normalizeTitle);
397
 
403
 
398
     return products
404
     return products
399
       .filter((product) => product.productType === productType)
405
       .filter((product) => product.productType === productType)
400
       .flatMap((product) => product.collections || [])
406
       .flatMap((product) => product.collections || [])
401
-      .find((collection) => targetTitles.includes(normalizeTitle(collection?.title || "")));
407
+      .find((collection) =>
408
+        targetTitles.includes(normalizeTitle(collection?.title || "")) ||
409
+        targetHandles.includes(normalizeTitle(collection?.handle || ""))
410
+      );
402
   }
411
   }
403
 
412
 
404
   const getMenuCollectionTitles = (productType) => {
413
   const getMenuCollectionTitles = (productType) => {
405
-    const menu = NAV_MENU_STRUCTURE.find((menuItem) => menuItem.productType === productType);
414
+    const menu = dynamicNavMenu.find((menuItem) => menuItem.productType === productType);
406
 
415
 
407
     if (!menu) return [];
416
     if (!menu) return [];
408
 
417
 
545
               }}
554
               }}
546
             >
555
             >
547
 
556
 
548
-              {NAV_MENU_STRUCTURE.map((menu) => (
557
+              {/* Navigation Menu Items */}
558
+              {dynamicNavMenu.map((menu) => (
549
                 <Box
559
                 <Box
550
                   key={menu.label}
560
                   key={menu.label}
551
                   ref={(node) => {
561
                   ref={(node) => {
984
       </AppBar>
994
       </AppBar>
985
       {showSearch && <SearchProduct onClose={() => { setShowSearch(false) }} />}
995
       {showSearch && <SearchProduct onClose={() => { setShowSearch(false) }} />}
986
 
996
 
987
-      <MobileNav open={openSideMenu} menu={NAV_MENU_STRUCTURE} products={products} infomenu={navItemCompanyInfo} onClose={() => { setOpenSideMenu(false) }} />
997
+      <MobileNav open={openSideMenu} menu={dynamicNavMenu} products={products} infomenu={navItemCompanyInfo} onClose={() => { setOpenSideMenu(false) }} />
988
       <SideCart open={openSideCart} onClose={() => { setOpenSideCart(false) }} />
998
       <SideCart open={openSideCart} onClose={() => { setOpenSideCart(false) }} />
989
 
999
 
990
     </>
1000
     </>

Загрузка…
Отмена
Сохранить