@springmicro/cart 0.7.23 → 0.7.24

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.7.23",
4
+ "version": "0.7.24",
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.7.23",
27
+ "@springmicro/utils": "^0.7.24",
28
28
  "dotenv": "^16.4.5",
29
29
  "nanostores": "^0.10.3",
30
30
  "react": "^19.2.4",
@@ -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": "d1f4ad0110ef4cf25f65cc1b59886624db416a78"
52
+ "gitHead": "e261e94677f6c7b48082834e98f63b927bc3585f"
53
53
  }
@@ -41,7 +41,7 @@ import CloseIcon from "@mui/icons-material/Close";
41
41
 
42
42
  async function createOrder(
43
43
  cart,
44
- customer_create,
44
+ billing_information,
45
45
  apiBaseUrl,
46
46
  affiliateCode,
47
47
  dev,
@@ -69,18 +69,22 @@ async function createOrder(
69
69
  "Content-Type": "application/json",
70
70
  },
71
71
  body: JSON.stringify({
72
- customer_create,
73
72
  cart_data: {
74
73
  items: cart.items.map((li) => ({
75
74
  ...li,
76
75
  name: undefined,
77
76
  image: undefined,
78
77
  })),
78
+ billing_information,
79
79
  },
80
80
  affiliate_code: affiliateCode,
81
81
  }),
82
82
  credentials: "include",
83
83
  });
84
+ if (!res.ok) {
85
+ throw "Something when wrong when creating your order";
86
+ }
87
+
84
88
  const order = await res.json();
85
89
 
86
90
  if (!order) throw "Missing order";
@@ -93,49 +97,61 @@ async function createOrder(
93
97
  return order;
94
98
  }
95
99
 
96
- async function getTax(apiBaseUrl, taxProvider, items, v, products, dev) {
97
- dev && console.log("Getting tax", items);
98
- dev &&
99
- console.log(
100
- "Getting product data",
101
- products,
102
- products[items[0].product_id],
103
- );
104
- const taxable_items = items.filter(
105
- (i) => i.unit_amount > 0 && products[i.product_id].type != "DONATION",
106
- );
107
- dev && console.log("Taxable items", taxable_items);
108
- if (taxable_items.length === 0) return { tax_amount: 0 };
109
-
110
- const res = await fetch(
111
- `${apiBaseUrl}/api/ecommerce/tax?tax_provider=${taxProvider ?? "dummy"}`,
112
- {
113
- method: "POST",
114
- body: JSON.stringify({
115
- cart_data: taxable_items.map((i) => ({
116
- amount: i.unit_amount,
117
- reference: i.reference,
118
- })),
119
- address: {
120
- country: v.country,
121
- city: v.locality,
122
- line1: v.street_address,
123
- postal_code: v.postal_code,
124
- state: v.region,
125
- },
126
- }),
127
- headers: {
128
- "Content-Type": "application/json",
129
- },
130
- },
131
- );
132
- return await res.json();
133
- }
100
+ // async function getTax(apiBaseUrl, taxProvider, items, v, products, dev) {
101
+ // dev && console.log("Getting tax", items);
102
+ // dev &&
103
+ // console.log(
104
+ // "Getting product data",
105
+ // products,
106
+ // products[items[0].product_id],
107
+ // );
108
+ // const taxable_items = items.filter(
109
+ // (i) => i.unit_amount > 0 && products[i.product_id].type != "DONATION",
110
+ // );
111
+ // dev && console.log("Taxable items", taxable_items);
112
+ // if (taxable_items.length === 0) return { tax_amount: 0 };
113
+
114
+ // const res = await fetch(
115
+ // `${apiBaseUrl}/api/ecommerce/tax?tax_provider=${taxProvider ?? "dummy"}`,
116
+ // {
117
+ // method: "POST",
118
+ // body: JSON.stringify({
119
+ // cart_data: taxable_items.map((i) => ({
120
+ // amount: i.unit_amount,
121
+ // reference: i.reference,
122
+ // })),
123
+ // address: {
124
+ // country: v.country,
125
+ // city: v.locality,
126
+ // line1: v.street_address,
127
+ // postal_code: v.postal_code,
128
+ // state: v.region,
129
+ // },
130
+ // }),
131
+ // headers: {
132
+ // "Content-Type": "application/json",
133
+ // },
134
+ // },
135
+ // );
136
+ // return await res.json();
137
+ // }
138
+
139
+ // getTax(
140
+ // apiBaseUrl,
141
+ // taxProvider,
142
+ // cart.items.map((i) => ({
143
+ // unit_amount: prices[i.price_id].unit_amount,
144
+ // reference: i.name,
145
+ // product_id: i.product_id,
146
+ // })),
147
+ // values,
148
+ // products,
149
+ // dev,
150
+ // ),
134
151
 
