@blocklet/payment-react 1.18.35 → 1.18.37
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/es/components/country-select.d.ts +1 -0
- package/es/components/country-select.js +359 -276
- package/es/contexts/payment.js +21 -1
- package/es/libs/cached-request.d.ts +1 -1
- package/es/payment/form/address.js +6 -6
- package/es/payment/form/index.js +1 -1
- package/es/payment/form/phone.js +2 -1
- package/es/payment/form/stripe/form.js +1 -0
- package/lib/components/country-select.d.ts +1 -0
- package/lib/components/country-select.js +188 -80
- package/lib/contexts/payment.js +21 -0
- package/lib/libs/cached-request.d.ts +1 -1
- package/lib/payment/form/address.js +7 -6
- package/lib/payment/form/index.js +1 -1
- package/lib/payment/form/phone.js +2 -1
- package/lib/payment/form/stripe/form.js +1 -0
- package/package.json +6 -6
- package/src/components/country-select.tsx +381 -290
- package/src/contexts/payment.tsx +29 -1
- package/src/libs/cached-request.ts +1 -1
- package/src/payment/form/address.tsx +6 -6
- package/src/payment/form/index.tsx +1 -1
- package/src/payment/form/phone.tsx +1 -0
- package/src/payment/form/stripe/form.tsx +1 -0
package/src/contexts/payment.tsx
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/indent */
|
|
1
2
|
import type { TPaymentCurrency, TPaymentMethodExpanded } from '@blocklet/payment-types';
|
|
2
3
|
import { Alert } from '@mui/material';
|
|
3
4
|
import { useLocalStorageState, useRequest } from 'ahooks';
|
|
4
5
|
import type { Axios } from 'axios';
|
|
5
|
-
import { createContext, useContext, useState } from 'react';
|
|
6
|
+
import { createContext, useContext, useEffect, useState } from 'react';
|
|
6
7
|
|
|
7
8
|
import api from '../libs/api';
|
|
8
9
|
import { getPrefix } from '../libs/util';
|
|
@@ -74,6 +75,19 @@ const getMethod = (methodId: string, methods: TPaymentMethodExpanded[]) => {
|
|
|
74
75
|
return methods.find((x) => x.id === methodId);
|
|
75
76
|
};
|
|
76
77
|
|
|
78
|
+
const syncToSpaceRequest = (userDid: string, spaceDid: string) => {
|
|
79
|
+
const cacheKey = `sync-space-${userDid}-${spaceDid}`;
|
|
80
|
+
const cachedRequest = new CachedRequest(cacheKey, () => api.post('/api/customers/sync-to-space'), {
|
|
81
|
+
ttl: 1000 * 60 * 60, // 1 hour
|
|
82
|
+
});
|
|
83
|
+
return cachedRequest.fetch(false).then((res) => {
|
|
84
|
+
if (!res.success) {
|
|
85
|
+
cachedRequest.clearCache();
|
|
86
|
+
}
|
|
87
|
+
return res;
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
|
|
77
91
|
function PaymentProvider({ session, connect, children, baseUrl, authToken }: PaymentContextProps) {
|
|
78
92
|
if (baseUrl) {
|
|
79
93
|
// This is hack but efficient to share
|
|
@@ -101,6 +115,20 @@ function PaymentProvider({ session, connect, children, baseUrl, authToken }: Pay
|
|
|
101
115
|
} = useRequest(getSettings, {
|
|
102
116
|
refreshDeps: [livemode],
|
|
103
117
|
});
|
|
118
|
+
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
const didSpace = session?.user?.didSpace as
|
|
121
|
+
| {
|
|
122
|
+
endpoint: string;
|
|
123
|
+
did: string;
|
|
124
|
+
}
|
|
125
|
+
| undefined;
|
|
126
|
+
const userDid = session?.user?.did;
|
|
127
|
+
if (userDid && didSpace && didSpace.endpoint && didSpace.did) {
|
|
128
|
+
syncToSpaceRequest(userDid, didSpace.did);
|
|
129
|
+
}
|
|
130
|
+
}, [session?.user]);
|
|
131
|
+
|
|
104
132
|
const prefix = getPrefix();
|
|
105
133
|
const [payable, setPayable] = useState(true);
|
|
106
134
|
|
|
@@ -21,7 +21,7 @@ AddressForm.defaultProps = {
|
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
export default function AddressForm({ mode, stripe, sx = {}, fieldValidation, errorPosition }: Props) {
|
|
24
|
-
const { t } = useLocaleContext();
|
|
24
|
+
const { t, locale } = useLocaleContext();
|
|
25
25
|
const { control } = useFormContext();
|
|
26
26
|
const country = useWatch({ control, name: 'billing_address.country' });
|
|
27
27
|
if (mode === 'required') {
|
|
@@ -34,7 +34,7 @@ export default function AddressForm({ mode, stripe, sx = {}, fieldValidation, er
|
|
|
34
34
|
name="billing_address.line1"
|
|
35
35
|
rules={{
|
|
36
36
|
required: t('payment.checkout.required'),
|
|
37
|
-
...getFieldValidation('billing_address.line1', fieldValidation),
|
|
37
|
+
...getFieldValidation('billing_address.line1', fieldValidation, locale),
|
|
38
38
|
}}
|
|
39
39
|
errorPosition={errorPosition}
|
|
40
40
|
variant="outlined"
|
|
@@ -45,7 +45,7 @@ export default function AddressForm({ mode, stripe, sx = {}, fieldValidation, er
|
|
|
45
45
|
name="billing_address.city"
|
|
46
46
|
rules={{
|
|
47
47
|
required: t('payment.checkout.required'),
|
|
48
|
-
...getFieldValidation('billing_address.city', fieldValidation),
|
|
48
|
+
...getFieldValidation('billing_address.city', fieldValidation, locale),
|
|
49
49
|
}}
|
|
50
50
|
errorPosition={errorPosition}
|
|
51
51
|
variant="outlined"
|
|
@@ -56,7 +56,7 @@ export default function AddressForm({ mode, stripe, sx = {}, fieldValidation, er
|
|
|
56
56
|
name="billing_address.state"
|
|
57
57
|
rules={{
|
|
58
58
|
required: t('payment.checkout.required'),
|
|
59
|
-
...getFieldValidation('billing_address.state', fieldValidation),
|
|
59
|
+
...getFieldValidation('billing_address.state', fieldValidation, locale),
|
|
60
60
|
}}
|
|
61
61
|
errorPosition={errorPosition}
|
|
62
62
|
variant="outlined"
|
|
@@ -71,7 +71,7 @@ export default function AddressForm({ mode, stripe, sx = {}, fieldValidation, er
|
|
|
71
71
|
const isValid = validatePostalCode(x, country);
|
|
72
72
|
return isValid ? true : t('payment.checkout.invalid');
|
|
73
73
|
},
|
|
74
|
-
...getFieldValidation('billing_address.postal_code', fieldValidation),
|
|
74
|
+
...getFieldValidation('billing_address.postal_code', fieldValidation, locale),
|
|
75
75
|
}}
|
|
76
76
|
errorPosition={errorPosition}
|
|
77
77
|
variant="outlined"
|
|
@@ -118,7 +118,7 @@ export default function AddressForm({ mode, stripe, sx = {}, fieldValidation, er
|
|
|
118
118
|
const isValid = validatePostalCode(x, country);
|
|
119
119
|
return isValid ? true : t('payment.checkout.invalid');
|
|
120
120
|
},
|
|
121
|
-
...getFieldValidation('billing_address.postal_code', fieldValidation),
|
|
121
|
+
...getFieldValidation('billing_address.postal_code', fieldValidation, locale),
|
|
122
122
|
}}
|
|
123
123
|
errorPosition={errorPosition}
|
|
124
124
|
variant="outlined"
|
|
@@ -177,7 +177,7 @@ export default function PaymentForm({
|
|
|
177
177
|
const { isMobile } = useMobile();
|
|
178
178
|
const { session, connect, payable } = usePaymentContext();
|
|
179
179
|
const subscription = useSubscription('events');
|
|
180
|
-
const formErrorPosition =
|
|
180
|
+
const formErrorPosition = 'bottom';
|
|
181
181
|
const {
|
|
182
182
|
control,
|
|
183
183
|
getValues,
|