|
|
@@ -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
|
|