浏览代码

feat module 6, 7 : remove sold out item or item not in menu from recently view in product page

master
nadia 1 天前
父节点
当前提交
b85afa18c9
共有 1 个文件被更改,包括 23 次插入1 次删除
  1. 23
    1
      src/components/ProductHistoryList/ProductHistoryList.jsx

+ 23
- 1
src/components/ProductHistoryList/ProductHistoryList.jsx 查看文件

@@ -2,9 +2,23 @@ import { useState, useEffect } from 'react';
2 2
 import { Box, Typography, IconButton, Pagination, Button } from '@mui/material';
3 3
 import { useSelector, useDispatch } from 'react-redux';
4 4
 import defaultImage from "../../assets/images/default.png"
5
+import { getMenuCollectionTitlesByProductType } from '../../config/menuCollections';
5 6
 
6 7
 import Grid from '@mui/material/Grid2';
7 8
 
9
+const normalizeTitle = (title = "") => title.trim().toUpperCase();
10
+
11
+const allowedRecentlyViewedCollections = getMenuCollectionTitlesByProductType();
12
+
13
+const isProductAllowedInRecentlyViewed = (product) => {
14
+  const allowedCollections = allowedRecentlyViewedCollections[product?.productType] || [];
15
+  const normalizedAllowedCollections = allowedCollections.map(normalizeTitle);
16
+
17
+  return product?.collections?.some((collection) =>
18
+    normalizedAllowedCollections.includes(normalizeTitle(collection?.title))
19
+  );
20
+};
21
+
8 22
 const ProductHistoryList = () => {
9 23
 
10 24
   const products = useSelector((state) => state.products.products.data); // only used as referenced
@@ -15,7 +29,15 @@ const ProductHistoryList = () => {
15 29
     if (products.length > 0 && localStorage.getItem('amber-product-history')) {
16 30
 
17 31
       const productHandleList = JSON.parse(localStorage.getItem('amber-product-history')) || []
18
-      const selectedProducts = products.filter(({ handle }) => productHandleList.includes(handle)) || []
32
+      const selectedProducts = products.filter((product) => (
33
+        productHandleList.includes(product.handle) && isProductAllowedInRecentlyViewed(product)
34
+      )) || []
35
+
36
+      // Keep the old history key, but remove products that are no longer available through the menu.
37
+      localStorage.setItem(
38
+        'amber-product-history',
39
+        JSON.stringify(selectedProducts.map(({ handle }) => handle))
40
+      )
19 41
 
20 42
       setFilterProducts(selectedProducts)
21 43
 

正在加载...
取消
保存