135
152
  export default function ReviewAndCalculateTaxes({
136
153
  subtotal,
137
154
  discount,
138
- taxState,
139
155
  shipping,
140
156
  statusState,
141
157
  cart,
@@ -163,7 +179,6 @@ export default function ReviewAndCalculateTaxes({
163
179
  const stacked = useMediaQuery(theme.breakpoints.down("lg"));
164
180
 
165
181
  const [status, setStatus] = statusState;
166
- const [tax, setTax] = taxState;
167
182
  const [order, setOrder] = orderState;
168
183
 
169
184
  const [precheckErrors, setPrecheckErrors] = useState([]);
@@ -174,32 +189,23 @@ export default function ReviewAndCalculateTaxes({
174
189
  // status === 0 when this is clicked.
175
190
  // Creates an order and calculates tax.
176
191
  dev && console.log("Cart items", cart.items);
177
- Promise.all([
178
- createOrder(
179
- cart,
180
- {
181
- email: values.email,
182
- name: values.name,
183
- },
184
- apiBaseUrl,
185
- affiliateCode,
186
- dev,
187
- ),
188
- getTax(
189
- apiBaseUrl,
190
- taxProvider,
191
- cart.items.map((i) => ({
192
- unit_amount: prices[i.price_id].unit_amount,
193
- reference: i.name,
194
- product_id: i.product_id,
195
- })),
196
- values,
197
- products,
198
- dev,
199
- ),
200
- ])
201
- .then(([order, taxData]) => {
202
- setTax(taxData);
192
+
193
+ createOrder(
194
+ cart,
195
+ {
196
+ email: values.email,
197
+ name: values.name,
198
+ country: values.country,
199
+ locality: values.locality,
200
+ street_address: values.street_address,
201
+ postal_code: values.postal_code,
202
+ state: values.region,
203
+ },
204
+ apiBaseUrl,
205
+ affiliateCode,
206
+ dev,
207
+ )
208
+ .then((order) => {
203
209
  setOrder(order);
204
210
  setStatus(1);
205
211
  setFormDisabled(false);
@@ -423,7 +429,7 @@ export default function ReviewAndCalculateTaxes({
423
429
  statusState: [status, setStatus],
424
430
  cart,
425
431
  subtotal,
426
- tax,
432
+ orderState,
427
433
  shipping,
428
434
  discount,
429
435
  prices,
@@ -10,7 +10,7 @@ export function CartList({
10
10
  statusState,
11
11
  cart,
12
12
  subtotal,
13
- tax,
13
+ orderState,
14
14
  shipping,
15
15
  discount,
16
16
  prices,
@@ -23,6 +23,9 @@ export function CartList({
23
23
  }) {
24
24
  const [status, setStatus] = statusState;
25
25
  const [formError, setFormError] = formErrorState;
26
+ const [order] = orderState;
27
+ const { taxes = "Pending", total } = order ?? {};
28
+
26
29
  return (
27
30
  <form
28
31
  style={{ flexGrow: 1 }}
@@ -65,29 +68,9 @@ export function CartList({
65
68
  <Typography sx={{ fontSize: "20px", p: 1 }}>
66
69
  Subtotal: {formatPrice(subtotal)}
67
70
  </Typography>
68
- {tax.error ? (
69
- <Tooltip
70
- disableInteractive
71
- title={`Tax could not be calculated. Error: ${tax.error}`}
72
- >
73
- <Typography
74
- sx={{
75
- fontSize: "20px",
76
- bgcolor: "#ff000040",
77
- borderRadius: 2,
78
- p: 1,
79
- textDecoration: "underline dotted",
80
- cursor: "help",
81
- }}
82
- >
83
- Taxes: {formatPrice(tax.tax_amount)}
84
- </Typography>
85
- </Tooltip>
86
- ) : (
87
- <Typography sx={{ fontSize: "20px", p: 1 }}>
88
- Taxes: {formatPrice(tax.tax_amount)}
89
- </Typography>
90
- )}
71
+ <Typography sx={{ fontSize: "20px", p: 1 }}>
72
+ Taxes: {formatPrice(taxes)}
73
+ </Typography>
91
74
  </Box>
92
75
  {(discount != 0 || shipping != 0) && (
93
76
  <Box
@@ -108,12 +91,12 @@ export function CartList({
108
91
  <Box>
109
92
  <Typography style={{ fontSize: "26px" }}>
110
93
  Total:{" "}
111
- {typeof subtotal === "number"
94
+ {total || typeof subtotal === "number"
112
95
  ? formatPrice(
113
96
  subtotal +
114
- (tax.tax_amount === "TBD" ? 0 : tax.tax_amount) +
97
+ (typeof taxes === "string" ? 0 : taxes) +
115
98
  shipping -
116
- discount
99
+ discount,
117
100
  )
118
101
  : subtotal}
119
102
  </Typography>
@@ -156,7 +139,7 @@ export function CartList({
156
139
  <Box className="checkout-list">
157
140
  {cart.items.map((p, i) => (
158
141
  <CartProductCard
159
- product={products[p.product_id] ?? p}
142
+ product={{ ...p, ...(products[p.product_id] ?? {}) }}
160
143
  i={i}
161
144
  price={prices[p.price_id]}
162
145
  disableProductLink={disableProductLink}
@@ -38,7 +38,9 @@ export function CartProductCard({
38
38
  !disableMissingImage && <div className="missing-image" />
39
39
  )}
40
40
  <div className="two-row">
41
- <Typography>{product.name}</Typography>
41
+ <Typography>
42
+ {product.name.replace("%s", product.hidden_id)}
43
+ </Typography>
42
44
  <Typography>
43
45
  {price
44
46
  ? `$${(price.unit_amount / 100).toFixed(2)}${
@@ -47,18 +47,17 @@ export default function Checkout({
47
47
  }) {
48
48
  const store = useStore(cartStore);
49
49
  const localCart = useMemo(() => JSON.parse(store), [store]);
50
-
51
- const cart = invoiceId ? { items: _order.basket } : localCart; // todo fix calculation
50
+ const [order, setOrder] = useState(_order); // starts undefined on a typical checkout. If an order is provided, it's likely an invoice.
51
+ console.log("CART", invoiceId, order, localCart);
52
+ const cart = invoiceId ? { items: order.basket } : localCart; // todo fix calculation
52
53
 
53
54
  const [products, setProducts] = useState({});
54
55
  const [prices, setPrices] = useState({});
55
56
  const [subtotal, setSubtotal] = useState("Loading prices...");
56
- const taxState = useState<any>({ tax_amount: "TBD" });
57
57
  const shipping = 0;
58
58
  const discount = 0;
59
59
 
60
60
  const [status, setStatus] = useState(0);
61
- const [order, setOrder] = useState(_order); // starts undefined on a typical checkout. If an order is provided, it's likely an invoice.
62
61
  const [successData, setSuccessData] = useState(null);
63
62
 
64
63
  useEffect(() => {
@@ -166,18 +165,6 @@ export default function Checkout({
166
165
  );
167
166
  }, [cart, prices]);
168
167
 
169
- useEffect(() => {
170
- const taxable_items = cart.items.filter(
171
- (i) =>
172
- prices[i.price_id]?.unit_amount > 0 &&
173
- products[i.product_id]?.type !== "DONATION",
174
- );
175
-
176
- if (taxable_items.length === 0) {
177
- taxState[1]({ tax_amount: 0 });
178
- }
179
- }, [cart, prices, products]);
180
-
181
168
  if (status === 0 && cart.items.length === 0)
182
169
  return (
183
170
  <div
@@ -221,7 +208,6 @@ export default function Checkout({
221
208
  discount={discount}
222
209
  shipping={shipping}
223
210
  taxProvider={taxProvider}
224
- taxState={taxState}
225
211
  statusState={[status, setStatus]}
226
212
  cart={cart}
227
213
  prices={prices}