@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.
Files changed (35) hide show
  1. package/.eslintrc.cjs +21 -21
  2. package/README.md +64 -64
  3. package/dist/index.js +9778 -9696
  4. package/dist/index.umd.cjs +66 -92
  5. package/package.json +5 -3
  6. package/src/AddToCartForm.tsx +16 -16
  7. package/src/CartButton.tsx +249 -249
  8. package/src/ProductCard.css +106 -106
  9. package/src/ProductCard.tsx +165 -165
  10. package/src/checkout/{CheckoutList.css → ReviewCartAndCalculateTaxes.css} +93 -93
  11. package/src/checkout/ReviewCartAndCalculateTaxes.tsx +366 -0
  12. package/src/checkout/components/AddCard.tsx +267 -0
  13. package/src/checkout/components/Address.tsx +261 -265
  14. package/src/checkout/components/CartList.tsx +151 -0
  15. package/src/checkout/components/CartProductCard.css +67 -67
  16. package/src/checkout/components/CartProductCard.tsx +90 -80
  17. package/src/checkout/components/Invoice.tsx +145 -0
  18. package/src/checkout/components/ProviderLogos.tsx +93 -93
  19. package/src/checkout/components/StatusBar.tsx +32 -0
  20. package/src/checkout/index.tsx +161 -0
  21. package/src/index.css +5 -5
  22. package/src/index.ts +36 -35
  23. package/src/types.d.ts +56 -56
  24. package/src/utils/api.ts +67 -67
  25. package/src/utils/cartAuthHandler.ts +50 -50
  26. package/src/utils/index.ts +28 -28
  27. package/src/utils/storage.ts +133 -133
  28. package/tsconfig.json +24 -24
  29. package/tsconfig.node.json +11 -11
  30. package/vite.config.ts +25 -25
  31. package/springmicro-cart-0.2.3.tgz +0 -0
  32. package/src/checkout/CheckoutList.tsx +0 -264
  33. package/src/checkout/components/Billing.tsx +0 -353
  34. package/src/checkout/components/Order.tsx +0 -93
  35. package/src/checkout/components/index.tsx +0 -104
