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

feat module 1,2,4 : sort child menu items by latest product updated

master
nadia 1 день назад
Родитель
Сommit
a17ea89d5a
3 измененных файлов: 28 добавлений и 3 удалений
  1. 26
    3
      src/config/menuCollections.js
  2. 1
    0
      src/services/ProductService.js
  3. 1
    0
      src/utils/helpers.js

+ 26
- 3
src/config/menuCollections.js Просмотреть файл

@@ -107,7 +107,22 @@ export const getMenuCollectionsByProductType = (products = []) => {
107 107
         menuCollections[productType][menuGroup].set(key, {
108 108
           ...collection,
109 109
           menuGroup,
110
+          latestProductUpdatedAt: product?.updatedAt || product?.createdAt || null,
110 111
         });
112
+      } else {
113
+        const existingCollection = menuCollections[productType][menuGroup].get(key);
114
+        const existingUpdatedAt = existingCollection?.latestProductUpdatedAt
115
+          ? Date.parse(existingCollection.latestProductUpdatedAt)
116
+          : 0;
117
+        const productUpdatedAt = product?.updatedAt || product?.createdAt || null;
118
+        const productUpdatedAtTime = productUpdatedAt ? Date.parse(productUpdatedAt) : 0;
119
+
120
+        if (productUpdatedAtTime > existingUpdatedAt) {
121
+          menuCollections[productType][menuGroup].set(key, {
122
+            ...existingCollection,
123
+            latestProductUpdatedAt: productUpdatedAt,
124
+          });
125
+        }
111 126
       }
112 127
     });
113 128
 
@@ -123,11 +138,19 @@ const mapCollectionToMenuItem = (collection = {}) => ({
123 138
   collection,
124 139
 });
125 140
 
126
-// Utility function to sort collections by their updatedAt timestamp, with a fallback to title comparison
141
+// Utility function to sort collections by their latest product update timestamp, with collection updatedAt and title fallbacks
127 142
 const sortCollectionsByNewest = (collections = []) => (
128 143
   [...collections].sort((a, b) => {
129
-    const aUpdatedAt = a?.updatedAt ? Date.parse(a.updatedAt) : 0;
130
-    const bUpdatedAt = b?.updatedAt ? Date.parse(b.updatedAt) : 0;
144
+    const aUpdatedAt = a?.latestProductUpdatedAt
145
+      ? Date.parse(a.latestProductUpdatedAt)
146
+      : a?.updatedAt
147
+        ? Date.parse(a.updatedAt)
148
+        : 0;
149
+    const bUpdatedAt = b?.latestProductUpdatedAt
150
+      ? Date.parse(b.latestProductUpdatedAt)
151
+      : b?.updatedAt
152
+        ? Date.parse(b.updatedAt)
153
+        : 0;
131 154
 
132 155
     if (aUpdatedAt !== bUpdatedAt) {
133 156
       return bUpdatedAt - aUpdatedAt;

+ 1
- 0
src/services/ProductService.js Просмотреть файл

@@ -23,6 +23,7 @@ const getProducts = async () => {
23 23
           handle
24 24
           title
25 25
           createdAt 
26
+          updatedAt
26 27
           productType
27 28
           tags
28 29
           priceRange {

+ 1
- 0
src/utils/helpers.js Просмотреть файл

@@ -20,6 +20,7 @@ export const formatProductData = (product) => {
20 20
         handle: product?.handle || "",
21 21
         title: product?.title || "",
22 22
         createdAt: publishedDate || product?.createdAt,
23
+        updatedAt: product?.updatedAt || product?.createdAt,
23 24
         collections: product?.collections?.nodes || null,
24 25
         descriptionHtml: product?.descriptionHtml || "",
25 26
         tags:product?.tags || null,

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