@@ -31,81 +31,94 @@ const AddToWishlistBtn = ({ product, slug }: AddToWishlistBtnProps) => {
3131 const addToWishlistFun = async ( ) => {
3232 // getting user by email so I can get his user id
3333 if ( session ?. user ?. email ) {
34- // sending fetch request to get user id because we will need it for saving wish item
35- apiClient . get ( `/api/users/email/${ session ?. user ?. email } ` , {
36- cache : "no-store" ,
37- } )
38- . then ( ( response ) => response . json ( ) )
39- . then ( ( data ) =>
40- apiClient . post ( "/api/wishlist" , {
41- method : "POST" ,
42- headers : {
43- Accept : "application/json, text/plain, */*" ,
44- "Content-Type" : "application/json" ,
45- } ,
46- body : JSON . stringify ( { productId : product ?. id , userId : data ?. id } ) ,
47- } )
48- . then ( ( response ) => response . json ( ) )
49- . then ( ( data ) => {
50- addToWishlist ( {
51- id : product ?. id ,
52- title : product ?. title ,
53- price : product ?. price ,
54- image : product ?. mainImage ,
55- slug : product ?. slug ,
56- stockAvailabillity : product ?. inStock ,
57- } ) ;
58- toast . success ( "Product added to the wishlist" ) ;
59- } )
60- ) ;
34+ try {
35+ // sending fetch request to get user id because we will need it for saving wish item
36+ const userResponse = await apiClient . get ( `/api/users/email/${ session ?. user ?. email } ` , {
37+ cache : "no-store" ,
38+ } ) ;
39+ const userData = await userResponse . json ( ) ;
40+
41+ // Add product to wishlist
42+ const wishlistResponse = await apiClient . post ( "/api/wishlist" , {
43+ productId : product ?. id ,
44+ userId : userData ?. id
45+ } ) ;
46+
47+ if ( wishlistResponse . ok ) {
48+ addToWishlist ( {
49+ id : product ?. id ,
50+ title : product ?. title ,
51+ price : product ?. price ,
52+ image : product ?. mainImage ,
53+ slug : product ?. slug ,
54+ stockAvailabillity : product ?. inStock ,
55+ } ) ;
56+ toast . success ( "Product added to the wishlist" ) ;
57+ } else {
58+ const errorData = await wishlistResponse . json ( ) ;
59+ toast . error ( errorData . message || "Failed to add product to wishlist" ) ;
60+ }
61+ } catch ( error ) {
62+ console . error ( "Error adding to wishlist:" , error ) ;
63+ toast . error ( "Failed to add product to wishlist" ) ;
64+ }
6165 } else {
6266 toast . error ( "You need to be logged in to add a product to the wishlist" ) ;
6367 }
6468 } ;
6569
6670 const removeFromWishlistFun = async ( ) => {
6771 if ( session ?. user ?. email ) {
68- // sending fetch request to get user id because we will need to delete wish item
69- apiClient . get ( `/api/users/email/${ session ?. user ?. email } ` , {
70- cache : "no-store" ,
71- } )
72- . then ( ( response ) => response . json ( ) )
73- . then ( ( data ) => {
74- return apiClient . delete (
75- `/api/wishlist/${ data ?. id } /${ product ?. id } ` ,
76- {
77- method : "DELETE" ,
78- }
79- ) ;
80- } )
81- . then ( ( response ) => {
72+ try {
73+ // sending fetch request to get user id because we will need to delete wish item
74+ const userResponse = await apiClient . get ( `/api/users/email/${ session ?. user ?. email } ` , {
75+ cache : "no-store" ,
76+ } ) ;
77+ const userData = await userResponse . json ( ) ;
78+
79+ // Remove product from wishlist
80+ const deleteResponse = await apiClient . delete (
81+ `/api/wishlist/${ userData ?. id } /${ product ?. id } `
82+ ) ;
83+
84+ if ( deleteResponse . ok ) {
8285 removeFromWishlist ( product ?. id ) ;
8386 toast . success ( "Product removed from the wishlist" ) ;
84- } ) ;
87+ } else {
88+ const errorData = await deleteResponse . json ( ) ;
89+ toast . error ( errorData . message || "Failed to remove product from wishlist" ) ;
90+ }
91+ } catch ( error ) {
92+ console . error ( "Error removing from wishlist:" , error ) ;
93+ toast . error ( "Failed to remove product from wishlist" ) ;
94+ }
8595 }
8696 } ;
8797
8898 const isInWishlist = async ( ) => {
89- // sending fetch request to get user id because we will need it for cheching whether the product is in wishlist
99+ // sending fetch request to get user id because we will need it for checking whether the product is in wishlist
90100 if ( session ?. user ?. email ) {
91- apiClient . get ( `/api/users/email/${ session ?. user ?. email } ` , {
92- cache : "no-store" ,
93- } )
94- . then ( ( response ) => response . json ( ) )
95- . then ( ( data ) => {
96- // checking is product in wishlist
97- return apiClient . get (
98- `/api/wishlist/${ data ?. id } /${ product ?. id } `
99- ) ;
100- } )
101- . then ( ( response ) => response . json ( ) )
102- . then ( ( data ) => {
103- if ( data [ 0 ] ?. id ) {
104- setIsProductInWishlist ( ( ) => true ) ;
105- } else {
106- setIsProductInWishlist ( ( ) => false ) ;
107- }
101+ try {
102+ const userResponse = await apiClient . get ( `/api/users/email/${ session ?. user ?. email } ` , {
103+ cache : "no-store" ,
108104 } ) ;
105+ const userData = await userResponse . json ( ) ;
106+
107+ // checking is product in wishlist
108+ const wishlistResponse = await apiClient . get (
109+ `/api/wishlist/${ userData ?. id } /${ product ?. id } `
110+ ) ;
111+ const wishlistData = await wishlistResponse . json ( ) ;
112+
113+ if ( wishlistData [ 0 ] ?. id ) {
114+ setIsProductInWishlist ( true ) ;
115+ } else {
116+ setIsProductInWishlist ( false ) ;
117+ }
118+ } catch ( error ) {
119+ console . error ( "Error checking wishlist status:" , error ) ;
120+ setIsProductInWishlist ( false ) ;
121+ }
109122 }
110123 } ;
111124
0 commit comments