1
+ import React , { useRef } from 'react' ;
2
+ import Link from 'next/link' ;
3
+ import { AiOutlineMinus , AiOutlinePlus , AiOutlineLeft , AiOutlineShopping } from 'react-icons/ai' ;
4
+ import { TiDeleteOutline } from 'react-icons/ti' ;
5
+ import toast from 'react-hot-toast' ;
6
+
7
+ import { useStateContext } from '../context/StateContext' ;
8
+ import { urlFor } from '../lib/client' ;
9
+ import getStripe from '../lib/getStripe' ;
10
+
11
+ const Cart = ( ) => {
12
+ const cartRef = useRef ( ) ;
13
+ const { totalPrice, totalQuantities, cartItems, setShowCart, toggleCartItemQuanitity, onRemove } = useStateContext ( ) ;
14
+
15
+ const handleCheckout = async ( ) => {
16
+ const stripe = await getStripe ( )
17
+ // getStripe() is responsible for initiating a payemnt request with Stripe
18
+
19
+ const response = await fetch ( '/api/stripe' , {
20
+ method : 'POST' ,
21
+ headers : {
22
+ 'Content-Type' : 'application/json' ,
23
+ } ,
24
+ body : JSON . stringify ( cartItems ) ,
25
+ } ) ;
26
+
27
+ if ( response . statusCode === 500 ) return ;
28
+
29
+ const data = await response . json ( ) ;
30
+
31
+ toast . loading ( 'Redirecting...' ) ;
32
+
33
+ stripe . redirectToCheckout ( { sessionId : data . id } ) ;
34
+
35
+ }
36
+
37
+
38
+ return (
39
+ < div className = "cart-wrapper" ref = { cartRef } >
40
+ < div className = "cart-container" >
41
+ < button
42
+ type = "button"
43
+ className = "cart-heading"
44
+ onClick = { ( ) => setShowCart ( false ) } >
45
+ < AiOutlineLeft />
46
+ < span className = "heading" > Your Cart</ span >
47
+ < span className = "cart-num-items" > ({ totalQuantities } items)</ span >
48
+ </ button >
49
+
50
+ { cartItems . length < 1 && (
51
+ < div className = "empty-cart" >
52
+ < AiOutlineShopping size = { 150 } />
53
+ < h3 > Your shopping bag is empty</ h3 >
54
+ < Link href = "/" >
55
+ < button
56
+ type = "button"
57
+ onClick = { ( ) => setShowCart ( false ) }
58
+ className = "btn"
59
+ >
60
+ Continue Shopping
61
+ </ button >
62
+ </ Link >
63
+ </ div >
64
+ ) }
65
+
66
+ < div className = "product-container" >
67
+ { cartItems . length >= 1 && cartItems . map ( ( item ) => (
68
+ < div className = "product" key = { item . _id } >
69
+ < img src = { urlFor ( item ?. image [ 0 ] ) } className = "cart-product-image" />
70
+ < div className = "item-desc" >
71
+ < div className = "flex top" >
72
+ < h5 > { item . name } </ h5 >
73
+ < h4 > ${ item . price } </ h4 >
74
+ </ div >
75
+ < div className = "flex bottom" >
76
+ < div >
77
+ < p className = "quantity-desc" >
78
+ < span className = "minus" onClick = { ( ) => toggleCartItemQuanitity ( item . _id , 'dec' ) } >
79
+ < AiOutlineMinus />
80
+ </ span >
81
+ < span className = "num" > { item . quantity } </ span >
82
+ < span className = "plus" onClick = { ( ) => toggleCartItemQuanitity ( item . _id , 'inc' ) } > < AiOutlinePlus /> </ span >
83
+ </ p >
84
+ </ div >
85
+ < button
86
+ type = "button"
87
+ className = "remove-item"
88
+ onClick = { ( ) => onRemove ( item ) }
89
+ >
90
+ < TiDeleteOutline />
91
+ </ button >
92
+ </ div >
93
+ </ div >
94
+ </ div >
95
+ ) ) }
96
+ </ div >
97
+ { cartItems . length >= 1 && (
98
+ < div className = "cart-bottom" >
99
+ < div className = "total" >
100
+ < h3 > Subtotal:</ h3 >
101
+ < h3 > ${ totalPrice } </ h3 >
102
+ </ div >
103
+ < div className = "btn-container" >
104
+ < button type = "button" className = "btn" onClick = { handleCheckout } >
105
+ CHECKOUT
106
+ </ button >
107
+ </ div >
108
+ </ div >
109
+ ) }
110
+ </ div >
111
+ </ div >
112
+ )
113
+ }
114
+
115
+ export default Cart
0 commit comments