Browse Source

feat module 1,2,4 : sort child menu items by newest collection / latest update collection

master
nadia 1 day ago
parent
commit
563248e2f7
1 changed files with 18 additions and 19 deletions
  1. 18
    19
      src/config/menuCollections.js

+ 18
- 19
src/config/menuCollections.js View File

@@ -22,22 +22,11 @@ export const NAV_MENU_STRUCTURE = [
22 22
       {
23 23
         label: "Casual",
24 24
         menuGroup: "casual",
25
-        // children: [
26
-        //   { label: "ND X Marii for Amber", titles: ["DENIM ND X MARII FOR AMBER", "ND X MARII FOR AMBER"] },
27
-        //   { label: "Somewhere Somehow Someone", titles: ["SOMEWHERE SOMEHOW SOMEONE"] },
28
-        //   { label: "Flower Power", titles: ["FLOWER POWER"] },
29
-        // ],
30 25
         children: [],
31 26
       },
32 27
       {
33 28
         label: "Traditional",
34 29
         menuGroup: "traditional",
35
-        // children: [
36
-        //   { label: "Eid's Time For Love", titles: ["EID'S TIME FOR LOVE"] },
37
-        //   { label: "Raya Romantics", titles: ["RAYA ROMANTICS", "RAYA ROMANTICS COLLECTION 2025"] },
38
-        //   { label: "Atma Sari", titles: ["ATMA SARI"] },
39
-        //   { label: "Oasis abaya", titles: ["OASIS ABAYA COLLECTION", "MIRAGE COLLECTION"] },
40
-        // ],
41 30
         children: [],
42 31
       },
43 32
     ],
@@ -46,10 +35,6 @@ export const NAV_MENU_STRUCTURE = [
46 35
     productType: "BEAUTY",
47 36
     label: "Essentials",
48 37
     menuGroup: "essentials",
49
-    // children: [
50
-    //   { label: "Cosmetics", titles: ["COSMETICS"] },
51
-    //   { label: "Hand & Body Lotion", titles: ["HAND & BODY LOTION"] },
52
-    // ],
53 38
     children: [],
54 39
   },
55 40
 ];
@@ -138,6 +123,20 @@ const mapCollectionToMenuItem = (collection = {}) => ({
138 123
   collection,
139 124
 });
140 125
 
126
+// Utility function to sort collections by their updatedAt timestamp, with a fallback to title comparison
127
+const sortCollectionsByNewest = (collections = []) => (
128
+  [...collections].sort((a, b) => {
129
+    const aUpdatedAt = a?.updatedAt ? Date.parse(a.updatedAt) : 0;
130
+    const bUpdatedAt = b?.updatedAt ? Date.parse(b.updatedAt) : 0;
131
+
132
+    if (aUpdatedAt !== bUpdatedAt) {
133
+      return bUpdatedAt - aUpdatedAt;
134
+    }
135
+
136
+    return normalizeTitle(a?.title || "").localeCompare(normalizeTitle(b?.title || ""));
137
+  })
138
+);
139
+
141 140
 // Utility function to build the navigation menu from products
142 141
 export const buildNavMenuFromProducts = (products = []) => {
143 142
   const menuCollections = getMenuCollectionsByProductType(products);
@@ -149,9 +148,9 @@ export const buildNavMenuFromProducts = (products = []) => {
149 148
         groups: menu.groups
150 149
           .map((group) => ({
151 150
             ...group,
152
-            children: Array.from(
151
+            children: sortCollectionsByNewest(Array.from(
153 152
               menuCollections?.[menu.productType]?.[group.menuGroup]?.values?.() || []
154
-            ).map(mapCollectionToMenuItem),
153
+            )).map(mapCollectionToMenuItem),
155 154
           }))
156 155
           .filter((group) => group.children.length > 0),
157 156
       };
@@ -160,9 +159,9 @@ export const buildNavMenuFromProducts = (products = []) => {
160 159
     if (menu.menuGroup) {
161 160
       return {
162 161
         ...menu,
163
-        children: Array.from(
162
+        children: sortCollectionsByNewest(Array.from(
164 163
           menuCollections?.[menu.productType]?.[menu.menuGroup]?.values?.() || []
165
-        ).map(mapCollectionToMenuItem),
164
+        )).map(mapCollectionToMenuItem),
166 165
       };
167 166
     }
168 167
 

Loading…
Cancel
Save