1
+ import { useState , useEffect } from "react" ;
2
+ import { Button } from "@/components/ui/button" ;
3
+ import { X } from "lucide-react" ;
4
+ import toast , { Toaster } from "react-hot-toast" ;
5
+
6
+ function MyCollections ( ) {
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 ( product => product . id !== productId ) ;
18
+ setCollections ( updatedCollections ) ;
19
+ localStorage . setItem ( "myCollections" , JSON . stringify ( updatedCollections ) ) ;
20
+ toast . success ( "Product removed from collections" ) ;
21
+ } ;
22
+
23
+ return (
24
+ < div className = "container mx-auto p-4" >
25
+ < h1 className = "text-2xl font-bold mb-4" > My Collections</ h1 >
26
+ < div className = "grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6" >
27
+ { collections . map ( ( product ) => (
28
+ < div key = { product . id } className = "border rounded-lg overflow-hidden shadow-lg" >
29
+ < div className = "h-64 overflow-hidden" >
30
+ < img
31
+ src = { product . image }
32
+ alt = { product . name }
33
+ className = "w-full h-full object-contain"
34
+ />
35
+ </ div >
36
+ < div className = "p-4 bg-white" >
37
+ < h2 className = "text-lg font-semibold mb-2" > { product . name } </ h2 >
38
+ < p className = "text-gray-600 font-bold text-xl" >
39
+ ${ product . price . toFixed ( 2 ) }
40
+ </ p >
41
+ < div className = "mt-4 flex justify-between items-center" >
42
+ < Button size = "sm" > View Details</ Button >
43
+ < Button
44
+ size = "sm"
45
+ variant = "destructive"
46
+ onClick = { ( ) => removeFromCollections ( product . id ) }
47
+ >
48
+ < X size = { 16 } />
49
+ </ Button >
50
+ </ div >
51
+ </ div >
52
+ </ div >
53
+ ) ) }
54
+ </ div >
55
+ { collections . length === 0 && (
56
+ < p className = "text-center text-gray-500 mt-8" > Your collection is empty.</ p >
57
+ ) }
58
+ < Toaster />
59
+ </ div >
60
+ ) ;
61
+ }
62
+
63
+ export default MyCollections ;
0 commit comments