import { React, useEffect, useState } from "react"; import "./deals.css"; import Add from "../imgs/heart.png"; import Added from "../imgs/red-heart.png"; import rating from "../imgs/rating.png"; import { AddToList, RemoveList } from "../action/List"; import { useSelector, useDispatch } from "react-redux"; import Footer from "./Footer"; import Spinner from "./Spinner"; import LowerNav from "./LowerNav"; import { NavLink } from "react-router-dom"; function Deals() { const [AllProducts, setAllProducts] = useState([]); const [AddedIds, setAddedIds] = useState([]); const [loading, setLoading] = useState(true); // add loading state const ListItems = useSelector((state) => state.ItemsAdded.ListItems); const dispatch = useDispatch(); useEffect(() => { const GetProducts = async () => { const data = await fetch("https://fakestoreapi.com/products"); const new_data = await data.json(); setLoading(false); setAllProducts(new_data); // Add a review number property to each item object const productsWithReviewNumber = new_data.map((item) => ({ ...item, reviewNumber: Math.floor(Math.random() * (150 - 50 + 1)) + 50, })); setAllProducts(productsWithReviewNumber); }; GetProducts(); }, []); useEffect(() => { // Update the added ids whenever the list items change const ids = ListItems.map((item) => item.id); setAddedIds(ids); }, [ListItems]); const isAdded = (itemId) => { // Check if the item id is in the added ids return AddedIds.includes(itemId); }; return (

Hot Deals 🔥

{loading && }
{AllProducts && AllProducts.map((items) => { return (
{ if (!isAdded(items.id)) { dispatch(AddToList(items)); } else { dispatch(RemoveList(items.id)); } }} src={isAdded(items.id) ? Added : Add} className="add-list" />

{items.title.length >= 32 ? items.title.slice(0, 32) + ".." : items.title}

{items.category}

{"5 " + "(" + items.reviewNumber + " reviews)"}

${items.price}

${Math.round(items.price * 1.66)}

(60% OFF)

); })}
); } export default Deals;