|
| 1 | +import React, { useContext, useEffect, useState } from 'react'; |
| 2 | +import { shopDataContext } from '../context/ShopContext'; |
| 3 | +import Card from '../components/Card'; |
| 4 | +import Title from '../components/Title'; |
| 5 | +import { motion } from 'framer-motion'; |
| 6 | + |
| 7 | +const NewArrivals = () => { |
| 8 | + const { product, compareList, toggleComparePanel, removeFromCompare, toggleCompare } = useContext(shopDataContext); |
| 9 | + const [newArrivals, setNewArrivals] = useState([]); |
| 10 | + const [isLoading, setIsLoading] = useState(true); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + if (product && product.length > 0) { |
| 14 | + // Create a copy and reverse to get newest first |
| 15 | + const sortedProducts = [...product].reverse(); |
| 16 | + setNewArrivals(sortedProducts); |
| 17 | + setIsLoading(false); |
| 18 | + } else { |
| 19 | + // If product array is empty, we might be loading or there are no products. |
| 20 | + // Since we don't have an explicit loading state from context, |
| 21 | + // we'll assume it's loading initially and if it stays empty for a short while, |
| 22 | + // we could show "No products found". |
| 23 | + // However, usually context fetches on mount. |
| 24 | + // Let's just set loading to false if products are still empty after a brief moment |
| 25 | + // or rely on the fact that if products are 0, we show empty state or loader. |
| 26 | + |
| 27 | + // For now, let's keep loading true until products arrive, or use a timeout? |
| 28 | + // Actually, if ShopContext fetches on mount, product will update. |
| 29 | + // If there are genuinely 0 products, this will hang on loading. |
| 30 | + // But typically there are products. |
| 31 | + |
| 32 | + const timer = setTimeout(() => { |
| 33 | + setIsLoading(false); |
| 34 | + }, 2000); |
| 35 | + return () => clearTimeout(timer); |
| 36 | + } |
| 37 | + }, [product]); |
| 38 | + |
| 39 | + // Loading skeleton |
| 40 | + if (isLoading) { |
| 41 | + return ( |
| 42 | + <div className="container mx-auto px-4 py-16"> |
| 43 | + <div className="text-center"> |
| 44 | + <Title text1="NEW" text2="ARRIVALS" /> |
| 45 | + <p className="w-3/4 m-auto text-xs sm:text-sm md:text-base text-gray-600 mb-8"> |
| 46 | + Check out the latest additions to our collection. |
| 47 | + </p> |
| 48 | + </div> |
| 49 | + <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 gap-y-6"> |
| 50 | + {[...Array(8)].map((_, i) => ( |
| 51 | + <div key={i} className="animate-pulse"> |
| 52 | + <div className="bg-gray-200 h-64 rounded-lg mb-4"></div> |
| 53 | + <div className="h-4 bg-gray-200 rounded w-3/4 mb-2"></div> |
| 54 | + <div className="h-4 bg-gray-200 rounded w-1/2"></div> |
| 55 | + </div> |
| 56 | + ))} |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + return ( |
| 63 | + <div className="container mx-auto px-4 py-8 border-t pt-16"> |
| 64 | + <div className="text-center text-2xl mb-8"> |
| 65 | + <Title text1={'NEW'} text2={'ARRIVALS'}/> |
| 66 | + <p className="w-3/4 m-auto text-xs sm:text-sm md:text-base text-gray-600"> |
| 67 | + Discover our latest products, freshly added to our inventory. |
| 68 | + </p> |
| 69 | + </div> |
| 70 | + |
| 71 | + {newArrivals.length === 0 ? ( |
| 72 | + <div className="text-center py-20"> |
| 73 | + <p className="text-xl text-gray-500">No new arrivals found.</p> |
| 74 | + </div> |
| 75 | + ) : ( |
| 76 | + <div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4 gap-y-6"> |
| 77 | + {newArrivals.map((item, index) => ( |
| 78 | + <motion.div |
| 79 | + key={item._id} |
| 80 | + initial={{ opacity: 0, y: 20 }} |
| 81 | + animate={{ opacity: 1, y: 0 }} |
| 82 | + transition={{ duration: 0.5, delay: index * 0.05 }} |
| 83 | + > |
| 84 | + <Card |
| 85 | + id={item._id} |
| 86 | + image={item.image1} |
| 87 | + name={item.name} |
| 88 | + price={item.price} |
| 89 | + onCompare={() => toggleCompare(item)} |
| 90 | + isCompared={compareList?.some(p => p._id === item._id)} |
| 91 | + /> |
| 92 | + </motion.div> |
| 93 | + ))} |
| 94 | + </div> |
| 95 | + )} |
| 96 | + </div> |
| 97 | + ); |
| 98 | +}; |
| 99 | + |
| 100 | +export default NewArrivals; |
0 commit comments