Explorar el Código

Implement collection sorting by priority in Navbar component

master
Amirul Anwar hace 14 horas
padre
commit
57fc5be021
Se han modificado 1 ficheros con 56 adiciones y 4 borrados
  1. 56
    4
      src/components/Navbar/Navbar.jsx

+ 56
- 4
src/components/Navbar/Navbar.jsx Ver fichero

65
   },
65
   },
66
 }));
66
 }));
67
 
67
 
68
+const COLLECTION_PRIORITY_BY_TYPE = {
69
+  apparel: [
70
+    "EID'S TIME FOR LOVE COLLECTION",
71
+    "ND X MARII FOR AMBER",
72
+    "MIRAGE COLLECTION",
73
+    "SOMEWHERE SOMEHOW SOMEONE",
74
+    "OASIS ABAYA COLLECTION",
75
+    "RAYA ROMANTICS COLLECTION 2025",
76
+    "CASUAL WEAR",
77
+    "AINA ABDUL MERCHANDISE",
78
+    "ATMA SARI",
79
+    "FLOWER POWER",
80
+    "AMBER SCARVES FLOWER POWER SERIES",
81
+  ],
82
+};
83
+
84
+const normalizeTitle = (value = "") => value.trim().toUpperCase();
85
+
86
+const sortCollectionsByPriority = (collection = [], productType = "") => {
87
+  const typeKey = (productType || "").trim().toLowerCase();
88
+  const priorityList = COLLECTION_PRIORITY_BY_TYPE[typeKey] || [];
89
+  const priorityMap = new Map(
90
+    priorityList.map((title, index) => [normalizeTitle(title), index])
91
+  );
92
+
93
+  return [...collection].sort((a, b) => {
94
+    const aTitle = normalizeTitle(a?.title || "");
95
+    const bTitle = normalizeTitle(b?.title || "");
96
+    const aPriority = priorityMap.has(aTitle)
97
+      ? priorityMap.get(aTitle)
98
+      : Number.MAX_SAFE_INTEGER;
99
+    const bPriority = priorityMap.has(bTitle)
100
+      ? priorityMap.get(bTitle)
101
+      : Number.MAX_SAFE_INTEGER;
102
+
103
+    if (aPriority !== bPriority) {
104
+      return aPriority - bPriority;
105
+    }
106
+
107
+    const aUpdatedAt = a?.updatedAt ? Date.parse(a.updatedAt) : 0;
108
+    const bUpdatedAt = b?.updatedAt ? Date.parse(b.updatedAt) : 0;
109
+
110
+    if (aUpdatedAt !== bUpdatedAt) {
111
+      return bUpdatedAt - aUpdatedAt;
112
+    }
113
+
114
+    return aTitle.localeCompare(bTitle);
115
+  });
116
+};
117
+
68
 function mapProductTypesWithCollections(products) {
118
 function mapProductTypesWithCollections(products) {
69
   // Create a Map to group collections by productType
119
   // Create a Map to group collections by productType
70
   const productTypeMap = new Map();
120
   const productTypeMap = new Map();
100
     // Convert the Map back to an array
150
     // Convert the Map back to an array
101
     return {
151
     return {
102
       productType: item.productType,
152
       productType: item.productType,
103
-      collection: Array.from(uniqueCollectionsMap.values())
153
+      collection: sortCollectionsByPriority(
154
+        Array.from(uniqueCollectionsMap.values()),
155
+        item.productType
156
+      )
104
     };
157
     };
105
   });
158
   });
106
 }
159
 }
266
 
319
 
267
   const displayCollectionList = (collection = [], productType) => {
320
   const displayCollectionList = (collection = [], productType) => {
268
 
321
 
269
-    
270
-    collection = collection.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt) )
322
+    const sortedCollection = sortCollectionsByPriority(collection, productType);
271
 
323
 
272
     setDisplayCollection([])
324
     setDisplayCollection([])
273
     setTimeout(() => {
325
     setTimeout(() => {
274
       setDisplayCollection({
326
       setDisplayCollection({
275
         productType,
327
         productType,
276
-        list: collection
328
+        list: sortedCollection
277
       })
329
       })
278
     }, 100);
330
     }, 100);
279
   }
331
   }

Loading…
Cancelar
Guardar