11'use client' ;
22import { useWishlistStore } from '@/app/_zustand/wishlistStore' ;
33import WishItem from '@/components/WishItem' ;
4- import { nanoid } from 'nanoid' ;
54import { useSession } from 'next-auth/react' ;
6- import { useEffect } from 'react' ;
5+ import { useCallback , useEffect } from 'react' ;
76
87export const WishlistModule = ( ) => {
9- const { data : session , status } = useSession ( ) ;
8+ const { data : session } = useSession ( ) ;
109 const { wishlist, setWishlist } = useWishlistStore ( ) ;
1110
12- const getWishlistByUserId = async ( id : string ) => {
13- const response = await fetch ( `http://localhost:3001/api/wishlist/${ id } ` , {
14- cache : 'no-store' ,
15- } ) ;
16- const wishlist = await response . json ( ) ;
17-
18- const productArray : {
19- id : string ;
20- title : string ;
21- price : number ;
22- image : string ;
23- slug : string ;
24- stockAvailabillity : number ;
25- } [ ] = [ ] ;
11+ const getWishlistByUserId = useCallback (
12+ async ( id : string ) => {
13+ const response = await fetch ( `http://localhost:3001/api/wishlist/${ id } ` , {
14+ cache : 'no-store' ,
15+ } ) ;
16+ const wishlistData = await response . json ( ) ;
2617
27- wishlist . map ( ( item : any ) =>
28- productArray . push ( {
18+ const productArray = wishlistData . map ( ( item : any ) => ( {
2919 id : item ?. product ?. id ,
3020 title : item ?. product ?. title ,
3121 price : item ?. product ?. price ,
3222 image : item ?. product ?. mainImage ,
3323 slug : item ?. product ?. slug ,
3424 stockAvailabillity : item ?. product ?. inStock ,
35- } ) ,
36- ) ;
25+ } ) ) ;
3726
38- setWishlist ( productArray ) ;
39- } ;
27+ setWishlist ( productArray ) ;
28+ } ,
29+ [ setWishlist ] ,
30+ ) ;
4031
41- const getUserByEmail = async ( ) => {
32+ const getUserByEmail = useCallback ( ( ) => {
4233 if ( session ?. user ?. email ) {
43- fetch ( `http://localhost:3001/api/users/email/${ session ? .user ? .email } ` , {
34+ fetch ( `http://localhost:3001/api/users/email/${ session . user . email } ` , {
4435 cache : 'no-store' ,
4536 } )
4637 . then ( ( response ) => response . json ( ) )
4738 . then ( ( data ) => {
48- getWishlistByUserId ( data ?. id ) ;
39+ if ( data ?. id ) {
40+ getWishlistByUserId ( data . id ) ;
41+ }
4942 } ) ;
5043 }
51- } ;
44+ } , [ session ?. user ?. email , getWishlistByUserId ] ) ;
5245
5346 useEffect ( ( ) => {
5447 getUserByEmail ( ) ;
55- } , [ session ?. user ?. email , wishlist . length ] ) ;
48+ } , [ getUserByEmail ] ) ;
49+
5650 return (
5751 < >
5852 { wishlist && wishlist . length === 0 ? (
@@ -73,18 +67,17 @@ export const WishlistModule = () => {
7367 </ tr >
7468 </ thead >
7569 < tbody >
76- { wishlist &&
77- wishlist ?. map ( ( item ) => (
78- < WishItem
79- id = { item ?. id }
80- title = { item ?. title }
81- price = { item ?. price }
82- image = { item ?. image }
83- slug = { item ?. slug }
84- stockAvailabillity = { item ?. stockAvailabillity }
85- key = { nanoid ( ) }
86- />
87- ) ) }
70+ { wishlist ?. map ( ( item ) => (
71+ < WishItem
72+ key = { item . id } // ✅ stable key
73+ id = { item . id }
74+ title = { item . title }
75+ price = { item . price }
76+ image = { item . image }
77+ slug = { item . slug }
78+ stockAvailabillity = { item . stockAvailabillity }
79+ />
80+ ) ) }
8881 </ tbody >
8982 </ table >
9083 </ div >
0 commit comments