@springmicro/cart 0.3.2 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +21 -21
- package/README.md +64 -64
- package/dist/index.js +9778 -9696
- package/dist/index.umd.cjs +66 -92
- package/package.json +5 -3
- package/src/AddToCartForm.tsx +16 -16
- package/src/CartButton.tsx +249 -249
- package/src/ProductCard.css +106 -106
- package/src/ProductCard.tsx +165 -165
- package/src/checkout/{CheckoutList.css → ReviewCartAndCalculateTaxes.css} +93 -93
- package/src/checkout/ReviewCartAndCalculateTaxes.tsx +366 -0
- package/src/checkout/components/AddCard.tsx +267 -0
- package/src/checkout/components/Address.tsx +261 -265
- package/src/checkout/components/CartList.tsx +151 -0
- package/src/checkout/components/CartProductCard.css +67 -67
- package/src/checkout/components/CartProductCard.tsx +90 -80
- package/src/checkout/components/Invoice.tsx +145 -0
- package/src/checkout/components/ProviderLogos.tsx +93 -93
- package/src/checkout/components/StatusBar.tsx +32 -0
- package/src/checkout/index.tsx +161 -0
- package/src/index.css +5 -5
- package/src/index.ts +36 -35
- package/src/types.d.ts +56 -56
- package/src/utils/api.ts +67 -67
- package/src/utils/cartAuthHandler.ts +50 -50
- package/src/utils/index.ts +28 -28
- package/src/utils/storage.ts +133 -133
- package/tsconfig.json +24 -24
- package/tsconfig.node.json +11 -11
- package/vite.config.ts +25 -25
- package/springmicro-cart-0.2.3.tgz +0 -0
- package/src/checkout/CheckoutList.tsx +0 -264
- package/src/checkout/components/Billing.tsx +0 -353
- package/src/checkout/components/Order.tsx +0 -93
- package/src/checkout/components/index.tsx +0 -104
package/src/utils/storage.ts
CHANGED
|
@@ -1,133 +1,133 @@
|
|
|
1
|
-
import {
|
|
2
|
-
fetchFromCartApi,
|
|
3
|
-
defaultCartValue,
|
|
4
|
-
pathDetailsIsFullyDefined,
|
|
5
|
-
} from "./";
|
|
6
|
-
import { persistentAtom } from "@nanostores/persistent";
|
|
7
|
-
import { atom } from "nanostores";
|
|
8
|
-
import { Cart, CartProduct, PathDetailsType } from "../types";
|
|
9
|
-
|
|
10
|
-
export const cartStore = persistentAtom(
|
|
11
|
-
"cart",
|
|
12
|
-
JSON.stringify(defaultCartValue)
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
export const apiPathDetails = atom<PathDetailsType>({});
|
|
16
|
-
|
|
17
|
-
function cartProductToCartDataItemIds(product: CartProduct) {
|
|
18
|
-
return {
|
|
19
|
-
...product,
|
|
20
|
-
image: undefined,
|
|
21
|
-
name: undefined,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
apiPathDetails.listen((pathDetails) => {
|
|
26
|
-
if (pathDetailsIsFullyDefined(pathDetails)) {
|
|
27
|
-
// Runs on init if there is a user id and api key. Automatically logs in if userId is updated from undefined.
|
|
28
|
-
fetchFromCartApi("GET", pathDetails).then(async (c) => {
|
|
29
|
-
const cart = await c.json();
|
|
30
|
-
cartStore.set(
|
|
31
|
-
JSON.stringify({
|
|
32
|
-
authentication: { loggedIn: true, user_id: cart.user_id },
|
|
33
|
-
items: JSON.parse((cart as any).items),
|
|
34
|
-
order: cart.order ? JSON.parse((cart as any).order) : undefined,
|
|
35
|
-
})
|
|
36
|
-
);
|
|
37
|
-
});
|
|
38
|
-
} else {
|
|
39
|
-
const localCartData = JSON.parse(cartStore.get());
|
|
40
|
-
if (localCartData.authentication.loggedIn)
|
|
41
|
-
cartStore.set(JSON.stringify(defaultCartValue));
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
export function addToCart(p: CartProduct) {
|
|
46
|
-
const cart: Cart = JSON.parse(cartStore.get());
|
|
47
|
-
const newCart: Cart = {
|
|
48
|
-
...cart,
|
|
49
|
-
items: [...cart.items, p],
|
|
50
|
-
order: undefined,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const pathDetails = apiPathDetails.get();
|
|
54
|
-
if (pathDetailsIsFullyDefined(pathDetails)) {
|
|
55
|
-
fetchFromCartApi(
|
|
56
|
-
"PUT",
|
|
57
|
-
pathDetails,
|
|
58
|
-
JSON.stringify({
|
|
59
|
-
user_id: pathDetails.userId,
|
|
60
|
-
items: newCart.items.map((p) => cartProductToCartDataItemIds(p)),
|
|
61
|
-
})
|
|
62
|
-
).then(async () => {
|
|
63
|
-
cartStore.set(JSON.stringify(newCart));
|
|
64
|
-
});
|
|
65
|
-
} else {
|
|
66
|
-
cartStore.set(JSON.stringify(newCart));
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function removeFromCart(i: number) {
|
|
71
|
-
const cart: Cart = JSON.parse(cartStore.get());
|
|
72
|
-
|
|
73
|
-
const products: CartProduct[] = [...cart.items];
|
|
74
|
-
products.splice(i, 1);
|
|
75
|
-
const newCart: Cart = { ...cart, items: products, order: undefined };
|
|
76
|
-
|
|
77
|
-
const pathDetails = apiPathDetails.get();
|
|
78
|
-
if (pathDetailsIsFullyDefined(pathDetails)) {
|
|
79
|
-
// If products.length is 0, delete cart. Otheriwise, update cart.
|
|
80
|
-
const cartIsEmpty = products.length > 0;
|
|
81
|
-
|
|
82
|
-
const body = {
|
|
83
|
-
user_id: pathDetails.userId,
|
|
84
|
-
items: newCart.items.map((p) => cartProductToCartDataItemIds(p)),
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
fetchFromCartApi(
|
|
88
|
-
cartIsEmpty ? "PUT" : "DELETE",
|
|
89
|
-
pathDetails,
|
|
90
|
-
cartIsEmpty ? JSON.stringify(body) : undefined
|
|
91
|
-
).then(async () => {
|
|
92
|
-
cartStore.set(JSON.stringify(newCart));
|
|
93
|
-
});
|
|
94
|
-
} else {
|
|
95
|
-
cartStore.set(JSON.stringify(newCart));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function clearCart() {
|
|
100
|
-
const cart: Cart = JSON.parse(cartStore.get());
|
|
101
|
-
const newCart: Cart = { ...cart, items: [], order: undefined };
|
|
102
|
-
|
|
103
|
-
const pathDetails = apiPathDetails.get();
|
|
104
|
-
if (pathDetailsIsFullyDefined(pathDetails)) {
|
|
105
|
-
fetchFromCartApi("DELETE", pathDetails).then(async () => {
|
|
106
|
-
cartStore.set(JSON.stringify(newCart));
|
|
107
|
-
});
|
|
108
|
-
} else {
|
|
109
|
-
cartStore.set(JSON.stringify(newCart));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function setOrder(order: { id: number; reference: string }) {
|
|
114
|
-
const cart: Cart = JSON.parse(cartStore.get());
|
|
115
|
-
const newCart: Cart = { ...cart, order };
|
|
116
|
-
|
|
117
|
-
const pathDetails = apiPathDetails.get();
|
|
118
|
-
if (pathDetailsIsFullyDefined(pathDetails)) {
|
|
119
|
-
fetchFromCartApi(
|
|
120
|
-
"PUT",
|
|
121
|
-
pathDetails,
|
|
122
|
-
JSON.stringify({
|
|
123
|
-
user_id: pathDetails.userId,
|
|
124
|
-
items: newCart.items.map((p) => cartProductToCartDataItemIds(p)),
|
|
125
|
-
order: JSON.stringify(order),
|
|
126
|
-
})
|
|
127
|
-
).then(async () => {
|
|
128
|
-
cartStore.set(JSON.stringify(newCart));
|
|
129
|
-
});
|
|
130
|
-
} else {
|
|
131
|
-
cartStore.set(JSON.stringify(newCart));
|
|
132
|
-
}
|
|
133
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
fetchFromCartApi,
|
|
3
|
+
defaultCartValue,
|
|
4
|
+
pathDetailsIsFullyDefined,
|
|
5
|
+
} from "./";
|
|
6
|
+
import { persistentAtom } from "@nanostores/persistent";
|
|
7
|
+
import { atom } from "nanostores";
|
|
8
|
+
import { Cart, CartProduct, PathDetailsType } from "../types";
|
|
9
|
+
|
|
10
|
+
export const cartStore = persistentAtom(
|
|
11
|
+
"cart",
|
|
12
|
+
JSON.stringify(defaultCartValue)
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
export const apiPathDetails = atom<PathDetailsType>({});
|
|
16
|
+
|
|
17
|
+
function cartProductToCartDataItemIds(product: CartProduct) {
|
|
18
|
+
return {
|
|
19
|
+
...product,
|
|
20
|
+
image: undefined,
|
|
21
|
+
name: undefined,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
apiPathDetails.listen((pathDetails) => {
|
|
26
|
+
if (pathDetailsIsFullyDefined(pathDetails)) {
|
|
27
|
+
// Runs on init if there is a user id and api key. Automatically logs in if userId is updated from undefined.
|
|
28
|
+
fetchFromCartApi("GET", pathDetails).then(async (c) => {
|
|
29
|
+
const cart = await c.json();
|
|
30
|
+
cartStore.set(
|
|
31
|
+
JSON.stringify({
|
|
32
|
+
authentication: { loggedIn: true, user_id: cart.user_id },
|
|
33
|
+
items: JSON.parse((cart as any).items),
|
|
34
|
+
order: cart.order ? JSON.parse((cart as any).order) : undefined,
|
|
35
|
+
})
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
} else {
|
|
39
|
+
const localCartData = JSON.parse(cartStore.get());
|
|
40
|
+
if (localCartData.authentication.loggedIn)
|
|
41
|
+
cartStore.set(JSON.stringify(defaultCartValue));
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export function addToCart(p: CartProduct) {
|
|
46
|
+
const cart: Cart = JSON.parse(cartStore.get());
|
|
47
|
+
const newCart: Cart = {
|
|
48
|
+
...cart,
|
|
49
|
+
items: [...cart.items, p],
|
|
50
|
+
order: undefined,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const pathDetails = apiPathDetails.get();
|
|
54
|
+
if (pathDetailsIsFullyDefined(pathDetails)) {
|
|
55
|
+
fetchFromCartApi(
|
|
56
|
+
"PUT",
|
|
57
|
+
pathDetails,
|
|
58
|
+
JSON.stringify({
|
|
59
|
+
user_id: pathDetails.userId,
|
|
60
|
+
items: newCart.items.map((p) => cartProductToCartDataItemIds(p)),
|
|
61
|
+
})
|
|
62
|
+
).then(async () => {
|
|
63
|
+
cartStore.set(JSON.stringify(newCart));
|
|
64
|
+
});
|
|
65
|
+
} else {
|
|
66
|
+
cartStore.set(JSON.stringify(newCart));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function removeFromCart(i: number) {
|
|
71
|
+
const cart: Cart = JSON.parse(cartStore.get());
|
|
72
|
+
|
|
73
|
+
const products: CartProduct[] = [...cart.items];
|
|
74
|
+
products.splice(i, 1);
|
|
75
|
+
const newCart: Cart = { ...cart, items: products, order: undefined };
|
|
76
|
+
|
|
77
|
+
const pathDetails = apiPathDetails.get();
|
|
78
|
+
if (pathDetailsIsFullyDefined(pathDetails)) {
|
|
79
|
+
// If products.length is 0, delete cart. Otheriwise, update cart.
|
|
80
|
+
const cartIsEmpty = products.length > 0;
|
|
81
|
+
|
|
82
|
+
const body = {
|
|
83
|
+
user_id: pathDetails.userId,
|
|
84
|
+
items: newCart.items.map((p) => cartProductToCartDataItemIds(p)),
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
fetchFromCartApi(
|
|
88
|
+
cartIsEmpty ? "PUT" : "DELETE",
|
|
89
|
+
pathDetails,
|
|
90
|
+
cartIsEmpty ? JSON.stringify(body) : undefined
|
|
91
|
+
).then(async () => {
|
|
92
|
+
cartStore.set(JSON.stringify(newCart));
|
|
93
|
+
});
|
|
94
|
+
} else {
|
|
95
|
+
cartStore.set(JSON.stringify(newCart));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function clearCart() {
|
|
100
|
+
const cart: Cart = JSON.parse(cartStore.get());
|
|
101
|
+
const newCart: Cart = { ...cart, items: [], order: undefined };
|
|
102
|
+
|
|
103
|
+
const pathDetails = apiPathDetails.get();
|
|
104
|
+
if (pathDetailsIsFullyDefined(pathDetails)) {
|
|
105
|
+
fetchFromCartApi("DELETE", pathDetails).then(async () => {
|
|
106
|
+
cartStore.set(JSON.stringify(newCart));
|
|
107
|
+
});
|
|
108
|
+
} else {
|
|
109
|
+
cartStore.set(JSON.stringify(newCart));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function setOrder(order: { id: number; reference: string }) {
|
|
114
|
+
const cart: Cart = JSON.parse(cartStore.get());
|
|
115
|
+
const newCart: Cart = { ...cart, order };
|
|
116
|
+
|
|
117
|
+
const pathDetails = apiPathDetails.get();
|
|
118
|
+
if (pathDetailsIsFullyDefined(pathDetails)) {
|
|
119
|
+
fetchFromCartApi(
|
|
120
|
+
"PUT",
|
|
121
|
+
pathDetails,
|
|
122
|
+
JSON.stringify({
|
|
123
|
+
user_id: pathDetails.userId,
|
|
124
|
+
items: newCart.items.map((p) => cartProductToCartDataItemIds(p)),
|
|
125
|
+
order: JSON.stringify(order),
|
|
126
|
+
})
|
|
127
|
+
).then(async () => {
|
|
128
|
+
cartStore.set(JSON.stringify(newCart));
|
|
129
|
+
});
|
|
130
|
+
} else {
|
|
131
|
+
cartStore.set(JSON.stringify(newCart));
|
|
132
|
+
}
|
|
133
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"useDefineForClassFields": true,
|
|
5
|
-
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
6
|
-
"module": "ESNext",
|
|
7
|
-
"skipLibCheck": true,
|
|
8
|
-
|
|
9
|
-
/* Bundler mode */
|
|
10
|
-
"moduleResolution": "bundler",
|
|
11
|
-
"allowImportingTsExtensions": true,
|
|
12
|
-
"resolveJsonModule": true,
|
|
13
|
-
"isolatedModules": true,
|
|
14
|
-
"noEmit": true,
|
|
15
|
-
"jsx": "react-jsx",
|
|
16
|
-
|
|
17
|
-
/* Linting */
|
|
18
|
-
"strict": false,
|
|
19
|
-
"noFallthroughCasesInSwitch": true,
|
|
20
|
-
"noImplicitAny": false
|
|
21
|
-
},
|
|
22
|
-
"include": ["src"],
|
|
23
|
-
"references": [{ "path": "./tsconfig.node.json" }]
|
|
24
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"useDefineForClassFields": true,
|
|
5
|
+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
|
|
9
|
+
/* Bundler mode */
|
|
10
|
+
"moduleResolution": "bundler",
|
|
11
|
+
"allowImportingTsExtensions": true,
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"noEmit": true,
|
|
15
|
+
"jsx": "react-jsx",
|
|
16
|
+
|
|
17
|
+
/* Linting */
|
|
18
|
+
"strict": false,
|
|
19
|
+
"noFallthroughCasesInSwitch": true,
|
|
20
|
+
"noImplicitAny": false
|
|
21
|
+
},
|
|
22
|
+
"include": ["src"],
|
|
23
|
+
"references": [{ "path": "./tsconfig.node.json" }]
|
|
24
|
+
}
|
package/tsconfig.node.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"composite": true,
|
|
4
|
-
"skipLibCheck": true,
|
|
5
|
-
"module": "ESNext",
|
|
6
|
-
"moduleResolution": "bundler",
|
|
7
|
-
"allowSyntheticDefaultImports": true,
|
|
8
|
-
"strict": true
|
|
9
|
-
},
|
|
10
|
-
"include": ["vite.config.ts"]
|
|
11
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"composite": true,
|
|
4
|
+
"skipLibCheck": true,
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"allowSyntheticDefaultImports": true,
|
|
8
|
+
"strict": true
|
|
9
|
+
},
|
|
10
|
+
"include": ["vite.config.ts"]
|
|
11
|
+
}
|
package/vite.config.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import react from "@vitejs/plugin-react";
|
|
2
|
-
import { resolve } from "path";
|
|
3
|
-
import { defineConfig } from "vite";
|
|
4
|
-
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";
|
|
5
|
-
|
|
6
|
-
export default defineConfig({
|
|
7
|
-
plugins: [react(), cssInjectedByJsPlugin()],
|
|
8
|
-
build: {
|
|
9
|
-
lib: {
|
|
10
|
-
entry: resolve(__dirname, "src/index.ts"),
|
|
11
|
-
name: "@springmicro/cart",
|
|
12
|
-
fileName: "index",
|
|
13
|
-
},
|
|
14
|
-
rollupOptions: {
|
|
15
|
-
external: ["react", "react-dom", "nanoid"],
|
|
16
|
-
output: {
|
|
17
|
-
globals: {
|
|
18
|
-
react: "React",
|
|
19
|
-
"react-dom": "ReactDOM",
|
|
20
|
-
nanoid: "Nanoid",
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
});
|
|
1
|
+
import react from "@vitejs/plugin-react";
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
import { defineConfig } from "vite";
|
|
4
|
+
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";
|
|
5
|
+
|
|
6
|
+
export default defineConfig({
|
|
7
|
+
plugins: [react(), cssInjectedByJsPlugin()],
|
|
8
|
+
build: {
|
|
9
|
+
lib: {
|
|
10
|
+
entry: resolve(__dirname, "src/index.ts"),
|
|
11
|
+
name: "@springmicro/cart",
|
|
12
|
+
fileName: "index",
|
|
13
|
+
},
|
|
14
|
+
rollupOptions: {
|
|
15
|
+
external: ["react", "react-dom", "nanoid"],
|
|
16
|
+
output: {
|
|
17
|
+
globals: {
|
|
18
|
+
react: "React",
|
|
19
|
+
"react-dom": "ReactDOM",
|
|
20
|
+
nanoid: "Nanoid",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
Binary file
|
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import "./CheckoutList.css";
|
|
2
|
-
import { useStore } from "@nanostores/react";
|
|
3
|
-
import { CartProductCard } from "./components/CartProductCard";
|
|
4
|
-
import React from "react";
|
|
5
|
-
import { Button, Tooltip, Typography } from "@mui/material";
|
|
6
|
-
import {
|
|
7
|
-
cartStore,
|
|
8
|
-
clearCart,
|
|
9
|
-
setOrder as cartSetOrder,
|
|
10
|
-
} from "../utils/storage";
|
|
11
|
-
import Checkout from "./components";
|
|
12
|
-
|
|
13
|
-
export default function CheckoutList({
|
|
14
|
-
apiBaseUrl,
|
|
15
|
-
emptyCartLink = "",
|
|
16
|
-
disableProductLink = false,
|
|
17
|
-
}: {
|
|
18
|
-
apiBaseUrl: string;
|
|
19
|
-
emptyCartLink?: string;
|
|
20
|
-
disableProductLink?: boolean;
|
|
21
|
-
}) {
|
|
22
|
-
const cart = JSON.parse(useStore(cartStore));
|
|
23
|
-
|
|
24
|
-
const [prices, setPrices] = React.useState({});
|
|
25
|
-
const [total, setTotal] = React.useState("Loading prices...");
|
|
26
|
-
|
|
27
|
-
const [status, setStatus] = React.useState(0);
|
|
28
|
-
const [order, setOrder] = React.useState(undefined);
|
|
29
|
-
|
|
30
|
-
React.useEffect(() => {
|
|
31
|
-
const params = new URLSearchParams(window.location.search);
|
|
32
|
-
|
|
33
|
-
const sr = params.get("showReceipt");
|
|
34
|
-
const oid = params.get("orderId");
|
|
35
|
-
const or = params.get("orderRef");
|
|
36
|
-
if (oid && or) {
|
|
37
|
-
fetch(`${apiBaseUrl}/api/ecommerce/orders/${oid}/reference/${or}`).then(
|
|
38
|
-
async (res) =>
|
|
39
|
-
await res.json().then((order) => {
|
|
40
|
-
setOrder(order);
|
|
41
|
-
setStatus(
|
|
42
|
-
order.charge_id ||
|
|
43
|
-
!["pending", "awaiting_payment"].includes(order.status)
|
|
44
|
-
? 2
|
|
45
|
-
: 1
|
|
46
|
-
);
|
|
47
|
-
})
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
}, []);
|
|
51
|
-
|
|
52
|
-
// build pricing list
|
|
53
|
-
React.useEffect(() => {
|
|
54
|
-
// filter out prices that have already been queried
|
|
55
|
-
const pricesToGet = cart.items
|
|
56
|
-
.map((c) => c.price_id)
|
|
57
|
-
.filter(
|
|
58
|
-
(pId) => Object.keys(prices).findIndex((pKey) => pKey == pId) === -1
|
|
59
|
-
);
|
|
60
|
-
if (pricesToGet.length === 0) return;
|
|
61
|
-
|
|
62
|
-
const url = `${apiBaseUrl}/api/ecommerce/price?filter={'ids':[${pricesToGet.join(
|
|
63
|
-
","
|
|
64
|
-
)}]}`;
|
|
65
|
-
fetch(url, {
|
|
66
|
-
method: "GET",
|
|
67
|
-
headers: {
|
|
68
|
-
"Content-Type": "application/json",
|
|
69
|
-
},
|
|
70
|
-
})
|
|
71
|
-
.then((res) =>
|
|
72
|
-
res.json().then((data) => {
|
|
73
|
-
const pricingData = { ...prices };
|
|
74
|
-
|
|
75
|
-
data.forEach((p) => {
|
|
76
|
-
pricingData[p.id] = p;
|
|
77
|
-
});
|
|
78
|
-
setPrices(pricingData);
|
|
79
|
-
})
|
|
80
|
-
)
|
|
81
|
-
.catch(() => {});
|
|
82
|
-
}, [cart]);
|
|
83
|
-
|
|
84
|
-
React.useEffect(() => {
|
|
85
|
-
setTotal(
|
|
86
|
-
`$${(
|
|
87
|
-
cart.items
|
|
88
|
-
.map((product) => prices[product.price_id]?.unit_amount)
|
|
89
|
-
.reduce((p, c) => (c ? p + c : p), 0) / 100
|
|
90
|
-
).toFixed(2)}`
|
|
91
|
-
);
|
|
92
|
-
}, [cart, prices]);
|
|
93
|
-
|
|
94
|
-
if (status === 0 && cart.items.length === 0)
|
|
95
|
-
return (
|
|
96
|
-
<div
|
|
97
|
-
style={{
|
|
98
|
-
width: "100%",
|
|
99
|
-
display: "flex",
|
|
100
|
-
alignItems: "center",
|
|
101
|
-
flexDirection: "column",
|
|
102
|
-
gap: 8,
|
|
103
|
-
}}
|
|
104
|
-
>
|
|
105
|
-
<Typography style={{ fontSize: 32, fontWeight: 600 }}>
|
|
106
|
-
Cart is empty
|
|
107
|
-
</Typography>
|
|
108
|
-
<a className="shopping-button" href={`/${emptyCartLink}`}>
|
|
109
|
-
<Typography>BACK</Typography>
|
|
110
|
-
</a>
|
|
111
|
-
</div>
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
return (
|
|
115
|
-
<div>
|
|
116
|
-
<StatusBar status={status} />
|
|
117
|
-
<div>
|
|
118
|
-
{status === 0 && (
|
|
119
|
-
<CartSection
|
|
120
|
-
total={total}
|
|
121
|
-
setStatus={setStatus}
|
|
122
|
-
cart={cart}
|
|
123
|
-
prices={prices}
|
|
124
|
-
apiBaseUrl={apiBaseUrl}
|
|
125
|
-
setOrder={setOrder}
|
|
126
|
-
disableProductLink={disableProductLink}
|
|
127
|
-
/>
|
|
128
|
-
)}
|
|
129
|
-
{status > 0 && order && (
|
|
130
|
-
<Checkout
|
|
131
|
-
apiBaseUrl={apiBaseUrl}
|
|
132
|
-
order={order}
|
|
133
|
-
onPlacement={() => {
|
|
134
|
-
clearCart();
|
|
135
|
-
setStatus(2);
|
|
136
|
-
}}
|
|
137
|
-
/>
|
|
138
|
-
)}
|
|
139
|
-
</div>
|
|
140
|
-
</div>
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function CartSection({
|
|
145
|
-
total,
|
|
146
|
-
setStatus,
|
|
147
|
-
cart,
|
|
148
|
-
prices,
|
|
149
|
-
apiBaseUrl,
|
|
150
|
-
setOrder,
|
|
151
|
-
disableProductLink,
|
|
152
|
-
}) {
|
|
153
|
-
function createOrder() {
|
|
154
|
-
// If an order has already been created and hasn't been changed, get the previous order.
|
|
155
|
-
if (cart.order && cart.order.id && cart.order.reference) {
|
|
156
|
-
fetch(
|
|
157
|
-
`${apiBaseUrl}/api/ecommerce/orders/${cart.order.id}/reference/${cart.order.reference}`
|
|
158
|
-
).then((res) =>
|
|
159
|
-
res.json().then((order) => {
|
|
160
|
-
setOrder(order);
|
|
161
|
-
const currentUrl = new URL(window.location.href);
|
|
162
|
-
currentUrl.searchParams.set("orderId", order.id);
|
|
163
|
-
currentUrl.searchParams.set("orderRef", order.reference);
|
|
164
|
-
window.history.pushState({}, "", currentUrl);
|
|
165
|
-
setStatus(1);
|
|
166
|
-
})
|
|
167
|
-
);
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Otherwise, create a new order.
|
|
172
|
-
fetch(apiBaseUrl + "/api/ecommerce/orders", {
|
|
173
|
-
method: "POST",
|
|
174
|
-
headers: {
|
|
175
|
-
"Content-Type": "application/json",
|
|
176
|
-
},
|
|
177
|
-
body: JSON.stringify({
|
|
178
|
-
customer_id: 1,
|
|
179
|
-
cart_data: {
|
|
180
|
-
items: cart.items.map((li) => ({
|
|
181
|
-
...li,
|
|
182
|
-
name: undefined,
|
|
183
|
-
image: undefined,
|
|
184
|
-
})),
|
|
185
|
-
},
|
|
186
|
-
}),
|
|
187
|
-
}).then((res) =>
|
|
188
|
-
res.json().then((order) => {
|
|
189
|
-
if (!order) throw "Missing order";
|
|
190
|
-
setOrder(order);
|
|
191
|
-
cartSetOrder({ id: order.id, reference: order.reference });
|
|
192
|
-
const currentUrl = new URL(window.location.href);
|
|
193
|
-
currentUrl.searchParams.set("orderId", order.id);
|
|
194
|
-
currentUrl.searchParams.set("orderRef", order.reference);
|
|
195
|
-
window.history.pushState({}, "", currentUrl);
|
|
196
|
-
setStatus(1);
|
|
197
|
-
})
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return (
|
|
202
|
-
<div style={{ display: "flex", justifyContent: "center" }}>
|
|
203
|
-
<div
|
|
204
|
-
style={{
|
|
205
|
-
display: "flex",
|
|
206
|
-
flexDirection: "column",
|
|
207
|
-
width: 600,
|
|
208
|
-
alignItems: "center",
|
|
209
|
-
boxShadow: "0px 2px 5px 2px #dfdfdfff",
|
|
210
|
-
margin: "4px",
|
|
211
|
-
padding: "2rem 0",
|
|
212
|
-
borderRadius: "8px",
|
|
213
|
-
}}
|
|
214
|
-
>
|
|
215
|
-
<Typography style={{ fontSize: "30px", fontWeight: "bold" }}>
|
|
216
|
-
Checkout
|
|
217
|
-
</Typography>
|
|
218
|
-
<Tooltip title="Total is pre-tax and includes the first payment for recurring payments.">
|
|
219
|
-
<Typography style={{ fontSize: "20px" }}>Total: {total}</Typography>
|
|
220
|
-
</Tooltip>
|
|
221
|
-
{/* <Button variant="contained" sx={{ mt: 2 }} onClick={createOrder}>
|
|
222
|
-
Confirm Order
|
|
223
|
-
</Button> */}
|
|
224
|
-
<div className="checkout-list">
|
|
225
|
-
{cart.items.map((p, i) => (
|
|
226
|
-
<CartProductCard
|
|
227
|
-
product={p}
|
|
228
|
-
i={i}
|
|
229
|
-
price={prices[p.price_id]}
|
|
230
|
-
disableProductLink={disableProductLink}
|
|
231
|
-
/>
|
|
232
|
-
))}
|
|
233
|
-
</div>
|
|
234
|
-
<Button variant="contained" sx={{ mt: 2 }} onClick={createOrder}>
|
|
235
|
-
Confirm Order
|
|
236
|
-
</Button>
|
|
237
|
-
</div>
|
|
238
|
-
</div>
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function StatusBar({ status }) {
|
|
243
|
-
return (
|
|
244
|
-
<div
|
|
245
|
-
style={{
|
|
246
|
-
display: "flex",
|
|
247
|
-
alignItems: "center",
|
|
248
|
-
flexDirection: "column",
|
|
249
|
-
}}
|
|
250
|
-
>
|
|
251
|
-
<div id="status-bar">
|
|
252
|
-
<Typography className="status-text active">REVIEW</Typography>
|
|
253
|
-
<div className={"status-bar" + (status > 0 ? " active" : "")}></div>
|
|
254
|
-
<Typography className={"status-text" + (status > 0 ? " active" : "")}>
|
|
255
|
-
PAYMENT
|
|
256
|
-
</Typography>
|
|
257
|
-
<div className={"status-bar" + (status > 1 ? " active" : "")}></div>
|
|
258
|
-
<Typography className={"status-text" + (status > 1 ? " active" : "")}>
|
|
259
|
-
ORDER PLACED
|
|
260
|
-
</Typography>
|
|
261
|
-
</div>
|
|
262
|
-
</div>
|
|
263
|
-
);
|
|
264
|
-
}
|