@meterbox/react 0.1.13 → 0.1.15
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/README.md +3 -1
- package/package.json +2 -2
- package/src/PlanChangeCard.js +103 -29
- package/src/PricingTable.js +3 -1
- package/src/SubscriptionPreview.js +4 -10
- package/src/i18n.js +31 -8
package/README.md
CHANGED
|
@@ -159,12 +159,14 @@ doesn't offer it surfaces a 400 via `onError` rather than redirecting.
|
|
|
159
159
|
|
|
160
160
|
Renders the response of `POST /v1/customers/:id/subscription/preview` (admin)
|
|
161
161
|
or `POST /v1/customer-portal/:id/subscription/preview` (customer JWT):
|
|
162
|
-
prorated charge,
|
|
162
|
+
prorated charge, the monthly price before and after, effective date.
|
|
163
163
|
|
|
164
164
|
### `<PlanChangeCard preview? scheduledChange? allowSchedule? | customerId customerToken planId planVersion? fetch? onConfirm? onCancelScheduled? onChanged? />`
|
|
165
165
|
|
|
166
166
|
Composes `<SubscriptionPreview>` with the actions it doesn't have: actually
|
|
167
167
|
applying or scheduling the change, and cancelling a pending scheduled one.
|
|
168
|
+
Change now and Schedule each ask first (which plan, when, and what's due
|
|
169
|
+
today when the preview says) and act only on "Yes, change it".
|
|
168
170
|
Two modes:
|
|
169
171
|
|
|
170
172
|
- **Presentational**: pass `preview` (already fetched) plus
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meterbox/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15",
|
|
4
4
|
"description": "Embeddable React components for Meterbox: pricing tables, subscription preview, credit balance and usage charts. Presentational by default, with data fetched by your backend via @meterbox/node.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://meterbox.ai",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@meterbox/node": "0.1.
|
|
20
|
+
"@meterbox/node": "0.1.3"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"react": ">=17",
|
package/src/PlanChangeCard.js
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Composes `<SubscriptionPreview>` with the two actions that were missing
|
|
5
5
|
* from the customer-portal surface: actually applying or scheduling the
|
|
6
|
-
* change
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* change, each asked to confirm first (which plan, when, and what's due
|
|
7
|
+
* today when the preview says). Previewing a plan change was already
|
|
8
|
+
* possible; there was no way for a customer to confirm one without the
|
|
9
|
+
* host app building its own checkout flow (a NEW Stripe Checkout session —
|
|
10
|
+
* the wrong primitive for modifying an existing subscription in place).
|
|
10
11
|
*
|
|
11
12
|
* Two modes, same split as `<SubscriptionPreview>`:
|
|
12
13
|
*
|
|
@@ -61,6 +62,16 @@ const styles = {
|
|
|
61
62
|
gap: 12,
|
|
62
63
|
},
|
|
63
64
|
actions: { display: "flex", gap: 8 },
|
|
65
|
+
confirm: {
|
|
66
|
+
display: "grid",
|
|
67
|
+
gap: 10,
|
|
68
|
+
padding: "10px 12px",
|
|
69
|
+
borderRadius: 8,
|
|
70
|
+
border: `1px solid ${theme.border}`,
|
|
71
|
+
background: theme.accentTint(8),
|
|
72
|
+
color: theme.text,
|
|
73
|
+
},
|
|
74
|
+
question: { margin: 0, fontSize: 14 },
|
|
64
75
|
btn: {
|
|
65
76
|
padding: "10px 16px",
|
|
66
77
|
border: "1px solid #d1d5db",
|
|
@@ -143,12 +154,29 @@ function PlanChangeCard({
|
|
|
143
154
|
const [err, setErr] = React.useState(null);
|
|
144
155
|
const [message, setMessage] = React.useState(null);
|
|
145
156
|
const [scheduled, setScheduled] = React.useState(scheduledChange || null);
|
|
146
|
-
|
|
157
|
+
// "now" or "period_end" while the customer is asked to confirm it.
|
|
158
|
+
const [asking, setAsking] = React.useState(null);
|
|
159
|
+
const { t, date, money } = useI18n();
|
|
147
160
|
|
|
148
161
|
React.useEffect(() => {
|
|
149
162
|
setScheduled(scheduledChange || null);
|
|
150
163
|
}, [scheduledChange]);
|
|
151
164
|
|
|
165
|
+
// Another plan picked while asking: ask again for that one.
|
|
166
|
+
React.useEffect(() => setAsking(null), [planId]);
|
|
167
|
+
|
|
168
|
+
// The buttons asked about are gone, so focus moves to the safe answer.
|
|
169
|
+
const keepRef = React.useRef(null);
|
|
170
|
+
React.useEffect(() => {
|
|
171
|
+
if (asking) keepRef.current?.focus();
|
|
172
|
+
}, [asking]);
|
|
173
|
+
|
|
174
|
+
function ask(effective) {
|
|
175
|
+
setErr(null);
|
|
176
|
+
setMessage(null);
|
|
177
|
+
setAsking(effective);
|
|
178
|
+
}
|
|
179
|
+
|
|
152
180
|
async function confirm(effective) {
|
|
153
181
|
if (!planId) return;
|
|
154
182
|
setErr(null);
|
|
@@ -199,6 +227,19 @@ function PlanChangeCard({
|
|
|
199
227
|
}
|
|
200
228
|
|
|
201
229
|
const ready = Boolean(planId) && (Boolean(onConfirm) || canSelfFetch);
|
|
230
|
+
// A plan change is charged or changes what the customer gets, so it's asked
|
|
231
|
+
// before it's made: which plan, when, and what's due today when known.
|
|
232
|
+
const targetName = preview?.target?.name || planId;
|
|
233
|
+
const dueToday = preview?.amount_due_usd;
|
|
234
|
+
let question = null;
|
|
235
|
+
if (asking === "period_end") {
|
|
236
|
+
question = t("planChange.confirmSchedule", { plan: targetName });
|
|
237
|
+
} else if (asking) {
|
|
238
|
+
question =
|
|
239
|
+
dueToday > 0
|
|
240
|
+
? t("planChange.confirmNowDue", { plan: targetName, amount: money(dueToday) })
|
|
241
|
+
: t("planChange.confirmNow", { plan: targetName });
|
|
242
|
+
}
|
|
202
243
|
|
|
203
244
|
return React.createElement(
|
|
204
245
|
"div",
|
|
@@ -235,31 +276,64 @@ function PlanChangeCard({
|
|
|
235
276
|
planVersion,
|
|
236
277
|
fetch: fetchImpl,
|
|
237
278
|
}),
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
279
|
+
question
|
|
280
|
+
? React.createElement(
|
|
281
|
+
"div",
|
|
282
|
+
{ style: styles.confirm },
|
|
283
|
+
React.createElement("p", { role: "alert", style: styles.question }, question),
|
|
284
|
+
React.createElement(
|
|
285
|
+
"div",
|
|
286
|
+
{ style: styles.actions },
|
|
287
|
+
React.createElement(
|
|
288
|
+
"button",
|
|
289
|
+
{
|
|
290
|
+
type: "button",
|
|
291
|
+
style: btnStyle(false, true),
|
|
292
|
+
onClick: () => {
|
|
293
|
+
const effective = asking;
|
|
294
|
+
setAsking(null);
|
|
295
|
+
confirm(effective);
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
t("planChange.confirm"),
|
|
299
|
+
),
|
|
300
|
+
React.createElement(
|
|
301
|
+
"button",
|
|
302
|
+
{
|
|
303
|
+
type: "button",
|
|
304
|
+
style: btnStyle(false, false),
|
|
305
|
+
ref: keepRef,
|
|
306
|
+
onClick: () => setAsking(null),
|
|
307
|
+
},
|
|
308
|
+
t("planChange.keep"),
|
|
309
|
+
),
|
|
310
|
+
),
|
|
311
|
+
)
|
|
312
|
+
: React.createElement(
|
|
313
|
+
"div",
|
|
314
|
+
{ style: styles.actions },
|
|
315
|
+
React.createElement(
|
|
316
|
+
"button",
|
|
317
|
+
{
|
|
318
|
+
type: "button",
|
|
319
|
+
style: btnStyle(!ready || busy, true),
|
|
320
|
+
disabled: !ready || busy,
|
|
321
|
+
onClick: () => ask("now"),
|
|
322
|
+
},
|
|
323
|
+
busy ? t("planChange.working") : t("planChange.now"),
|
|
324
|
+
),
|
|
325
|
+
allowSchedule &&
|
|
326
|
+
React.createElement(
|
|
327
|
+
"button",
|
|
328
|
+
{
|
|
329
|
+
type: "button",
|
|
330
|
+
style: btnStyle(!ready || busy, false),
|
|
331
|
+
disabled: !ready || busy,
|
|
332
|
+
onClick: () => ask("period_end"),
|
|
333
|
+
},
|
|
334
|
+
t("planChange.schedule"),
|
|
335
|
+
),
|
|
261
336
|
),
|
|
262
|
-
),
|
|
263
337
|
err && React.createElement("div", { style: { ...styles.message, ...styles.error } }, err),
|
|
264
338
|
message &&
|
|
265
339
|
!err &&
|
package/src/PricingTable.js
CHANGED
|
@@ -82,7 +82,9 @@ const styles = {
|
|
|
82
82
|
padding: 4,
|
|
83
83
|
width: "fit-content",
|
|
84
84
|
margin: "0 auto",
|
|
85
|
-
|
|
85
|
+
// On the card color, so its labels (the cards' text color) read on any
|
|
86
|
+
// page behind it, a dark one too.
|
|
87
|
+
background: `color-mix(in srgb, ${theme.accent} 6%, ${theme.card})`,
|
|
86
88
|
border: `1px solid ${theme.border}`,
|
|
87
89
|
borderRadius: 999,
|
|
88
90
|
},
|
|
@@ -48,15 +48,14 @@ const styles = {
|
|
|
48
48
|
fontWeight: 700,
|
|
49
49
|
},
|
|
50
50
|
delta: { fontSize: 13, fontWeight: 600 },
|
|
51
|
-
positive: { color: "#16a34a" },
|
|
52
|
-
negative: { color: "#dc2626" },
|
|
53
51
|
};
|
|
54
52
|
|
|
55
53
|
async function fetchPreview({ baseUrl, customerId, customerToken, planId, planVersion, fetchImpl }) {
|
|
56
54
|
const url = `${String(baseUrl).replace(/\/+$/, "")}/v1/customer-portal/${encodeURIComponent(
|
|
57
55
|
customerId,
|
|
58
56
|
)}/subscription/preview`;
|
|
59
|
-
const body =
|
|
57
|
+
const body =
|
|
58
|
+
planVersion != null ? { plan_id: planId, plan_version: planVersion } : { plan_id: planId };
|
|
60
59
|
const res = await fetchImpl(url, {
|
|
61
60
|
method: "POST",
|
|
62
61
|
headers: {
|
|
@@ -199,13 +198,8 @@ function SubscriptionPreview({
|
|
|
199
198
|
mrrDelta != null &&
|
|
200
199
|
React.createElement(
|
|
201
200
|
"span",
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
...styles.delta,
|
|
205
|
-
...(mrrDelta >= 0 ? styles.positive : styles.negative),
|
|
206
|
-
marginLeft: 8,
|
|
207
|
-
},
|
|
208
|
-
},
|
|
201
|
+
// Neutral: whether a higher price is good news depends on who's reading.
|
|
202
|
+
{ style: { ...styles.delta, marginLeft: 8 } },
|
|
209
203
|
`(${mrrDelta >= 0 ? "+" : ""}${money(mrrDelta)})`,
|
|
210
204
|
),
|
|
211
205
|
),
|
package/src/i18n.js
CHANGED
|
@@ -112,6 +112,11 @@ const en = {
|
|
|
112
112
|
"planChange.working": "Working…",
|
|
113
113
|
"planChange.now": "Change now",
|
|
114
114
|
"planChange.schedule": "Schedule for end of period",
|
|
115
|
+
"planChange.confirmNow": "Change your plan to {plan} now?",
|
|
116
|
+
"planChange.confirmNowDue": "Change your plan to {plan} now? {amount} is charged today.",
|
|
117
|
+
"planChange.confirmSchedule": "Change your plan to {plan} at the end of this billing period?",
|
|
118
|
+
"planChange.confirm": "Yes, change it",
|
|
119
|
+
"planChange.keep": "Keep my plan",
|
|
115
120
|
|
|
116
121
|
"preview.loading": "Loading preview…",
|
|
117
122
|
"preview.error": "Could not load preview.",
|
|
@@ -119,8 +124,8 @@ const en = {
|
|
|
119
124
|
"preview.from": "From",
|
|
120
125
|
"preview.to": "To",
|
|
121
126
|
"preview.effective": "Effective",
|
|
122
|
-
"preview.mrrNow": "
|
|
123
|
-
"preview.mrrAfter": "
|
|
127
|
+
"preview.mrrNow": "Monthly price now",
|
|
128
|
+
"preview.mrrAfter": "Monthly price after",
|
|
124
129
|
"preview.dueToday": "Prorated charge today",
|
|
125
130
|
|
|
126
131
|
"credits.balance": "Credit balance",
|
|
@@ -265,6 +270,12 @@ const es = {
|
|
|
265
270
|
"planChange.working": "Procesando…",
|
|
266
271
|
"planChange.now": "Cambiar ahora",
|
|
267
272
|
"planChange.schedule": "Programar para el final del período",
|
|
273
|
+
"planChange.confirmNow": "¿Cambiar tu plan a {plan} ahora?",
|
|
274
|
+
"planChange.confirmNowDue": "¿Cambiar tu plan a {plan} ahora? Hoy se cobran {amount}.",
|
|
275
|
+
"planChange.confirmSchedule":
|
|
276
|
+
"¿Cambiar tu plan a {plan} al final de este período de facturación?",
|
|
277
|
+
"planChange.confirm": "Sí, cambiarlo",
|
|
278
|
+
"planChange.keep": "Mantener mi plan",
|
|
268
279
|
|
|
269
280
|
"preview.loading": "Cargando la vista previa…",
|
|
270
281
|
"preview.error": "No se pudo cargar la vista previa.",
|
|
@@ -272,8 +283,8 @@ const es = {
|
|
|
272
283
|
"preview.from": "De",
|
|
273
284
|
"preview.to": "A",
|
|
274
285
|
"preview.effective": "Vigente desde",
|
|
275
|
-
"preview.mrrNow": "
|
|
276
|
-
"preview.mrrAfter": "
|
|
286
|
+
"preview.mrrNow": "Precio mensual actual",
|
|
287
|
+
"preview.mrrAfter": "Precio mensual después",
|
|
277
288
|
"preview.dueToday": "Cargo prorrateado hoy",
|
|
278
289
|
|
|
279
290
|
"credits.balance": "Saldo de créditos",
|
|
@@ -418,6 +429,13 @@ const fr = {
|
|
|
418
429
|
"planChange.working": "En cours…",
|
|
419
430
|
"planChange.now": "Changer maintenant",
|
|
420
431
|
"planChange.schedule": "Programmer en fin de période",
|
|
432
|
+
"planChange.confirmNow": "Passer au forfait {plan} maintenant ?",
|
|
433
|
+
"planChange.confirmNowDue":
|
|
434
|
+
"Passer au forfait {plan} maintenant ? {amount} sera facturé aujourd'hui.",
|
|
435
|
+
"planChange.confirmSchedule":
|
|
436
|
+
"Passer au forfait {plan} à la fin de cette période de facturation ?",
|
|
437
|
+
"planChange.confirm": "Oui, changer",
|
|
438
|
+
"planChange.keep": "Garder mon forfait",
|
|
421
439
|
|
|
422
440
|
"preview.loading": "Chargement de l'aperçu…",
|
|
423
441
|
"preview.error": "Impossible de charger l'aperçu.",
|
|
@@ -425,8 +443,8 @@ const fr = {
|
|
|
425
443
|
"preview.from": "De",
|
|
426
444
|
"preview.to": "Vers",
|
|
427
445
|
"preview.effective": "Prend effet",
|
|
428
|
-
"preview.mrrNow": "
|
|
429
|
-
"preview.mrrAfter": "
|
|
446
|
+
"preview.mrrNow": "Prix mensuel actuel",
|
|
447
|
+
"preview.mrrAfter": "Prix mensuel après",
|
|
430
448
|
"preview.dueToday": "Montant au prorata aujourd'hui",
|
|
431
449
|
|
|
432
450
|
"credits.balance": "Solde de crédits",
|
|
@@ -572,6 +590,11 @@ const de = {
|
|
|
572
590
|
"planChange.working": "Wird bearbeitet…",
|
|
573
591
|
"planChange.now": "Jetzt wechseln",
|
|
574
592
|
"planChange.schedule": "Zum Ende des Zeitraums planen",
|
|
593
|
+
"planChange.confirmNow": "Jetzt zum Tarif {plan} wechseln?",
|
|
594
|
+
"planChange.confirmNowDue": "Jetzt zum Tarif {plan} wechseln? Heute werden {amount} berechnet.",
|
|
595
|
+
"planChange.confirmSchedule": "Zum Ende dieses Abrechnungszeitraums zum Tarif {plan} wechseln?",
|
|
596
|
+
"planChange.confirm": "Ja, wechseln",
|
|
597
|
+
"planChange.keep": "Tarif behalten",
|
|
575
598
|
|
|
576
599
|
"preview.loading": "Vorschau wird geladen…",
|
|
577
600
|
"preview.error": "Die Vorschau konnte nicht geladen werden.",
|
|
@@ -579,8 +602,8 @@ const de = {
|
|
|
579
602
|
"preview.from": "Von",
|
|
580
603
|
"preview.to": "Zu",
|
|
581
604
|
"preview.effective": "Gültig ab",
|
|
582
|
-
"preview.mrrNow": "
|
|
583
|
-
"preview.mrrAfter": "
|
|
605
|
+
"preview.mrrNow": "Monatspreis jetzt",
|
|
606
|
+
"preview.mrrAfter": "Monatspreis danach",
|
|
584
607
|
"preview.dueToday": "Anteilige Gebühr heute",
|
|
585
608
|
|
|
586
609
|
"credits.balance": "Guthaben",
|