import { useEffect, useState, useRef } from 'react';
import { Box, Typography, Button, useMediaQuery } from '@mui/material';
import Grid from '@mui/material/Grid2';
import { useSelector, useDispatch } from 'react-redux';
import { fetchProducts } from '../../redux/slices/productSlice';
import { Swiper, SwiperSlide } from 'swiper/react';
import { Scrollbar, A11y } from 'swiper/modules';
import ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
import KeyboardArrowRightIcon from '@mui/icons-material/KeyboardArrowRight';
import defaultImage from "../../assets/images/default.png"
import atomeLogo from "../../assets/images/atome.webp"
import { isProductInVisibleMenu } from "../../config/menuCollections";
const ProductSelected = () => {
const swiperRef = useRef(null); // Create a ref for the Swiper instance
const products = useSelector((state) => state.products.products.data)
const [filterProducts, setFilterProducts] = useState([])
useEffect(() => {
if (products.length > 0) {
let selectedProducts = products.filter((product) => (
product.selected && isProductInVisibleMenu(product, products)
)) || []
//sort
selectedProducts = selectedProducts.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
setFilterProducts(selectedProducts)
}
}, [products])
const swipeToLeft = () => {
if (swiperRef.current) {
swiperRef.current.slidePrev(); // Move to the previous slide
}
};
const swipeToRight = () => {
if (swiperRef.current) {
swiperRef.current.slideNext(); // Move to the previous slide
}
};
const renderProduct = (
handle,
img_url,
title,
collection_name,
minPrice,
minPriceCurrency,
maxPrice,
maxPriceCurrency,
minDiscountPrice,
minDiscountPriceCurrency,
maxDiscountPrice,
maxDiscountPriceCurrency,
extra_desc,
selected = false
) => {
return (
{collection_name}
{title}
0) ? "line-through" : "none"
}}
>
{`${minPriceCurrency} ${parseFloat(minPrice).toFixed(2)}`}
{(minDiscountPrice > 0) &&
{`${minDiscountPriceCurrency} ${parseFloat(minDiscountPrice).toFixed(2)}`}
}
or 3 payments of {`${(minDiscountPrice > 0) ? parseFloat((minDiscountPrice / 3)).toFixed(2) : parseFloat((minPrice / 3)).toFixed(2)} ${minPriceCurrency}`} with Atome
)
}
return (
{/* OFFSET PURPOSE */}
NEW IN
= 640px
1024: { slidesPerView: 4 }, // For screens >= 1024px
}}
pagination={{ clickable: true }}
scrollbar={{ draggable: true }}
onSwiper={(swiper) => (swiperRef.current = swiper)}
>
{filterProducts.map((product, index) => {
let {
handle,
title,
images,
collections,
minVariantPrice,
maxVariantPrice,
compareAtPriceRangeMinVariantPrice,
compareAtPriceRangeMaxVariantPrice,
productType,
variants,
selected,
} = product;
let minPrice = minVariantPrice.amount
let minPriceCurrency = minVariantPrice.currencyCode
let maxPrice = maxVariantPrice.amount
let maxPriceCurrency = maxVariantPrice.currencyCode
// SHOULD BE THE DISCOUNTED PRICE
let minDiscountPrice = compareAtPriceRangeMinVariantPrice.amount;
let minDiscountPriceCurrency = compareAtPriceRangeMinVariantPrice.currencyCode;
let maxDiscountPrice = compareAtPriceRangeMaxVariantPrice.amount;
let maxDiscountPriceCurrency = compareAtPriceRangeMaxVariantPrice.currencyCode;
let img_url = images[0]?.url || defaultImage
let collection_name = collections[0]?.title
return (
{renderProduct(
handle,
img_url,
title,
collection_name,
minPrice,
minPriceCurrency,
maxPrice,
maxPriceCurrency,
minDiscountPrice,
minDiscountPriceCurrency,
maxDiscountPrice,
maxDiscountPriceCurrency,
"",
selected
)}
)
})}
);
};
export default ProductSelected;