@@ -1,265 +1,261 @@
1
- import { FormikProps } from "formik";
2
- import {
3
- allCountries,
4
- allProvinces,
5
- allCountriesReverse,
6
- getRegionLabel,
7
- getPostalCodeLabel,
8
- getPostalCodeDefault,
9
- provinces,
10
- } from "@springmicro/utils/address";
11
- import type { Alpha2Code, Province } from "@springmicro/utils/address";
12
- import React from "react";
13
- import { useEffect, useState } from "react";
14
- import { SxProps } from "@mui/material";
15
- import MenuItem from "@mui/material/MenuItem";
16
- import Box from "@mui/material/Box";
17
- import TextField, { StandardTextFieldProps } from "@mui/material/TextField";
18
- import Autocomplete from "@mui/material/Autocomplete";
19
-
20
- export type AddressValues = {
21
- country: string;
22
- region: string;
23
- line1: string;
24
- line2?: string;
25
- organization?: string;
26
- email: string;
27
- city: string;
28
- postal_code: string;
29
- };
30
-
31
- export type AddressFieldProps = {
32
- formik: FormikProps<AddressValues & any>; // formik object might contain other values in addition to address
33
- name: keyof AddressValues;
34
- sx?: SxProps;
35
- };
36
-
37
- export function AddressCountryField({ formik, name, sx }: AddressFieldProps) {
38
- /**
39
- * Selects the full country name, need to do a reverse lookup for the Alpha2Code.
40
- */
41
- const [inputValue, setInputValue] = useState(formik.values[name] as string);
42
-
43
- return (
44
- <Autocomplete
45
- sx={{ width: 300, ...sx }}
46
- options={Object.values(allCountries)}
47
- autoHighlight
48
- getOptionLabel={(option) => option}
49
- value={formik.values[name] as string}
50
- onChange={(e, newValue) => {
51
- // console.log(newValue)
52
- formik.setValues({
53
- ...formik.values,
54
- region: "",
55
- country: newValue as string,
56
- line1: "",
57
- line2: "",
58
- organization: "",
59
- city: "",
60
- postal_code: "",
61
- });
62
- }}
63
- onBlur={formik.handleBlur}
64
- inputValue={inputValue}
65
- onInputChange={(event, newInputValue) => {
66
- setInputValue(newInputValue);
67
- }}
68
- renderOption={(props, option) => (
69
- <Box
70
- component="li"
71
- sx={{ "& > img": { mr: 2, flexShrink: 0 } }}
72
- {...props}
73
- >
74
- <img
75
- loading="lazy"
76
- width="20"
77
- src={`https://flagcdn.com/w20/${allCountriesReverse[
78
- option
79
- ].toLowerCase()}.png`}
80
- srcSet={`https://flagcdn.com/w40/${allCountriesReverse[
81
- option
82
- ].toLowerCase()}.png 2x`}
83
- alt=""
84
- />
85
- {option}
86
- </Box>
87
- )}
88
- renderInput={(params) => (
89
- <TextField
90
- {...params}
91
- required={true}
92
- name={name as string}
93
- label="Country"
94
- />
95
- )}
96
- />
97
- );
98
- }
99
-
100
- export function AddressRegionField({ formik, name, sx }: AddressFieldProps) {
101
- const [addressRegionFieldOptional, setAddressRegionFieldOptional] =
102
- useState<boolean>(false);
103
- const country2code = allCountriesReverse[
104
- formik.values["country"] as string
105
- ] as Alpha2Code;
106
-
107
- useEffect(() => {
108
- setAddressRegionFieldOptional(
109
- provinces.filter(
110
- (province: Province) => province.country === country2code
111
- ).length === 0
112
- );
113
- }, [country2code]);
114
-
115
- const baseProps = {
116
- sx: { width: 300, ...sx },
117
- label: getRegionLabel(country2code),
118
- name: name as string,
119
- id: name as string,
120
- value: formik.values[name],
121
- onChange: formik.handleChange,
122
- onBlur: formik.handleBlur,
123
- };
124
-
125
- if (!country2code || !getRegionLabel(country2code)) {
126
- return <></>;
127
- }
128
-
129
- if (addressRegionFieldOptional) {
130
- return (
131
- <TextField
132
- {...baseProps}
133
- required={false}
134
- sx={{ display: "block", ...baseProps.sx }}
135
- />
136
- );
137
- } else {
138
- return (
139
- <TextField {...baseProps} select required={true}>
140
- {allProvinces
141
- .filter((val) => val.country === country2code)
142
- .map((province, i) => (
143
- <MenuItem key={i} value={province.value}>
144
- {province.name}
145
- </MenuItem>
146
- ))}
147
- </TextField>
148
- );
149
- }
150
- }
151
-
152
- export function AddressStreetField({
153
- formik,
154
- name,
155
- sx,
156
- required = true,
157
- lineNo,
158
- }: AddressFieldProps & {
159
- required?: boolean;
160
- lineNo?: "1" | "2" | "3" | undefined;
161
- }) {
162
- return (
163
- <>
164
- <TextField
165
- label={lineNo ? `Street Address ${lineNo}` : "Street Address"}
166
- sx={{ width: 400, display: "block", ...sx }}
167
- name={name as string}
168
- id={name as string}
169
- value={formik.values[name]}
170
- onChange={formik.handleChange}
171
- onBlur={formik.handleBlur}
172
- required={required}
173
- />
174
- </>
175
- );
176
- }
177
-
178
- export function AddressEmailField({ formik, name, sx }: AddressFieldProps) {
179
- return (
180
- <>
181
- <TextField
182
- label={"Email"}
183
- sx={{ width: 400, display: "block", ...sx }}
184
- name={name as string}
185
- id={name as string}
186
- value={formik.values[name]}
187
- onChange={formik.handleChange}
188
- onBlur={formik.handleBlur}
189
- type="email"
190
- required={true}
191
- />
192
- </>
193
- );
194
- }
195
-
196
- export function AddressOrganizationNameField({
197
- formik,
198
- name,
199
- sx,
200
- }: AddressFieldProps) {
201
- return (
202
- <TextField
203
- label="Organization Name"
204
- sx={{ width: 400, display: "block", ...sx }}
205
- name={name as string}
206
- id={name as string}
207
- value={formik.values[name]}
208
- onChange={formik.handleChange}
209
- onBlur={formik.handleBlur}
210
- required={false}
211
- helperText="Only include organization name if you want it included on the address."
212
- />
213
- );
214
- }
215
-
216
- export function AddressCityField({ formik, name, sx }: AddressFieldProps) {
217
- return (
218
- <TextField
219
- label="City"
220
- sx={{ width: 400, display: "block", ...sx }}
221
- name={name as string}
222
- id={name as string}
223
- value={formik.values[name]}
224
- onChange={formik.handleChange}
225
- onBlur={formik.handleBlur}
226
- required={true}
227
- />
228
- );
229
- }
230
-
231
- export function AddressPostalCodeField({
232
- formik,
233
- name,
234
- sx,
235
- }: AddressFieldProps) {
236
- const country2code = allCountriesReverse[
237
- formik.values["country"] as string
238
- ] as Alpha2Code;
239
-
240
- React.useEffect(() => {
241
- if (getPostalCodeDefault(country2code)) {
242
- formik
243
- .setFieldValue(name, getPostalCodeDefault(country2code))
244
- .then(() => formik.setFieldTouched(name, true));
245
- }
246
- }, [country2code]);
247
-
248
- return (
249
- <TextField
250
- label={getPostalCodeLabel(country2code)}
251
- sx={{ width: 400, display: "block", ...sx }}
252
- name={name as string}
253
- id={name as string}
254
- value={formik.values[name]}
255
- onChange={formik.handleChange}
256
- onBlur={formik.handleBlur}
257
- required={true}
258
- helperText={
259
- getPostalCodeDefault(country2code)
260
- ? 'Our records indicate that your country does not have a postal code system, so "00000" will be used by default.'
261
- : ""
262
- }
263
- />
264
- );
265
- }
1
+ import { FormikProps } from "formik";
2
+ import {
3
+ allCountries,
4
+ allProvinces,
5
+ allCountriesReverse,
6
+ getRegionLabel,
7
+ getPostalCodeLabel,
8
+ getPostalCodeDefault,
9
+ provinces,
10
+ } from "@springmicro/utils/address";
11
+ import type { Alpha2Code, Province } from "@springmicro/utils/address";
12
+ import React from "react";
13
+ import { useEffect, useState } from "react";
14
+ import { SxProps } from "@mui/material";
15
+ import MenuItem from "@mui/material/MenuItem";
16
+ import Box from "@mui/material/Box";
17
+ import TextField, { StandardTextFieldProps } from "@mui/material/TextField";
18
+ import Autocomplete from "@mui/material/Autocomplete";
19
+
20
+ export type AddressValues = {
21
+ country: string;
22
+ region: string;
23
+ line1: string;
24
+ line2?: string;
25
+ organization?: string;
26
+ email: string;
27
+ city: string;
28
+ postal_code: string;
29
+ };
30
+
31
+ export type AddressFieldProps = {
32
+ formik: FormikProps<AddressValues & any>; // formik object might contain other values in addition to address
33
+ name: keyof AddressValues;
34
+ sx?: SxProps;
35
+ };
36
+
37
+ export function AddressCountryField({ formik, name, sx }: AddressFieldProps) {
38
+ /**
39
+ * Selects the full country name, need to do a reverse lookup for the Alpha2Code.
40
+ */
41
+ const [inputValue, setInputValue] = useState(formik.values[name] as string);
42
+
43
+ return (
44
+ <Autocomplete
45
+ sx={{ width: 300, ...sx }}
46
+ options={Object.keys(allCountries)}
47
+ autoHighlight
48
+ getOptionLabel={(option) => allCountries[option]}
49
+ value={formik.values[name] as string}
50
+ onChange={(e, newValue) => {
51
+ // console.log(newValue)
52
+ formik.setValues({
53
+ ...formik.values,
54
+ region: "",
55
+ country: newValue as string,
56
+ line1: "",
57
+ line2: "",
58
+ organization: "",
59
+ city: "",
60
+ postal_code: "",
61
+ });
62
+ }}
63
+ onBlur={formik.handleBlur}
64
+ inputValue={inputValue}
65
+ onInputChange={(event, newInputValue) => {
66
+ setInputValue(newInputValue);
67
+ }}
68
+ renderOption={(props, option) => (
69
+ <Box
70
+ component="li"
71
+ sx={{ "& > img": { mr: 2, flexShrink: 0 } }}
72
+ {...props}
73
+ >
74
+ <img
75
+ loading="lazy"
76
+ width="20"
77
+ src={`https://flagcdn.com/w20/${allCountriesReverse[
78
+ allCountries[option]
79
+ ].toLowerCase()}.png`}
80
+ srcSet={`https://flagcdn.com/w40/${allCountriesReverse[
81
+ allCountries[option]
82
+ ].toLowerCase()}.png 2x`}
83
+ alt=""
84
+ />
85
+ {allCountries[option]}
86
+ </Box>
87
+ )}
88
+ renderInput={(params) => (
89
+ <TextField
90
+ {...params}
91
+ required={true}
92
+ name={name as string}
93
+ label="Country"
94
+ />
95
+ )}
96
+ />
97
+ );
98
+ }
99
+
100
+ export function AddressRegionField({ formik, name, sx }: AddressFieldProps) {
101
+ const [addressRegionFieldOptional, setAddressRegionFieldOptional] =
102
+ useState<boolean>(false);
103
+ const country2code = formik.values["country"] as Alpha2Code;
104
+
105
+ useEffect(() => {
106
+ setAddressRegionFieldOptional(
107
+ provinces.filter(
108
+ (province: Province) => province.country === country2code
109
+ ).length === 0
110
+ );
111
+ }, [country2code]);
112
+
113
+ const baseProps = {
114
+ sx: { width: 300, ...sx },
115
+ label: getRegionLabel(country2code),
116
+ name: name as string,
117
+ id: name as string,
118
+ value: formik.values[name],
119
+ onChange: formik.handleChange,
120
+ onBlur: formik.handleBlur,
121
+ };
122
+
123
+ if (!country2code || !getRegionLabel(country2code)) {
124
+ return <></>;
125
+ }
126
+
127
+ if (addressRegionFieldOptional) {
128
+ return (
129
+ <TextField
130
+ {...baseProps}
131
+ required={false}
132
+ sx={{ display: "block", ...baseProps.sx }}
133
+ />
134
+ );
135
+ } else {
136
+ return (
137
+ <TextField {...baseProps} select required={true}>
138
+ {allProvinces
139
+ .filter((val) => val.country === country2code)
140
+ .map((province, i) => (
141
+ <MenuItem key={i} value={province.value}>
142
+ {province.name}
143
+ </MenuItem>
144
+ ))}
145
+ </TextField>
146
+ );
147
+ }
148
+ }
149
+
150
+ export function AddressStreetField({
151
+ formik,
152
+ name,
153
+ sx,
154
+ required = true,
155
+ lineNo,
156
+ }: AddressFieldProps & {
157
+ required?: boolean;
158
+ lineNo?: "1" | "2" | "3" | undefined;
159
+ }) {
160
+ return (
161
+ <>
162
+ <TextField
163
+ label={lineNo ? `Street Address ${lineNo}` : "Street Address"}
164
+ sx={{ width: 400, display: "block", ...sx }}
165
+ name={name as string}
166
+ id={name as string}
167
+ value={formik.values[name]}
168
+ onChange={formik.handleChange}
169
+ onBlur={formik.handleBlur}
170
+ required={required}
171
+ />
172
+ </>
173
+ );
174
+ }
175
+
176
+ export function AddressEmailField({ formik, name, sx }: AddressFieldProps) {
177
+ return (
178
+ <>
179
+ <TextField
180
+ label={"Email"}
181
+ sx={{ width: 400, display: "block", ...sx }}
182
+ name={name as string}
183
+ id={name as string}
184
+ value={formik.values[name]}
185
+ onChange={formik.handleChange}
186
+ onBlur={formik.handleBlur}
187
+ type="email"
188
+ required={true}
189
+ />
190
+ </>
191
+ );
192
+ }
193
+
194
+ export function AddressOrganizationNameField({
195
+ formik,
196
+ name,
197
+ sx,
198
+ }: AddressFieldProps) {
199
+ return (
200
+ <TextField
201
+ label="Organization Name"
202
+ sx={{ width: 400, display: "block", ...sx }}
203
+ name={name as string}
204
+ id={name as string}
205
+ value={formik.values[name]}
206
+ onChange={formik.handleChange}
207
+ onBlur={formik.handleBlur}
208
+ required={false}
209
+ helperText="Only include organization name if you want it included on the address."
210
+ />
211
+ );
212
+ }
213
+
214
+ export function AddressCityField({ formik, name, sx }: AddressFieldProps) {
215
+ return (
216
+ <TextField
217
+ label="City"
218
+ sx={{ width: 400, display: "block", ...sx }}
219
+ name={name as string}
220
+ id={name as string}
221
+ value={formik.values[name]}
222
+ onChange={formik.handleChange}
223
+ onBlur={formik.handleBlur}
224
+ required={true}
225
+ />
226
+ );
227
+ }
228
+
229
+ export function AddressPostalCodeField({
230
+ formik,
231
+ name,
232
+ sx,
233
+ }: AddressFieldProps) {
234
+ const country2code = formik.values["country"] as Alpha2Code;
235
+
236
+ React.useEffect(() => {
237
+ if (getPostalCodeDefault(country2code)) {
238
+ formik
239
+ .setFieldValue(name, getPostalCodeDefault(country2code))
240
+ .then(() => formik.setFieldTouched(name, true));
241
+ }
242
+ }, [country2code]);
243
+
244
+ return (
245
+ <TextField
246
+ label={getPostalCodeLabel(country2code)}
247
+ sx={{ width: 400, display: "block", ...sx }}
248
+ name={name as string}
249
+ id={name as string}
250
+ value={formik.values[name]}
251
+ onChange={formik.handleChange}
252
+ onBlur={formik.handleBlur}
253
+ required={true}
254
+ helperText={
255
+ getPostalCodeDefault(country2code)
256
+ ? 'Our records indicate that your country does not have a postal code system, so "00000" will be used by default.'
257
+ : ""
258
+ }
259
+ />
260
+ );
261
+ }
@@ -0,0 +1,151 @@
1
+ import { Box, Button, Tooltip, Typography } from "@mui/material";
2
+ import { CartProductCard } from "./CartProductCard";
3
+
4
+ function formatPrice(cents) {
5
+ if (typeof cents === "string") return cents;
6
+ return `$${(cents / 100).toFixed(2)}`;
7
+ }
8
+
9
+ export function CartList({
10
+ status,
11
+ cart,
12
+ subtotal,
13
+ tax,
14
+ shipping,
15
+ discount,
16
+ prices,
17
+ disableMissingImage,
18
+ disableProductLink,
19
+ formik,
20
+ formDisabled,
21
+ formError,
22
+ }) {
23
+ return (
24
+ <form
25
+ style={{ flexGrow: 1 }}
26
+ onSubmit={(e) => {
27
+ e.preventDefault();
28
+ formik.handleSubmit(e);
29
+ }}
30
+ >
31
+ <Box
32
+ sx={{
33
+ display: "flex",
34
+ justifyContent: "center",
35
+ flexGrow: 1,
36
+ mb: { xs: 4, xl: undefined },
37
+ }}
38
+ >
39
+ <Box
40
+ sx={{
41
+ display: "flex",
42
+ flexDirection: "column",
43
+ maxWidth: 650,
44
+ alignItems: "center",
45
+ boxShadow: "0px 2px 5px 2px #dfdfdfff",
46
+ margin: "4px",
47
+ padding: "2rem 0",
48
+ borderRadius: "8px",
49
+ flexGrow: 1,
50
+ }}
51
+ >
52
+ <Typography style={{ fontSize: "30px", fontWeight: "bold" }}>
53
+ Checkout
54
+ </Typography>
55
+ <Box
56
+ sx={{
57
+ width: "50%",
58
+ display: "flex",
59
+ justifyContent: "space-between",
60
+ }}
61
+ >
62
+ <Typography sx={{ fontSize: "20px", p: 1 }}>
63
+ Subtotal: {formatPrice(subtotal)}
64
+ </Typography>
65
+ {tax.error ? (
66
+ <Tooltip
67
+ disableInteractive
68
+ title={`Tax could not be calculated. Error: ${tax.error}`}
69
+ >
70
+ <Typography
71
+ sx={{
72
+ fontSize: "20px",
73
+ bgcolor: "#ff000040",
74
+ borderRadius: 2,
75
+ p: 1,
76
+ textDecoration: "underline dotted",
77
+ cursor: "help",
78
+ }}
79
+ >
80
+ Taxes: {formatPrice(tax.tax_amount)}
81
+ </Typography>
82
+ </Tooltip>
83
+ ) : (
84
+ <Typography sx={{ fontSize: "20px", p: 1 }}>
85
+ Taxes: {formatPrice(tax.tax_amount)}
86
+ </Typography>
87
+ )}
88
+ </Box>
89
+ {(discount != 0 || shipping != 0) && (
90
+ <Box
91
+ sx={{
92
+ width: "50%",
93
+ display: "flex",
94
+ justifyContent: "space-between",
95
+ }}
96
+ >
97
+ <Typography sx={{ fontSize: "20px", p: 1 }}>
98
+ {shipping && <>Shipping: {formatPrice(shipping)}</>}
99
+ </Typography>
100
+ <Typography sx={{ fontSize: "20px", p: 1 }}>
101
+ {discount && <>Discount: {formatPrice(discount)}</>}
102
+ </Typography>
103
+ </Box>
104
+ )}
105
+ <Box>
106
+ <Typography style={{ fontSize: "26px" }}>
107
+ Total:{" "}
108
+ {typeof subtotal === "number"
109
+ ? formatPrice(
110
+ subtotal +
111
+ (tax.tax_amount === "TBD" ? 0 : tax.tax_amount) +
112
+ shipping -
113
+ discount
114
+ )
115
+ : subtotal}
116
+ </Typography>
117
+ </Box>
118
+ {formError && (
119
+ <Typography color="red">
120
+ Could not confirm payment. Your card info may have been entered
121
+ incorrectly.
122
+ </Typography>
123
+ )}
124
+ {status === 1 && (
125
+ <Button
126
+ variant="contained"
127
+ sx={{ mt: 2 }}
128
+ type="submit"
129
+ disabled={formDisabled}
130
+ color={formError ? "error" : undefined}
131
+ >
132
+ Confirm Order
133
+ </Button>
134
+ )}
135
+ <Box className="checkout-list">
136
+ {cart.items.map((p, i) => (
137
+ <CartProductCard
138
+ product={p}
139
+ i={i}
140
+ price={prices[p.price_id]}
141
+ disableProductLink={disableProductLink}
142
+ disableMissingImage={disableMissingImage}
143
+ disableModification={status != 0}
144
+ />
145
+ ))}
146
+ </Box>
147
+ </Box>
148
+ </Box>
149
+ </form>
150
+ );
151
+ }