|
| 1 | +import { Button } from "@/components/ui/button"; |
| 2 | +import { X } from "lucide-react"; |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import toast, { Toaster } from "react-hot-toast"; |
| 5 | + |
| 6 | +function MagicFit() { |
| 7 | + const [collections, setCollections] = useState([]); |
| 8 | + |
| 9 | + useEffect(() => { |
| 10 | + const savedCollections = localStorage.getItem("myCollections"); |
| 11 | + if (savedCollections) { |
| 12 | + setCollections(JSON.parse(savedCollections)); |
| 13 | + } |
| 14 | + }, []); |
| 15 | + |
| 16 | + const removeFromCollections = (productId) => { |
| 17 | + const updatedCollections = collections.filter( |
| 18 | + (product) => product.id !== productId |
| 19 | + ); |
| 20 | + setCollections(updatedCollections); |
| 21 | + localStorage.setItem("myCollections", JSON.stringify(updatedCollections)); |
| 22 | + toast.success("Product removed from collections"); |
| 23 | + }; |
| 24 | + |
| 25 | + return ( |
| 26 | + <div className="container mx-auto p-4 mt-20"> |
| 27 | + <h1 className="text-4xl font-bold mb-4 text-center py-4"> |
| 28 | + My Collections |
| 29 | + </h1> |
| 30 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6"> |
| 31 | + {collections.map((product) => { |
| 32 | + const selectedSize = product.selectedSize || {}; |
| 33 | + return ( |
| 34 | + <div |
| 35 | + key={product.id} |
| 36 | + className="border rounded-lg overflow-hidden shadow-lg text-center" |
| 37 | + > |
| 38 | + <div className="h-64 overflow-hidden"> |
| 39 | + <img |
| 40 | + src={product.image} |
| 41 | + alt={product.name} |
| 42 | + className="w-full h-full object-contain" |
| 43 | + /> |
| 44 | + </div> |
| 45 | + <div className="p-4 bg-white"> |
| 46 | + <h2 className="text-lg font-semibold mb-2">{product.name}</h2> |
| 47 | + <h3 className="text-md font-semibold mb-1"> |
| 48 | + Selected Size: {selectedSize.size} |
| 49 | + </h3> |
| 50 | + |
| 51 | + {selectedSize.size ? ( |
| 52 | + <div> |
| 53 | + <p> |
| 54 | + Neck: {selectedSize.sizeData?.neck || "N/A"} | Shoulder:{" "} |
| 55 | + {selectedSize.sizeData?.shoulder || "N/A"} | Chest:{" "} |
| 56 | + {selectedSize.sizeData?.chest || "N/A"} | Waist:{" "} |
| 57 | + {selectedSize.sizeData?.waist || "N/A"} | Arm Length:{" "} |
| 58 | + {selectedSize.sizeData?.armLength || "N/A"} |
| 59 | + </p> |
| 60 | + </div> |
| 61 | + ) : ( |
| 62 | + <p>N/A</p> |
| 63 | + )} |
| 64 | + <p className="text-gray-600 font-bold text-4xl py-3"> |
| 65 | + ${product.price.toFixed(2)} |
| 66 | + </p> |
| 67 | + <div className="mt-4 flex justify-between items-center"> |
| 68 | + <Button size="sm">View Details</Button> |
| 69 | + <Button |
| 70 | + size="sm" |
| 71 | + variant="destructive" |
| 72 | + onClick={() => removeFromCollections(product.id)} |
| 73 | + > |
| 74 | + <X size={16} /> |
| 75 | + </Button> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + ); |
| 80 | + })} |
| 81 | + </div> |
| 82 | + {collections.length === 0 && ( |
| 83 | + <p className="text-center text-gray-500 mt-8"> |
| 84 | + Your collection is empty. |
| 85 | + </p> |
| 86 | + )} |
| 87 | + <Toaster /> |
| 88 | + </div> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +export default MagicFit; |
0 commit comments