@meterbox/react 0.1.14 → 0.1.16

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 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, MRR delta, effective date.
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.14",
3
+ "version": "0.1.16",
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",
@@ -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. Previewing a plan change was already possible; there was no way
7
- * for a customer to confirm one without the host app building its own
8
- * checkout flow (a NEW Stripe Checkout session — the wrong primitive for
9
- * modifying an existing subscription in place).
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,35 @@ 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
- const { t, date } = useI18n();
157
+ // "now" or "period_end" while the customer is asked to confirm it.
158
+ const [asking, setAsking] = React.useState(null);
159
+ // Another plan picked while asking: ask again for that one. Reset as it
160
+ // renders, not in an effect: React runs a render's pending effects before
161
+ // it handles a click, so one that reset this on mount could undo a click on
162
+ // "Change now" that came first.
163
+ const [askedFor, setAskedFor] = React.useState(planId);
164
+ if (askedFor !== planId) {
165
+ setAskedFor(planId);
166
+ setAsking(null);
167
+ }
168
+ const { t, date, money } = useI18n();
147
169
 
148
170
  React.useEffect(() => {
149
171
  setScheduled(scheduledChange || null);
150
172
  }, [scheduledChange]);
151
173
 
174
+ // The buttons asked about are gone, so focus moves to the safe answer.
175
+ const keepRef = React.useRef(null);
176
+ React.useEffect(() => {
177
+ if (asking) keepRef.current?.focus();
178
+ }, [asking]);
179
+
180
+ function ask(effective) {
181
+ setErr(null);
182
+ setMessage(null);
183
+ setAsking(effective);
184
+ }
185
+
152
186
  async function confirm(effective) {
153
187
  if (!planId) return;
154
188
  setErr(null);
@@ -199,6 +233,19 @@ function PlanChangeCard({
199
233
  }
200
234
 
201
235
  const ready = Boolean(planId) && (Boolean(onConfirm) || canSelfFetch);
236
+ // A plan change is charged or changes what the customer gets, so it's asked
237
+ // before it's made: which plan, when, and what's due today when known.
238
+ const targetName = preview?.target?.name || planId;
239
+ const dueToday = preview?.amount_due_usd;
240
+ let question = null;
241
+ if (asking === "period_end") {
242
+ question = t("planChange.confirmSchedule", { plan: targetName });
243
+ } else if (asking) {
244
+ question =
245
+ dueToday > 0
246
+ ? t("planChange.confirmNowDue", { plan: targetName, amount: money(dueToday) })
247
+ : t("planChange.confirmNow", { plan: targetName });
248
+ }
202
249
 
203
250
  return React.createElement(
204
251
  "div",
@@ -235,31 +282,64 @@ function PlanChangeCard({
235
282
  planVersion,
236
283
  fetch: fetchImpl,
237
284
  }),
238
- React.createElement(
239
- "div",
240
- { style: styles.actions },
241
- React.createElement(
242
- "button",
243
- {
244
- type: "button",
245
- style: btnStyle(!ready || busy, true),
246
- disabled: !ready || busy,
247
- onClick: () => confirm("now"),
248
- },
249
- busy ? t("planChange.working") : t("planChange.now"),
250
- ),
251
- allowSchedule &&
252
- React.createElement(
253
- "button",
254
- {
255
- type: "button",
256
- style: btnStyle(!ready || busy, false),
257
- disabled: !ready || busy,
258
- onClick: () => confirm("period_end"),
259
- },
260
- t("planChange.schedule"),
285
+ question
286
+ ? React.createElement(
287
+ "div",
288
+ { style: styles.confirm },
289
+ React.createElement("p", { role: "alert", style: styles.question }, question),
290
+ React.createElement(
291
+ "div",
292
+ { style: styles.actions },
293
+ React.createElement(
294
+ "button",
295
+ {
296
+ type: "button",
297
+ style: btnStyle(false, true),
298
+ onClick: () => {
299
+ const effective = asking;
300
+ setAsking(null);
301
+ confirm(effective);
302
+ },
303
+ },
304
+ t("planChange.confirm"),
305
+ ),
306
+ React.createElement(
307
+ "button",
308
+ {
309
+ type: "button",
310
+ style: btnStyle(false, false),
311
+ ref: keepRef,
312
+ onClick: () => setAsking(null),
313
+ },
314
+ t("planChange.keep"),
315
+ ),
316
+ ),
317
+ )
318
+ : React.createElement(
319
+ "div",
320
+ { style: styles.actions },
321
+ React.createElement(
322
+ "button",
323
+ {
324
+ type: "button",
325
+ style: btnStyle(!ready || busy, true),
326
+ disabled: !ready || busy,
327
+ onClick: () => ask("now"),
328
+ },
329
+ busy ? t("planChange.working") : t("planChange.now"),
330
+ ),
331
+ allowSchedule &&
332
+ React.createElement(
333
+ "button",
334
+ {
335
+ type: "button",
336
+ style: btnStyle(!ready || busy, false),
337
+ disabled: !ready || busy,
338
+ onClick: () => ask("period_end"),
339
+ },
340
+ t("planChange.schedule"),
341
+ ),
261
342
  ),
262
- ),
263
343
  err && React.createElement("div", { style: { ...styles.message, ...styles.error } }, err),
264
344
  message &&
265
345
  !err &&
@@ -82,7 +82,9 @@ const styles = {
82
82
  padding: 4,
83
83
  width: "fit-content",
84
84
  margin: "0 auto",
85
- background: theme.accentTint(6),
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 = planVersion != null ? { plan_id: planId, plan_version: planVersion } : { plan_id: planId };
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
- style: {
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": "MRR now",
123
- "preview.mrrAfter": "MRR after",
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": "MRR actual",
276
- "preview.mrrAfter": "MRR después",
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": "MRR actuel",
429
- "preview.mrrAfter": "MRR après",
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": "MRR jetzt",
583
- "preview.mrrAfter": "MRR danach",
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",