Parcourir la source

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

master
nadia il y a 1 jour
Parent
révision
b85afa18c9
1 fichiers modifiés avec 23 ajouts et 1 suppressions
  1. 23
    1
      src/components/ProductHistoryList/ProductHistoryList.jsx

+ 23
- 1
src/components/ProductHistoryList/ProductHistoryList.jsx Voir le fichier

2
 import { Box, Typography, IconButton, Pagination, Button } from '@mui/material';
2
 import { Box, Typography, IconButton, Pagination, Button } from '@mui/material';
3
 import { useSelector, useDispatch } from 'react-redux';
3
 import { useSelector, useDispatch } from 'react-redux';
4
 import defaultImage from "../../assets/images/default.png"
4
 import defaultImage from "../../assets/images/default.png"
5
+import { getMenuCollectionTitlesByProductType } from '../../config/menuCollections';
5
 
6
 
6
 import Grid from '@mui/material/Grid2';
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
 const ProductHistoryList = () => {
22
 const ProductHistoryList = () => {
9
 
23
 
10
   const products = useSelector((state) => state.products.products.data); // only used as referenced
24
   const products = useSelector((state) => state.products.products.data); // only used as referenced
15
     if (products.length > 0 && localStorage.getItem('amber-product-history')) {
29
     if (products.length > 0 && localStorage.getItem('amber-product-history')) {
16
 
30
 
17
       const productHandleList = JSON.parse(localStorage.getItem('amber-product-history')) || []
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
       setFilterProducts(selectedProducts)
42
       setFilterProducts(selectedProducts)
21
 
43
 

Chargement…
Annuler
Enregistrer