Explorar el Código

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

master
nadia hace 1 día
padre
commit
563248e2f7
Se han modificado 1 ficheros con 18 adiciones y 19 borrados
  1. 18
    19
      src/config/menuCollections.js

+ 18
- 19
src/config/menuCollections.js Ver fichero

22
       {
22
       {
23
         label: "Casual",
23
         label: "Casual",
24
         menuGroup: "casual",
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
         children: [],
25
         children: [],
31
       },
26
       },
32
       {
27
       {
33
         label: "Traditional",
28
         label: "Traditional",
34
         menuGroup: "traditional",
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
         children: [],
30
         children: [],
42
       },
31
       },
43
     ],
32
     ],
46
     productType: "BEAUTY",
35
     productType: "BEAUTY",
47
     label: "Essentials",
36
     label: "Essentials",
48
     menuGroup: "essentials",
37
     menuGroup: "essentials",
49
-    // children: [
50
-    //   { label: "Cosmetics", titles: ["COSMETICS"] },
51
-    //   { label: "Hand & Body Lotion", titles: ["HAND & BODY LOTION"] },
52
-    // ],
53
     children: [],
38
     children: [],
54
   },
39
   },
55
 ];
40
 ];
138
   collection,
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
 // Utility function to build the navigation menu from products
140
 // Utility function to build the navigation menu from products
142
 export const buildNavMenuFromProducts = (products = []) => {
141
 export const buildNavMenuFromProducts = (products = []) => {
143
   const menuCollections = getMenuCollectionsByProductType(products);
142
   const menuCollections = getMenuCollectionsByProductType(products);
149
         groups: menu.groups
148
         groups: menu.groups
150
           .map((group) => ({
149
           .map((group) => ({
151
             ...group,
150
             ...group,
152
-            children: Array.from(
151
+            children: sortCollectionsByNewest(Array.from(
153
               menuCollections?.[menu.productType]?.[group.menuGroup]?.values?.() || []
152
               menuCollections?.[menu.productType]?.[group.menuGroup]?.values?.() || []
154
-            ).map(mapCollectionToMenuItem),
153
+            )).map(mapCollectionToMenuItem),
155
           }))
154
           }))
156
           .filter((group) => group.children.length > 0),
155
           .filter((group) => group.children.length > 0),
157
       };
156
       };
160
     if (menu.menuGroup) {
159
     if (menu.menuGroup) {
161
       return {
160
       return {
162
         ...menu,
161
         ...menu,
163
-        children: Array.from(
162
+        children: sortCollectionsByNewest(Array.from(
164
           menuCollections?.[menu.productType]?.[menu.menuGroup]?.values?.() || []
163
           menuCollections?.[menu.productType]?.[menu.menuGroup]?.values?.() || []
165
-        ).map(mapCollectionToMenuItem),
164
+        )).map(mapCollectionToMenuItem),
166
       };
165
       };
167
     }
166
     }
168
 
167
 

Loading…
Cancelar
Guardar