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'; 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(({ selected }) => selected) || [] //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, extra_desc) => { return ( {title} {collection_name} {title} {`${minPriceCurrency} ${parseFloat(minPrice).toFixed(2)}`} {extra_desc} ) } 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, productType, variants } = product let minPrice = minVariantPrice.amount let minPriceCurrency = minVariantPrice.currencyCode let maxPrice = maxVariantPrice.amount let maxPriceCurrency = maxVariantPrice.currencyCode let img_url = images[0]?.url let collection_name = collections[0]?.title return ( {renderProduct(handle, img_url, title, collection_name, minPrice, minPriceCurrency, maxPrice, maxPriceCurrency, "")} ) })} ); }; export default ProductSelected;