@blocklet/payment-react 1.18.3 → 1.18.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/es/checkout/donate.d.ts +5 -3
- package/es/checkout/donate.js +109 -36
- package/es/contexts/donate.d.ts +41 -0
- package/es/contexts/donate.js +177 -0
- package/es/contexts/payment.js +3 -16
- package/es/index.d.ts +2 -0
- package/es/index.js +2 -0
- package/es/libs/cache.d.ts +14 -0
- package/es/libs/cache.js +23 -0
- package/es/libs/cached-request.d.ts +17 -0
- package/es/libs/cached-request.js +79 -0
- package/es/libs/util.d.ts +2 -0
- package/es/libs/util.js +11 -0
- package/es/locales/en.js +8 -1
- package/es/locales/zh.js +8 -1
- package/es/payment/skeleton/donation.js +1 -1
- package/lib/checkout/donate.d.ts +5 -3
- package/lib/checkout/donate.js +107 -36
- package/lib/contexts/donate.d.ts +41 -0
- package/lib/contexts/donate.js +187 -0
- package/lib/contexts/payment.js +7 -22
- package/lib/index.d.ts +2 -0
- package/lib/index.js +24 -0
- package/lib/libs/cache.d.ts +14 -0
- package/lib/libs/cache.js +29 -0
- package/lib/libs/cached-request.d.ts +17 -0
- package/lib/libs/cached-request.js +90 -0
- package/lib/libs/util.d.ts +2 -0
- package/lib/libs/util.js +13 -0
- package/lib/locales/en.js +8 -1
- package/lib/locales/zh.js +8 -1
- package/lib/payment/skeleton/donation.js +1 -1
- package/package.json +6 -6
- package/src/checkout/donate.tsx +135 -45
- package/src/contexts/donate.tsx +234 -0
- package/src/contexts/payment.tsx +5 -20
- package/src/index.ts +2 -0
- package/src/libs/cache.ts +33 -0
- package/src/libs/cached-request.ts +103 -0
- package/src/libs/util.ts +13 -0
- package/src/locales/en.tsx +7 -0
- package/src/locales/zh.tsx +7 -0
- package/src/payment/skeleton/donation.tsx +1 -1
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { globalCache } from "./cache.js";
|
|
2
|
+
export class CachedRequest {
|
|
3
|
+
cacheKey;
|
|
4
|
+
fetchData;
|
|
5
|
+
options;
|
|
6
|
+
constructor(cacheKey, fetchData, options = {}) {
|
|
7
|
+
this.cacheKey = cacheKey;
|
|
8
|
+
this.fetchData = fetchData;
|
|
9
|
+
this.options = {
|
|
10
|
+
strategy: "session",
|
|
11
|
+
ttl: 0,
|
|
12
|
+
...options
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
getCache() {
|
|
16
|
+
const { strategy } = this.options;
|
|
17
|
+
switch (strategy) {
|
|
18
|
+
case "local":
|
|
19
|
+
return localStorage;
|
|
20
|
+
case "memory":
|
|
21
|
+
return globalCache;
|
|
22
|
+
case "session":
|
|
23
|
+
default:
|
|
24
|
+
return sessionStorage;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
getCachedData() {
|
|
28
|
+
const cache = this.getCache();
|
|
29
|
+
const data = this.options.strategy === "memory" ? cache.get(this.cacheKey) : cache.getItem(this.cacheKey);
|
|
30
|
+
if (!data)
|
|
31
|
+
return null;
|
|
32
|
+
const parsed = JSON.parse(data);
|
|
33
|
+
if (this.options.ttl && parsed.timestamp) {
|
|
34
|
+
const isExpired = Date.now() - parsed.timestamp > this.options.ttl;
|
|
35
|
+
if (isExpired) {
|
|
36
|
+
this.clearCache();
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return parsed.data;
|
|
41
|
+
}
|
|
42
|
+
setCachedData(data) {
|
|
43
|
+
const cache = this.getCache();
|
|
44
|
+
const value = JSON.stringify({
|
|
45
|
+
data,
|
|
46
|
+
timestamp: Date.now()
|
|
47
|
+
});
|
|
48
|
+
if (this.options.strategy === "memory") {
|
|
49
|
+
cache.set(this.cacheKey, value);
|
|
50
|
+
} else {
|
|
51
|
+
cache.setItem(this.cacheKey, value);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
clearCache() {
|
|
55
|
+
const cache = this.getCache();
|
|
56
|
+
if (this.options.strategy === "memory") {
|
|
57
|
+
cache.delete(this.cacheKey);
|
|
58
|
+
} else {
|
|
59
|
+
cache.removeItem(this.cacheKey);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
fetch(forceRefresh = false) {
|
|
63
|
+
const cachedData = !forceRefresh && this.getCachedData();
|
|
64
|
+
if (cachedData)
|
|
65
|
+
return Promise.resolve(cachedData);
|
|
66
|
+
const globalItem = globalCache.get(this.cacheKey);
|
|
67
|
+
if (globalItem?.promise) {
|
|
68
|
+
return globalItem.promise;
|
|
69
|
+
}
|
|
70
|
+
const newPromise = this.fetchData().then(({ data }) => {
|
|
71
|
+
this.setCachedData(data);
|
|
72
|
+
return data;
|
|
73
|
+
}).finally(() => {
|
|
74
|
+
globalCache.delete(this.cacheKey);
|
|
75
|
+
});
|
|
76
|
+
globalCache.set(this.cacheKey, { promise: newPromise });
|
|
77
|
+
return newPromise;
|
|
78
|
+
}
|
|
79
|
+
}
|
package/es/libs/util.d.ts
CHANGED
|
@@ -111,3 +111,5 @@ export declare function getInvoiceDescriptionAndReason(invoice: TInvoiceExpanded
|
|
|
111
111
|
reason: string;
|
|
112
112
|
type: string;
|
|
113
113
|
};
|
|
114
|
+
export declare function getPaymentKitComponent(): any;
|
|
115
|
+
export declare function openDonationSettings(openInNewTab?: boolean): void;
|
package/es/libs/util.js
CHANGED
|
@@ -913,3 +913,14 @@ export function getInvoiceDescriptionAndReason(invoice, locale = "en") {
|
|
|
913
913
|
type: invoiceType
|
|
914
914
|
};
|
|
915
915
|
}
|
|
916
|
+
export function getPaymentKitComponent() {
|
|
917
|
+
const paymentKit = window.blocklet?.componentMountPoints?.find((c) => c.did === PAYMENT_KIT_DID);
|
|
918
|
+
return paymentKit || null;
|
|
919
|
+
}
|
|
920
|
+
export function openDonationSettings(openInNewTab = false) {
|
|
921
|
+
const paymentKit = getPaymentKitComponent();
|
|
922
|
+
if (paymentKit) {
|
|
923
|
+
const mountPoint = paymentKit.mountPoint.endsWith("/") ? paymentKit.mountPoint.slice(0, -1) : paymentKit.mountPoint;
|
|
924
|
+
window.open(`${window.location.origin}${mountPoint}/integrations/donations`, openInNewTab ? "_blank" : "_self");
|
|
925
|
+
}
|
|
926
|
+
}
|
package/es/locales/en.js
CHANGED
|
@@ -134,7 +134,14 @@ export default flat({
|
|
|
134
134
|
one: "{name} will receive all tips",
|
|
135
135
|
multiple: "Tips will be distributed to {count} beneficiaries",
|
|
136
136
|
view: "Click to view details"
|
|
137
|
-
}
|
|
137
|
+
},
|
|
138
|
+
inactive: "Donation feature is not enabled",
|
|
139
|
+
enable: "Enable Donation",
|
|
140
|
+
enableSuccess: "Enable Success",
|
|
141
|
+
configPrompt: "Donation feature is enabled, you can configure donation options in Payment Kit",
|
|
142
|
+
configNow: "Configure Now",
|
|
143
|
+
later: "Configure Later",
|
|
144
|
+
configTip: "Configure donation settings in Payment Kit"
|
|
138
145
|
},
|
|
139
146
|
cardPay: "{action} with card",
|
|
140
147
|
empty: "No thing to pay",
|
package/es/locales/zh.js
CHANGED
|
@@ -134,7 +134,14 @@ export default flat({
|
|
|
134
134
|
one: "{name} \u5C06\u83B7\u5F97\u5168\u90E8\u6253\u8D4F",
|
|
135
135
|
multiple: "\u6253\u8D4F\u5C06\u6309\u6BD4\u4F8B\u5206\u914D\u7ED9 {count} \u4F4D\u53D7\u76CA\u4EBA",
|
|
136
136
|
view: "\u70B9\u51FB\u67E5\u770B\u8BE6\u60C5"
|
|
137
|
-
}
|
|
137
|
+
},
|
|
138
|
+
inactive: "\u6253\u8D4F\u529F\u80FD\u6682\u672A\u5F00\u542F",
|
|
139
|
+
enable: "\u542F\u7528\u6253\u8D4F",
|
|
140
|
+
enableSuccess: "\u542F\u7528\u6210\u529F",
|
|
141
|
+
configPrompt: "\u6253\u8D4F\u529F\u80FD\u5DF2\u542F\u7528\uFF0C\u60A8\u53EF\u4EE5\u524D\u5F80 Payment Kit \u8FDB\u4E00\u6B65\u914D\u7F6E\u6253\u8D4F\u9009\u9879",
|
|
142
|
+
configNow: "\u7ACB\u5373\u914D\u7F6E",
|
|
143
|
+
later: "\u7A0D\u540E\u914D\u7F6E",
|
|
144
|
+
configTip: "\u524D\u5F80 Payment Kit \u914D\u7F6E\u6253\u8D4F\u9009\u9879"
|
|
138
145
|
},
|
|
139
146
|
cardPay: "\u4F7F\u7528\u5361\u7247{action}",
|
|
140
147
|
empty: "\u6CA1\u6709\u53EF\u652F\u4ED8\u7684\u9879\u76EE",
|
|
@@ -3,7 +3,7 @@ import { Box, Divider, Fade, Skeleton, Stack } from "@mui/material";
|
|
|
3
3
|
export default function DonationSkeleton() {
|
|
4
4
|
return /* @__PURE__ */ jsx(Fade, { in: true, children: /* @__PURE__ */ jsxs(Stack, { direction: "column", children: [
|
|
5
5
|
/* @__PURE__ */ jsx(Skeleton, { variant: "text", sx: { fontSize: "2rem", width: "40%" } }),
|
|
6
|
-
/* @__PURE__ */ jsx(Skeleton, { sx: { mt: 2 }, variant: "rounded", height:
|
|
6
|
+
/* @__PURE__ */ jsx(Skeleton, { sx: { mt: 2 }, variant: "rounded", height: 40 }),
|
|
7
7
|
/* @__PURE__ */ jsx(
|
|
8
8
|
Divider,
|
|
9
9
|
{
|
package/lib/checkout/donate.d.ts
CHANGED
|
@@ -7,12 +7,14 @@ export type DonateHistory = {
|
|
|
7
7
|
method: TPaymentMethod;
|
|
8
8
|
totalAmount: string;
|
|
9
9
|
};
|
|
10
|
+
export type RequiredDonationSettings = Pick<DonationSettings, 'target' | 'title' | 'description' | 'reference' | 'beneficiaries'>;
|
|
11
|
+
type OptionalDonationSettings = Partial<Omit<DonationSettings, keyof RequiredDonationSettings>>;
|
|
10
12
|
export interface ButtonType extends Omit<MUIButtonProps, 'text' | 'icon'> {
|
|
11
|
-
text
|
|
13
|
+
text?: string | React.ReactNode;
|
|
12
14
|
icon: React.ReactNode;
|
|
13
15
|
}
|
|
14
16
|
export type DonateProps = Pick<CheckoutProps, 'onPaid' | 'onError'> & {
|
|
15
|
-
settings:
|
|
17
|
+
settings: RequiredDonationSettings & OptionalDonationSettings;
|
|
16
18
|
livemode?: boolean;
|
|
17
19
|
timeout?: number;
|
|
18
20
|
mode?: 'inline' | 'default' | 'custom';
|
|
@@ -20,7 +22,7 @@ export type DonateProps = Pick<CheckoutProps, 'onPaid' | 'onError'> & {
|
|
|
20
22
|
button?: ButtonType;
|
|
21
23
|
};
|
|
22
24
|
theme?: 'default' | 'inherit' | PaymentThemeOptions;
|
|
23
|
-
children?: (openDialog: () => void, donateTotalAmount: string, supporters: DonateHistory, loading?: boolean) => React.ReactNode;
|
|
25
|
+
children?: (openDialog: () => void, donateTotalAmount: string, supporters: DonateHistory, loading?: boolean, donateSettings?: DonationSettings) => React.ReactNode;
|
|
24
26
|
};
|
|
25
27
|
declare function CheckoutDonate(props: DonateProps): import("react").JSX.Element;
|
|
26
28
|
declare namespace CheckoutDonate {
|
package/lib/checkout/donate.js
CHANGED
|
@@ -12,6 +12,7 @@ var _ahooks = require("ahooks");
|
|
|
12
12
|
var _omit = _interopRequireDefault(require("lodash/omit"));
|
|
13
13
|
var _unionBy = _interopRequireDefault(require("lodash/unionBy"));
|
|
14
14
|
var _react = require("react");
|
|
15
|
+
var _iconsMaterial = require("@mui/icons-material");
|
|
15
16
|
var _tx = _interopRequireDefault(require("../components/blockchain/tx"));
|
|
16
17
|
var _api = _interopRequireDefault(require("../libs/api"));
|
|
17
18
|
var _util = require("../libs/util");
|
|
@@ -20,6 +21,7 @@ var _theme = require("../theme");
|
|
|
20
21
|
var _payment = require("../contexts/payment");
|
|
21
22
|
var _livemode = _interopRequireDefault(require("../components/livemode"));
|
|
22
23
|
var _mobile = require("../hooks/mobile");
|
|
24
|
+
var _donate = require("../contexts/donate");
|
|
23
25
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
24
26
|
const donationCache = {};
|
|
25
27
|
const createOrUpdateDonation = (settings, livemode = true) => {
|
|
@@ -256,13 +258,43 @@ function SupporterSimple({
|
|
|
256
258
|
})]
|
|
257
259
|
});
|
|
258
260
|
}
|
|
261
|
+
const defaultDonateAmount = {
|
|
262
|
+
presets: ["1", "5", "10"],
|
|
263
|
+
preset: "1",
|
|
264
|
+
minimum: "0.01",
|
|
265
|
+
maximum: "100",
|
|
266
|
+
custom: true
|
|
267
|
+
};
|
|
259
268
|
function useDonation(settings, livemode, mode = "default") {
|
|
260
269
|
const [state, setState] = (0, _ahooks.useSetState)({
|
|
261
270
|
open: false,
|
|
262
271
|
supporterLoaded: false,
|
|
263
272
|
exist: false
|
|
264
273
|
});
|
|
265
|
-
const
|
|
274
|
+
const donateContext = (0, _donate.useDonateContext)();
|
|
275
|
+
const {
|
|
276
|
+
isMobile
|
|
277
|
+
} = (0, _mobile.useMobile)();
|
|
278
|
+
const {
|
|
279
|
+
settings: donateConfig = {}
|
|
280
|
+
} = donateContext || {};
|
|
281
|
+
const donateSettings = {
|
|
282
|
+
...settings,
|
|
283
|
+
amount: settings.amount || donateConfig?.settings?.amount || defaultDonateAmount,
|
|
284
|
+
appearance: {
|
|
285
|
+
button: {
|
|
286
|
+
...(settings?.appearance?.button || {}),
|
|
287
|
+
text: settings?.appearance?.button?.text || donateConfig?.settings?.btnText || "Donate",
|
|
288
|
+
icon: settings?.appearance?.button?.icon || donateConfig?.settings?.icon || null
|
|
289
|
+
},
|
|
290
|
+
history: {
|
|
291
|
+
variant: settings?.appearance?.history?.variant || donateConfig?.settings?.historyType || "avatar"
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
const hasRequestedRef = (0, _react.useRef)(false);
|
|
296
|
+
const containerRef = (0, _react.useRef)(null);
|
|
297
|
+
const donation = (0, _ahooks.useRequest)(() => createOrUpdateDonation(donateSettings, livemode), {
|
|
266
298
|
manual: true,
|
|
267
299
|
loadingDelay: 300
|
|
268
300
|
});
|
|
@@ -270,14 +302,26 @@ function useDonation(settings, livemode, mode = "default") {
|
|
|
270
302
|
manual: true,
|
|
271
303
|
loadingDelay: 300
|
|
272
304
|
});
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
305
|
+
const rootMargin = isMobile ? "50px" : `${Math.min(window.innerHeight / 2, 300)}px`;
|
|
306
|
+
(0, _react.useEffect)(() => {
|
|
307
|
+
if (mode === "inline") return;
|
|
308
|
+
const element = containerRef.current;
|
|
309
|
+
if (!element) return;
|
|
310
|
+
const observer = new IntersectionObserver(([entry]) => {
|
|
311
|
+
if (entry.isIntersecting && !hasRequestedRef.current) {
|
|
312
|
+
hasRequestedRef.current = true;
|
|
313
|
+
(0, _util.lazyLoad)(() => {
|
|
314
|
+
donation.run();
|
|
315
|
+
supporters.run();
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
}, {
|
|
319
|
+
threshold: 0,
|
|
320
|
+
rootMargin
|
|
321
|
+
});
|
|
322
|
+
observer.observe(element);
|
|
323
|
+
return () => observer.unobserve(element);
|
|
324
|
+
}, [mode]);
|
|
281
325
|
(0, _react.useEffect)(() => {
|
|
282
326
|
if (donation.data && state.supporterLoaded === false) {
|
|
283
327
|
setState({
|
|
@@ -287,10 +331,13 @@ function useDonation(settings, livemode, mode = "default") {
|
|
|
287
331
|
}
|
|
288
332
|
}, [donation.data]);
|
|
289
333
|
return {
|
|
334
|
+
containerRef,
|
|
290
335
|
donation,
|
|
291
336
|
supporters,
|
|
292
337
|
state,
|
|
293
|
-
setState
|
|
338
|
+
setState,
|
|
339
|
+
donateSettings,
|
|
340
|
+
supportUpdateSettings: !!donateContext.settings
|
|
294
341
|
};
|
|
295
342
|
}
|
|
296
343
|
function CheckoutDonateInner({
|
|
@@ -305,10 +352,13 @@ function CheckoutDonateInner({
|
|
|
305
352
|
children
|
|
306
353
|
}) {
|
|
307
354
|
const {
|
|
355
|
+
containerRef,
|
|
308
356
|
state,
|
|
309
357
|
setState,
|
|
310
358
|
donation,
|
|
311
|
-
supporters
|
|
359
|
+
supporters,
|
|
360
|
+
donateSettings,
|
|
361
|
+
supportUpdateSettings
|
|
312
362
|
} = useDonation(settings, livemode, mode);
|
|
313
363
|
const customers = (0, _unionBy.default)(supporters?.data?.supporters || [], "customer_did");
|
|
314
364
|
const {
|
|
@@ -320,7 +370,8 @@ function CheckoutDonateInner({
|
|
|
320
370
|
isMobile
|
|
321
371
|
} = (0, _mobile.useMobile)();
|
|
322
372
|
const {
|
|
323
|
-
connect
|
|
373
|
+
connect,
|
|
374
|
+
session
|
|
324
375
|
} = (0, _payment.usePaymentContext)();
|
|
325
376
|
const handlePaid = (...args) => {
|
|
326
377
|
if (onPaid) {
|
|
@@ -353,23 +404,24 @@ function CheckoutDonateInner({
|
|
|
353
404
|
open: true
|
|
354
405
|
});
|
|
355
406
|
};
|
|
407
|
+
const inlineText = inlineOptions?.button?.text || donateSettings.appearance.button.text;
|
|
356
408
|
const inlineRender = /* @__PURE__ */(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
|
|
357
409
|
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Button, {
|
|
358
|
-
size:
|
|
359
|
-
color:
|
|
360
|
-
variant:
|
|
361
|
-
...
|
|
410
|
+
size: donateSettings.appearance?.button?.size || "medium",
|
|
411
|
+
color: donateSettings.appearance?.button?.color || "primary",
|
|
412
|
+
variant: donateSettings.appearance?.button?.variant || "contained",
|
|
413
|
+
...donateSettings.appearance?.button,
|
|
362
414
|
onClick: handlePopoverOpen,
|
|
363
415
|
children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
364
416
|
direction: "row",
|
|
365
417
|
alignItems: "center",
|
|
366
418
|
spacing: 0.5,
|
|
367
|
-
children: [
|
|
419
|
+
children: [donateSettings.appearance.button.icon, typeof donateSettings.appearance.button.text === "string" ? /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
|
|
368
420
|
sx: {
|
|
369
421
|
whiteSpace: "nowrap"
|
|
370
422
|
},
|
|
371
|
-
children:
|
|
372
|
-
}) :
|
|
423
|
+
children: donateSettings.appearance.button.text
|
|
424
|
+
}) : donateSettings.appearance.button.text]
|
|
373
425
|
})
|
|
374
426
|
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Popover, {
|
|
375
427
|
id: "mouse-over-popper",
|
|
@@ -414,12 +466,12 @@ function CheckoutDonateInner({
|
|
|
414
466
|
direction: "row",
|
|
415
467
|
alignItems: "center",
|
|
416
468
|
spacing: 0.5,
|
|
417
|
-
children: [inlineOptions?.button?.icon, typeof
|
|
469
|
+
children: [inlineOptions?.button?.icon, typeof inlineText === "string" ? /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
|
|
418
470
|
sx: {
|
|
419
471
|
whiteSpace: "nowrap"
|
|
420
472
|
},
|
|
421
|
-
children:
|
|
422
|
-
}) :
|
|
473
|
+
children: inlineText
|
|
474
|
+
}) : inlineText]
|
|
423
475
|
})
|
|
424
476
|
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(SupporterSimple, {
|
|
425
477
|
...supporters.data
|
|
@@ -442,22 +494,22 @@ function CheckoutDonateInner({
|
|
|
442
494
|
sm: 2
|
|
443
495
|
},
|
|
444
496
|
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Button, {
|
|
445
|
-
size:
|
|
446
|
-
color:
|
|
447
|
-
variant:
|
|
448
|
-
...
|
|
497
|
+
size: donateSettings.appearance?.button?.size || "medium",
|
|
498
|
+
color: donateSettings.appearance?.button?.color || "primary",
|
|
499
|
+
variant: donateSettings.appearance?.button?.variant || "contained",
|
|
500
|
+
...donateSettings.appearance?.button,
|
|
449
501
|
onClick: () => startDonate(),
|
|
450
502
|
children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
451
503
|
direction: "row",
|
|
452
504
|
alignItems: "center",
|
|
453
505
|
spacing: 0.5,
|
|
454
|
-
children: [
|
|
455
|
-
children:
|
|
456
|
-
}) :
|
|
506
|
+
children: [donateSettings.appearance.button.icon, typeof donateSettings.appearance.button.text === "string" ? /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
|
|
507
|
+
children: donateSettings.appearance.button.text
|
|
508
|
+
}) : donateSettings.appearance.button.text]
|
|
457
509
|
})
|
|
458
|
-
}), supporters.data &&
|
|
510
|
+
}), supporters.data && donateSettings.appearance.history.variant === "avatar" && /* @__PURE__ */(0, _jsxRuntime.jsx)(SupporterAvatar, {
|
|
459
511
|
...supporters.data
|
|
460
|
-
}), supporters.data &&
|
|
512
|
+
}), supporters.data && donateSettings.appearance.history.variant === "table" && /* @__PURE__ */(0, _jsxRuntime.jsx)(SupporterTable, {
|
|
461
513
|
...supporters.data
|
|
462
514
|
})]
|
|
463
515
|
});
|
|
@@ -467,7 +519,7 @@ function CheckoutDonateInner({
|
|
|
467
519
|
}
|
|
468
520
|
if (mode === "custom") {
|
|
469
521
|
return children && typeof children === "function" ? /* @__PURE__ */(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
|
|
470
|
-
children: children(startDonate, `${(0, _util.formatAmount)(supporters.data?.totalAmount || "0", supporters.data?.currency?.decimal)} ${supporters.data?.currency?.symbol}`, supporters.data || {}, !!supporters.loading)
|
|
522
|
+
children: children(startDonate, `${(0, _util.formatAmount)(supporters.data?.totalAmount || "0", supporters.data?.currency?.decimal)} ${supporters.data?.currency?.symbol}`, supporters.data || {}, !!supporters.loading, donateSettings)
|
|
471
523
|
}) : /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Typography, {
|
|
472
524
|
children: ["Please provide a valid render function ", /* @__PURE__ */(0, _jsxRuntime.jsx)("pre", {
|
|
473
525
|
children: "(openDonate, donateTotalAmount, supporters) => ReactNode"
|
|
@@ -476,7 +528,9 @@ function CheckoutDonateInner({
|
|
|
476
528
|
}
|
|
477
529
|
return defaultRender;
|
|
478
530
|
};
|
|
479
|
-
|
|
531
|
+
const isAdmin = ["owner", "admin"].includes(session?.user?.role);
|
|
532
|
+
return /* @__PURE__ */(0, _jsxRuntime.jsxs)("div", {
|
|
533
|
+
ref: containerRef,
|
|
480
534
|
children: [renderInnerView(), donation.data && /* @__PURE__ */(0, _jsxRuntime.jsx)(_Dialog.default, {
|
|
481
535
|
open: state.open,
|
|
482
536
|
title: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Box, {
|
|
@@ -490,10 +544,27 @@ function CheckoutDonateInner({
|
|
|
490
544
|
textOverflow: "ellipsis",
|
|
491
545
|
overflow: "hidden"
|
|
492
546
|
},
|
|
493
|
-
children:
|
|
547
|
+
children: donateSettings.title
|
|
548
|
+
}), supportUpdateSettings && isAdmin && /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Tooltip, {
|
|
549
|
+
title: t("payment.checkout.donation.configTip"),
|
|
550
|
+
placement: "bottom",
|
|
551
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.IconButton, {
|
|
552
|
+
size: "small",
|
|
553
|
+
onClick: e => {
|
|
554
|
+
e.stopPropagation();
|
|
555
|
+
(0, _util.openDonationSettings)(true);
|
|
556
|
+
},
|
|
557
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsx)(_iconsMaterial.Settings, {
|
|
558
|
+
fontSize: "small",
|
|
559
|
+
sx: {
|
|
560
|
+
ml: -0.5
|
|
561
|
+
}
|
|
562
|
+
})
|
|
563
|
+
})
|
|
494
564
|
}), !donation.data.livemode && /* @__PURE__ */(0, _jsxRuntime.jsx)(_livemode.default, {
|
|
495
565
|
sx: {
|
|
496
|
-
width: "fit-content"
|
|
566
|
+
width: "fit-content",
|
|
567
|
+
ml: 0.5
|
|
497
568
|
}
|
|
498
569
|
})]
|
|
499
570
|
}),
|
|
@@ -551,7 +622,7 @@ function CheckoutDonateInner({
|
|
|
551
622
|
id: donation.data?.id,
|
|
552
623
|
onPaid: handlePaid,
|
|
553
624
|
onError,
|
|
554
|
-
action:
|
|
625
|
+
action: donateSettings.appearance?.button?.text,
|
|
555
626
|
mode: "inline",
|
|
556
627
|
theme,
|
|
557
628
|
formType: "donation",
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { TSetting } from '@blocklet/payment-types';
|
|
2
|
+
import type { Axios } from 'axios';
|
|
3
|
+
export interface DonateConfigSettings {
|
|
4
|
+
amount?: {
|
|
5
|
+
presets?: string[];
|
|
6
|
+
preset?: string;
|
|
7
|
+
custom: boolean;
|
|
8
|
+
minimum?: string;
|
|
9
|
+
maximum?: string;
|
|
10
|
+
};
|
|
11
|
+
btnText?: string;
|
|
12
|
+
historyType?: 'table' | 'avatar';
|
|
13
|
+
}
|
|
14
|
+
export type DonateContextType = {
|
|
15
|
+
settings: TSetting;
|
|
16
|
+
refresh: (forceRefresh?: boolean) => void;
|
|
17
|
+
updateSettings: (newSettings: DonateConfigSettings) => Promise<void>;
|
|
18
|
+
api: Axios;
|
|
19
|
+
};
|
|
20
|
+
export type DonateContextProps = {
|
|
21
|
+
mountLocation: string;
|
|
22
|
+
description: string;
|
|
23
|
+
defaultSettings?: DonateConfigSettings;
|
|
24
|
+
children: any;
|
|
25
|
+
active?: boolean;
|
|
26
|
+
enableDonate?: boolean;
|
|
27
|
+
};
|
|
28
|
+
declare const DonateContext: import("react").Context<DonateContextType>;
|
|
29
|
+
declare const Consumer: import("react").Consumer<DonateContextType>;
|
|
30
|
+
declare function DonateProvider({ mountLocation, description, defaultSettings, children, active, enableDonate, }: DonateContextProps): import("react").JSX.Element | null;
|
|
31
|
+
declare namespace DonateProvider {
|
|
32
|
+
var defaultProps: {
|
|
33
|
+
defaultSettings: {};
|
|
34
|
+
active: boolean;
|
|
35
|
+
enableDonate: boolean;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
declare function useDonateContext(): DonateContextType;
|
|
39
|
+
export declare const clearDonateCache: (mountLocation: string) => void;
|
|
40
|
+
export declare const clearDonateSettings: (mountLocation: string) => Promise<void>;
|
|
41
|
+
export { DonateContext, DonateProvider, Consumer as DonateConsumer, useDonateContext };
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.DonateContext = exports.DonateConsumer = void 0;
|
|
7
|
+
exports.DonateProvider = DonateProvider;
|
|
8
|
+
exports.clearDonateSettings = exports.clearDonateCache = void 0;
|
|
9
|
+
exports.useDonateContext = useDonateContext;
|
|
10
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
11
|
+
var _ahooks = require("ahooks");
|
|
12
|
+
var _react = require("react");
|
|
13
|
+
var _Toast = _interopRequireDefault(require("@arcblock/ux/lib/Toast"));
|
|
14
|
+
var _context = require("@arcblock/ux/lib/Locale/context");
|
|
15
|
+
var _material = require("@mui/material");
|
|
16
|
+
var _api = _interopRequireDefault(require("../libs/api"));
|
|
17
|
+
var _util = require("../libs/util");
|
|
18
|
+
var _cachedRequest = require("../libs/cached-request");
|
|
19
|
+
var _confirm = _interopRequireDefault(require("../components/confirm"));
|
|
20
|
+
var _payment = require("./payment");
|
|
21
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
+
const DonateContext = exports.DonateContext = (0, _react.createContext)({
|
|
23
|
+
api: _api.default
|
|
24
|
+
});
|
|
25
|
+
const {
|
|
26
|
+
Provider,
|
|
27
|
+
Consumer
|
|
28
|
+
} = DonateContext;
|
|
29
|
+
exports.DonateConsumer = Consumer;
|
|
30
|
+
const fetchDonateSetting = (params, forceRefresh = false) => {
|
|
31
|
+
const livemode = localStorage.getItem("livemode") !== "false";
|
|
32
|
+
const cacheKey = `donate-settings-${params.mountLocation}-${livemode}`;
|
|
33
|
+
const cachedRequest = new _cachedRequest.CachedRequest(cacheKey, () => _api.default.post("/api/settings", {
|
|
34
|
+
...params,
|
|
35
|
+
type: "donate",
|
|
36
|
+
livemode,
|
|
37
|
+
settings: params.defaultSettings
|
|
38
|
+
}), {
|
|
39
|
+
ttl: 1e3 * 60 * 60,
|
|
40
|
+
strategy: "local"
|
|
41
|
+
});
|
|
42
|
+
return cachedRequest.fetch(forceRefresh);
|
|
43
|
+
};
|
|
44
|
+
function DonateProvider({
|
|
45
|
+
mountLocation,
|
|
46
|
+
description,
|
|
47
|
+
defaultSettings = {},
|
|
48
|
+
children,
|
|
49
|
+
active = true,
|
|
50
|
+
enableDonate = false
|
|
51
|
+
}) {
|
|
52
|
+
const {
|
|
53
|
+
t
|
|
54
|
+
} = (0, _context.useLocaleContext)();
|
|
55
|
+
const [showConfirm, setShowConfirm] = (0, _react.useState)(false);
|
|
56
|
+
const {
|
|
57
|
+
session
|
|
58
|
+
} = (0, _payment.usePaymentContext)();
|
|
59
|
+
const isAdmin = ["owner", "admin"].includes(session?.user?.role);
|
|
60
|
+
const {
|
|
61
|
+
data = {
|
|
62
|
+
settings: {},
|
|
63
|
+
active: true
|
|
64
|
+
},
|
|
65
|
+
error,
|
|
66
|
+
run,
|
|
67
|
+
loading
|
|
68
|
+
} = (0, _ahooks.useRequest)(forceRender => fetchDonateSetting({
|
|
69
|
+
mountLocation,
|
|
70
|
+
description,
|
|
71
|
+
defaultSettings,
|
|
72
|
+
active,
|
|
73
|
+
componentDid: window.blocklet?.componentId?.split("/").pop()
|
|
74
|
+
}, forceRender), {
|
|
75
|
+
refreshDeps: [mountLocation],
|
|
76
|
+
onError: err => {
|
|
77
|
+
_Toast.default.error((0, _util.formatError)(err));
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
const updateSettings = async newSettings => {
|
|
81
|
+
try {
|
|
82
|
+
const livemode = localStorage.getItem("livemode") !== "false";
|
|
83
|
+
await _api.default.put(`/api/settings/${mountLocation}`, {
|
|
84
|
+
livemode,
|
|
85
|
+
settings: newSettings
|
|
86
|
+
});
|
|
87
|
+
run(true);
|
|
88
|
+
_Toast.default.success(t("common.saved"));
|
|
89
|
+
} catch (err) {
|
|
90
|
+
_Toast.default.error((0, _util.formatError)(err));
|
|
91
|
+
throw err;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const supportPaymentKit = (0, _util.getPaymentKitComponent)();
|
|
95
|
+
const handleEnable = async () => {
|
|
96
|
+
if (!enableDonate || !data || data?.active) return;
|
|
97
|
+
try {
|
|
98
|
+
await _api.default.put(`/api/settings/${data.id}`, {
|
|
99
|
+
active: true
|
|
100
|
+
});
|
|
101
|
+
if (supportPaymentKit) {
|
|
102
|
+
setShowConfirm(true);
|
|
103
|
+
} else {
|
|
104
|
+
_Toast.default.success(t("payment.checkout.donation.enableSuccess"));
|
|
105
|
+
run(true);
|
|
106
|
+
}
|
|
107
|
+
} catch (err) {
|
|
108
|
+
_Toast.default.error((0, _util.formatError)(err));
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
if (loading || error) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
return /* @__PURE__ */(0, _jsxRuntime.jsx)(Provider, {
|
|
115
|
+
value: {
|
|
116
|
+
settings: data,
|
|
117
|
+
refresh: run,
|
|
118
|
+
updateSettings,
|
|
119
|
+
api: _api.default
|
|
120
|
+
},
|
|
121
|
+
children: data?.active === false ? /* @__PURE__ */(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
|
|
122
|
+
children: [enableDonate && isAdmin && /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
123
|
+
spacing: 1,
|
|
124
|
+
sx: {
|
|
125
|
+
alignItems: "center"
|
|
126
|
+
},
|
|
127
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
|
|
128
|
+
color: "text.secondary",
|
|
129
|
+
children: t("payment.checkout.donation.inactive")
|
|
130
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Button, {
|
|
131
|
+
size: "small",
|
|
132
|
+
variant: "outlined",
|
|
133
|
+
color: "primary",
|
|
134
|
+
onClick: handleEnable,
|
|
135
|
+
sx: {
|
|
136
|
+
width: "fit-content"
|
|
137
|
+
},
|
|
138
|
+
children: t("payment.checkout.donation.enable")
|
|
139
|
+
})]
|
|
140
|
+
}), showConfirm && /* @__PURE__ */(0, _jsxRuntime.jsx)(_confirm.default, {
|
|
141
|
+
title: t("payment.checkout.donation.enableSuccess"),
|
|
142
|
+
message: t("payment.checkout.donation.configPrompt"),
|
|
143
|
+
cancel: t("payment.checkout.donation.later"),
|
|
144
|
+
confirm: t("payment.checkout.donation.configNow"),
|
|
145
|
+
onCancel: () => {
|
|
146
|
+
setShowConfirm(false);
|
|
147
|
+
run(true);
|
|
148
|
+
},
|
|
149
|
+
color: "primary",
|
|
150
|
+
onConfirm: () => {
|
|
151
|
+
run(true);
|
|
152
|
+
(0, _util.openDonationSettings)(false);
|
|
153
|
+
setShowConfirm(false);
|
|
154
|
+
}
|
|
155
|
+
})]
|
|
156
|
+
}) : children
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
function useDonateContext() {
|
|
160
|
+
const context = (0, _react.useContext)(DonateContext);
|
|
161
|
+
return context;
|
|
162
|
+
}
|
|
163
|
+
DonateProvider.defaultProps = {
|
|
164
|
+
defaultSettings: {},
|
|
165
|
+
active: true,
|
|
166
|
+
enableDonate: false
|
|
167
|
+
};
|
|
168
|
+
const clearDonateCache = mountLocation => {
|
|
169
|
+
const livemode = localStorage.getItem("livemode") !== "false";
|
|
170
|
+
const cacheKey = `donate-settings-${mountLocation}-${livemode}`;
|
|
171
|
+
localStorage.removeItem(cacheKey);
|
|
172
|
+
};
|
|
173
|
+
exports.clearDonateCache = clearDonateCache;
|
|
174
|
+
const clearDonateSettings = async mountLocation => {
|
|
175
|
+
try {
|
|
176
|
+
const livemode = localStorage.getItem("livemode") !== "false";
|
|
177
|
+
await _api.default.delete(`/api/settings/${mountLocation}`, {
|
|
178
|
+
params: {
|
|
179
|
+
livemode
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
clearDonateCache(mountLocation);
|
|
183
|
+
} catch (err) {
|
|
184
|
+
_Toast.default.error((0, _util.formatError)(err));
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
exports.clearDonateSettings = clearDonateSettings;
|