@springmicro/cart 0.5.13 → 0.5.14

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@springmicro/cart",
3
3
  "private": false,
4
- "version": "0.5.13",
4
+ "version": "0.5.14",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  "@nanostores/persistent": "^0.10.1",
25
25
  "@nanostores/query": "^0.3.3",
26
26
  "@nanostores/react": "^0.7.2",
27
- "@springmicro/utils": "0.5.13",
27
+ "@springmicro/utils": "0.5.14",
28
28
  "dotenv": "^16.4.5",
29
29
  "nanostores": "^0.10.3",
30
30
  "react": "^18.2.0",
@@ -49,5 +49,5 @@
49
49
  "vite-plugin-css-injected-by-js": "^3.5.1",
50
50
  "yup": "^1.4.0"
51
51
  },
52
- "gitHead": "596e9fe58eab22df211e097c2058b5cc267fe30c"
52
+ "gitHead": "0c889259d325d955a7981f8e8975edc42c8b9cf8"
53
53
  }
@@ -26,7 +26,8 @@ import { AddressValues } from "./components/Address";
26
26
  import { useFormik } from "formik";
27
27
  import { postCheckout } from "../utils/api";
28
28
 
29
- async function createOrder(cart, apiBaseUrl) {
29
+ async function createOrder(cart, apiBaseUrl, dev) {
30
+ dev && console.log("Creating order");
30
31
  // If an order has already been created and hasn't been changed, get the previous order.
31
32
  if (cart.order && cart.order.id && cart.order.reference) {
32
33
  const res = await fetch(
@@ -71,7 +72,8 @@ async function createOrder(cart, apiBaseUrl) {
71
72
  return order;
72
73
  }
73
74
 
74
- async function getTax(apiBaseUrl, taxProvider, items, v, products) {
75
+ async function getTax(apiBaseUrl, taxProvider, items, v, products, dev) {
76
+ dev && console.log("Getting tax");
75
77
  const taxable_items = items.filter(
76
78
  (i) => i.unit_amount > 0 && products[i.product_id].type !== "DONATION"
77
79
  );
@@ -118,6 +120,7 @@ export default function ReviewAndCalculateTaxes({
118
120
  onPlacement = undefined,
119
121
  CollectExtraInfo = undefined,
120
122
  defaultValues = {},
123
+ dev,
121
124
  }) {
122
125
  const [formDisabled, setFormDisabled] = useState(true);
123
126
  const [formError, setFormError] = useState(undefined);
@@ -133,7 +136,7 @@ export default function ReviewAndCalculateTaxes({
133
136
  // status === 0 when this is clicked.
134
137
  // Creates an order and calculates tax.
135
138
  Promise.all([
136
- createOrder(cart, apiBaseUrl),
139
+ createOrder(cart, apiBaseUrl, dev),
137
140
  getTax(
138
141
  apiBaseUrl,
139
142
  taxProvider,
@@ -141,7 +144,9 @@ export default function ReviewAndCalculateTaxes({
141
144
  amount: prices[i.price_id].unit_amount,
142
145
  reference: i.name,
143
146
  })),
144
- values
147
+ values,
148
+ products,
149
+ dev
145
150
  ),
146
151
  ])
147
152
  .then(([order, taxData]) => {
@@ -155,6 +160,7 @@ export default function ReviewAndCalculateTaxes({
155
160
  }
156
161
 
157
162
  async function confirmOrder(values) {
163
+ dev && console.log("Confirm order");
158
164
  setFormError(undefined);
159
165
  setFormDisabled(true);
160
166
  postCheckout(
@@ -166,9 +172,10 @@ export default function ReviewAndCalculateTaxes({
166
172
  invoiceId
167
173
  )
168
174
  .then((data) => {
175
+ dev && console.log("Checkout submitted");
169
176
  if (data instanceof Response) {
170
177
  setFormError(true);
171
- alert(JSON.stringify(data));
178
+ dev && alert(data.statusText);
172
179
  } else {
173
180
  // success
174
181
  setSuccessData(data);
@@ -181,12 +188,13 @@ export default function ReviewAndCalculateTaxes({
181
188
  currentUrl.searchParams.set("orderRef", order.reference);
182
189
 
183
190
  // Update the browser's URL and history without refreshing the page
191
+ dev && console.log("Checkout success!");
184
192
  window.history.pushState({}, "", currentUrl);
185
193
  }
186
194
  setFormDisabled(false);
187
195
  })
188
- .catch(() => {
189
- // console.log("ERROR");
196
+ .catch((e) => {
197
+ dev && console.log("Checkout error", e);
190
198
  setFormError(true);
191
199
  });
192
200
  }
@@ -262,6 +270,7 @@ export default function ReviewAndCalculateTaxes({
262
270
  : validationSchemaBase
263
271
  ),
264
272
  onSubmit: async (v, helpers) => {
273
+ dev && console.log("Formik submit");
265
274
  setFormDisabled(true);
266
275
  const values = { ...v };
267
276
  if (formDisabled) {
@@ -294,6 +303,9 @@ export default function ReviewAndCalculateTaxes({
294
303
  // const res = await submitFunc(values);
295
304
  submitFunc(values)
296
305
  .then((v) => {})
306
+ .catch((e) => {
307
+ dev && console.log("Submit errors", e);
308
+ })
297
309
  .finally(() => {
298
310
  setFormDisabled(false);
299
311
  });
@@ -325,6 +337,10 @@ export default function ReviewAndCalculateTaxes({
325
337
  };
326
338
 
327
339
  useEffect(() => {
340
+ dev &&
341
+ (Object.keys(formik.errors).length > 0
342
+ ? console.log("Formik errors", formik.errors)
343
+ : "No formik errors");
328
344
  setFormDisabled(Object.keys(formik.errors).length > 0);
329
345
  }, [formik.errors]);
330
346
 
@@ -342,7 +358,7 @@ export default function ReviewAndCalculateTaxes({
342
358
  >
343
359
  <CartList
344
360
  {...{
345
- status,
361
+ statusState: [status, setStatus],
346
362
  cart,
347
363
  subtotal,
348
364
  tax,
@@ -354,7 +370,7 @@ export default function ReviewAndCalculateTaxes({
354
370
  disableProductLink,
355
371
  formik,
356
372
  formDisabled,
357
- formError,
373
+ formErrorState: [formError, setFormError],
358
374
  }}
359
375
  />
360
376
  {status === 0 && (
@@ -177,7 +177,7 @@ export function AddressEmailField({ formik, name, sx }: AddressFieldProps) {
177
177
  return (
178
178
  <>
179
179
  <TextField
180
- label={"Email"}
180
+ label="Billing email"
181
181
  sx={{ width: 400, display: "block", ...sx }}
182
182
  name={name as string}
183
183
  id={name as string}
@@ -7,7 +7,7 @@ function formatPrice(cents) {
7
7
  }
8
8
 
9
9
  export function CartList({
10
- status,
10
+ statusState,
11
11
  cart,
12
12
  subtotal,
13
13
  tax,
@@ -19,8 +19,10 @@ export function CartList({
19
19
  disableProductLink,
20
20
  formik,
21
21
  formDisabled,
22
- formError,
22
+ formErrorState,
23
23
  }) {
24
+ const [status, setStatus] = statusState;
25
+ const [formError, setFormError] = formErrorState;
24
26
  return (
25
27
  <form
26
28
  style={{ flexGrow: 1 }}
@@ -116,12 +118,6 @@ export function CartList({
116
118
  : subtotal}
117
119
  </Typography>
118
120
  </Box>
119
- {formError && (
120
- <Typography color="red">
121
- Could not confirm payment. Your card info may have been entered
122
- incorrectly.
123
- </Typography>
124
- )}
125
121
  {status === 1 && (
126
122
  <Button
127
123
  variant="contained"
@@ -133,22 +129,41 @@ export function CartList({
133
129
  Confirm Order
134
130
  </Button>
135
131
  )}
132
+ {formError && (
133
+ <Box
134
+ sx={{
135
+ display: "flex",
136
+ flexDirection: "column",
137
+ gap: 1,
138
+ alignItems: "center",
139
+ }}
140
+ >
141
+ <Typography color="red">
142
+ Could not confirm payment. Your card info may have been entered
143
+ incorrectly.
144
+ </Typography>
145
+ <Button
146
+ onClick={() => {
147
+ setFormError(undefined);
148
+ setStatus((i) => i - 1);
149
+ }}
150
+ variant="contained"
151
+ >
152
+ Back
153
+ </Button>
154
+ </Box>
155
+ )}
136
156
  <Box className="checkout-list">
137
- {cart.items.map(
138
- (p, i) => (
139
- console.log(p),
140
- (
141
- <CartProductCard
142
- product={products[p.product_id] ?? p}
143
- i={i}
144
- price={prices[p.price_id]}
145
- disableProductLink={disableProductLink}
146
- disableMissingImage={disableMissingImage}
147
- disableModification={status != 0}
148
- />
149
- )
150
- )
151
- )}
157
+ {cart.items.map((p, i) => (
158
+ <CartProductCard
159
+ product={products[p.product_id] ?? p}
160
+ i={i}
161
+ price={prices[p.price_id]}
162
+ disableProductLink={disableProductLink}
163
+ disableMissingImage={disableMissingImage}
164
+ disableModification={status != 0}
165
+ />
166
+ ))}
152
167
  </Box>
153
168
  </Box>
154
169
  </Box>
@@ -1,4 +1,4 @@
1
- import { Box, IconButton, Typography } from "@mui/material";
1
+ import { Box, IconButton, Typography, Tooltip } from "@mui/material";
2
2
  import ArrowBackIcon from "@mui/icons-material/ArrowBack";
3
3
 
4
4
  export function StatusBar({ status, backlink = "" }) {
@@ -11,9 +11,11 @@ export function StatusBar({ status, backlink = "" }) {
11
11
  }}
12
12
  >
13
13
  <Box sx={{ flex: "1 1" }}>
14
- <IconButton href={`/${backlink}`}>
15
- <ArrowBackIcon />
16
- </IconButton>
14
+ <Tooltip title="Exit checkout">
15
+ <IconButton href={`/${backlink}`}>
16
+ <ArrowBackIcon />
17
+ </IconButton>
18
+ </Tooltip>
17
19
  </Box>
18
20
  <div id="status-bar">
19
21
  <Typography className="status-text active">PAYMENT DETAILS</Typography>
@@ -16,6 +16,7 @@ export default function Checkout({
16
16
  disableMissingImage = false,
17
17
  CollectExtraInfo,
18
18
  defaultValues,
19
+ dev,
19
20
  }: {
20
21
  apiBaseUrl: string;
21
22
  taxProvider: string;
@@ -24,6 +25,7 @@ export default function Checkout({
24
25
  disableMissingImage?: boolean;
25
26
  CollectExtraInfo?: React.FC<{ formik: FormikConfig<any> }>;
26
27
  defaultValues?: Record<string, any>;
28
+ dev?: boolean;
27
29
  }) {
28
30
  const store = useStore(cartStore);
29
31
  const cart = useMemo(() => JSON.parse(store), [store]);
@@ -32,7 +34,7 @@ export default function Checkout({
32
34
  const [products, setProducts] = useState({});
33
35
  const [prices, setPrices] = useState({});
34
36
  const [subtotal, setSubtotal] = useState("Loading prices...");
35
- const taxState = useState({ tax_amount: "TBD" });
37
+ const taxState = useState<any>({ tax_amount: "TBD" });
36
38
  const shipping = 0;
37
39
  const discount = 0;
38
40
 
@@ -211,6 +213,7 @@ export default function Checkout({
211
213
  }}
212
214
  CollectExtraInfo={CollectExtraInfo}
213
215
  defaultValues={defaultValues}
216
+ dev={dev}
214
217
  />
215
218
  )}
216
219
  {status === 2 && order !== undefined && (