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

Implement collection sorting by priority in Navbar component

master
Amirul Anwar 14 часов назад
Родитель
Сommit
57fc5be021
1 измененных файлов: 56 добавлений и 4 удалений
  1. 56
    4
      src/components/Navbar/Navbar.jsx

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

@@ -65,6 +65,56 @@ const LanguageSelectItem = styled(MenuItem)(() => ({
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 118
 function mapProductTypesWithCollections(products) {
69 119
   // Create a Map to group collections by productType
70 120
   const productTypeMap = new Map();
@@ -100,7 +150,10 @@ function getUniqueCollections(data) {
100 150
     // Convert the Map back to an array
101 151
     return {
102 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,14 +319,13 @@ const Navbar = () => {
266 319
 
267 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 324
     setDisplayCollection([])
273 325
     setTimeout(() => {
274 326
       setDisplayCollection({
275 327
         productType,
276
-        list: collection
328
+        list: sortedCollection
277 329
       })
278 330
     }, 100);
279 331
   }

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