forked from wix/wix-headless-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
117 lines (107 loc) · 3.08 KB
/
store.js
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
import Cookies from "js-cookie";
import { useEffect, useState } from "react";
import { createClient, OAuthStrategy } from "@wix/sdk";
import { products } from "@wix/stores";
import { currentCart } from "@wix/ecom";
import { redirects } from "@wix/redirects";
import testIds from "@/src/utils/test-ids";
const myWixClient = createClient({
modules: { products, currentCart, redirects },
auth: OAuthStrategy({
clientId: `9e37d7b0-3621-418f-a6b6-b82bdeaf051d`,
tokens: JSON.parse(Cookies.get("session") || null),
}),
});
export default function Store() {
const [productList, setProductList] = useState([]);
const [cart, setCart] = useState({});
async function fetchProducts() {
const productList = await myWixClient.products.queryProducts().find();
setProductList(productList.items);
}
async function fetchCart() {
try {
setCart(await myWixClient.currentCart.getCurrentCart());
} catch {}
}
async function addToCart(product) {
const options = product.productOptions.reduce(
(selected, option) => ({
...selected,
[option.name]: option.choices[0].description,
}),
{}
);
const { cart } = await myWixClient.currentCart.addToCurrentCart({
lineItems: [
{
catalogReference: {
appId: "1380b703-ce81-ff05-f115-39571d94dfcd",
catalogItemId: product._id,
options: { options },
},
quantity: 1,
},
],
});
setCart(cart);
}
async function clearCart() {
await myWixClient.currentCart.deleteCurrentCart();
setCart({});
}
async function createRedirect() {
const { checkoutId } =
await myWixClient.currentCart.createCheckoutFromCurrentCart({
channelType: currentCart.ChannelType.WEB,
});
const redirect = await myWixClient.redirects.createRedirectSession({
ecomCheckout: { checkoutId },
callbacks: { postFlowUrl: window.location.href },
});
window.location = redirect.redirectSession.fullUrl;
}
useEffect(() => {
fetchProducts();
}, []);
useEffect(() => {
fetchCart();
}, []);
return (
<main data-testid={testIds.COMMERCE_PAGE.CONTAINER}>
<div>
<h2>Choose Products:</h2>
{productList.map((product) => {
return (
<section
data-testid={testIds.COMMERCE_PAGE.PRODUCT}
key={product._id}
onClick={() => addToCart(product)}
>
{product.name}
</section>
);
})}
</div>
<div>
<h2>Cart:</h2>
{cart.lineItems?.length > 0 && (
<>
<section
data-testid={testIds.COMMERCE_PAGE.CHECKOUT}
onClick={() => createRedirect()}
>
<h3>
{cart.lineItems.length} items ({cart.subtotal.formattedAmount})
</h3>
<span>Checkout</span>
</section>
<section onClick={() => clearCart()}>
<span>Clear cart</span>
</section>
</>
)}
</div>
</main>
);
}