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