@meterbox/react 0.1.0 → 0.1.1
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 +7 -1
- package/package.json +1 -1
- package/src/PricingTable.js +28 -7
package/README.md
CHANGED
|
@@ -44,7 +44,13 @@ import {
|
|
|
44
44
|
} from "@meterbox/react";
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
### `<PricingTable plans currentPlanId? currency? onSelect? />`
|
|
47
|
+
### `<PricingTable plans currentPlanId? currency? onSelect? />` or `<PricingTable baseUrl publishableKey … />`
|
|
48
|
+
|
|
49
|
+
On a public page with no backend of its own, pass `baseUrl` and a
|
|
50
|
+
publishable key (`mb_pub_...`, from Settings > API keys) instead of `plans`.
|
|
51
|
+
The table then reads the public pricing snapshot,
|
|
52
|
+
`GET /v1/catalog/pricing-page`: published plans with display fields only. A
|
|
53
|
+
publishable key is safe in page source and unlocks nothing else.
|
|
48
54
|
|
|
49
55
|
Renders the plan catalog as a comparison grid. Highlights `currentPlanId`
|
|
50
56
|
if supplied, picks a localized price when `currency` matches a row in
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meterbox/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
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",
|
package/src/PricingTable.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* <PricingTable plans={...} currentPlanId? onSelect? />
|
|
3
|
+
* <PricingTable baseUrl publishableKey />
|
|
3
4
|
*
|
|
4
5
|
* Renders the tenant's plan catalog as a comparison grid. `plans` is the
|
|
5
6
|
* array your backend fetched from `GET /v1/plans` (via
|
|
6
|
-
* `@meterbox/node`'s `getV1Plans`).
|
|
7
|
+
* `@meterbox/node`'s `getV1Plans`). Or, on a public page with no backend of
|
|
8
|
+
* its own, pass `baseUrl` + `publishableKey` (`mb_pub_...`, Settings > API
|
|
9
|
+
* keys) and it reads the public pricing snapshot instead
|
|
10
|
+
* (`GET /v1/catalog/pricing-page`: published plans, display fields only).
|
|
11
|
+
* Highlights `currentPlanId` if
|
|
7
12
|
* supplied; invokes `onSelect(plan, billingInterval)` when a CTA is
|
|
8
13
|
* clicked so the tenant can route to their own checkout / contact form —
|
|
9
14
|
* `billingInterval` ("monthly" or "annual") is whichever the shopper had
|
|
@@ -113,12 +118,27 @@ function formatPrice(amount, currency = "USD") {
|
|
|
113
118
|
}
|
|
114
119
|
}
|
|
115
120
|
|
|
116
|
-
|
|
117
|
-
|
|
121
|
+
/** A plan from the public snapshot, in the shape the table renders. */
|
|
122
|
+
function fromSnapshot(p) {
|
|
123
|
+
return {
|
|
124
|
+
id: p.id,
|
|
125
|
+
name: p.name,
|
|
126
|
+
description: p.description || null,
|
|
127
|
+
features: p.features || [],
|
|
128
|
+
stripe_price_monthly_usd: p.price ? p.price.monthly_usd : 0,
|
|
129
|
+
annual_free_months: p.annual_free_months == null ? null : p.annual_free_months,
|
|
130
|
+
localized_prices: p.localized_prices || [],
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function fetchCatalog({ baseUrl, publishableKey, fetchImpl }) {
|
|
135
|
+
const url = `${baseUrl.replace(/\/+$/, "")}/v1/catalog/pricing-page?key=${encodeURIComponent(
|
|
136
|
+
publishableKey,
|
|
137
|
+
)}`;
|
|
118
138
|
const res = await fetchImpl(url, { method: "GET" });
|
|
119
139
|
if (!res.ok) throw new Error(`pricing-table: ${res.status}`);
|
|
120
140
|
const body = await res.json();
|
|
121
|
-
return
|
|
141
|
+
return (body.plans || []).map(fromSnapshot);
|
|
122
142
|
}
|
|
123
143
|
|
|
124
144
|
function PricingTable({
|
|
@@ -127,16 +147,17 @@ function PricingTable({
|
|
|
127
147
|
currency = "USD",
|
|
128
148
|
onSelect,
|
|
129
149
|
baseUrl,
|
|
150
|
+
publishableKey,
|
|
130
151
|
fetch: fetchImpl,
|
|
131
152
|
}) {
|
|
132
|
-
const selfFetch = !Array.isArray(plans) && !!baseUrl;
|
|
153
|
+
const selfFetch = !Array.isArray(plans) && !!baseUrl && !!publishableKey;
|
|
133
154
|
const [fetched, setFetched] = React.useState(null);
|
|
134
155
|
const [billingInterval, setBillingInterval] = React.useState("monthly");
|
|
135
156
|
|
|
136
157
|
React.useEffect(() => {
|
|
137
158
|
if (!selfFetch) return undefined;
|
|
138
159
|
let alive = true;
|
|
139
|
-
fetchCatalog({ baseUrl, fetchImpl: fetchImpl || globalThis.fetch })
|
|
160
|
+
fetchCatalog({ baseUrl, publishableKey, fetchImpl: fetchImpl || globalThis.fetch })
|
|
140
161
|
.then((d) => {
|
|
141
162
|
if (alive) setFetched(d);
|
|
142
163
|
})
|
|
@@ -146,7 +167,7 @@ function PricingTable({
|
|
|
146
167
|
return () => {
|
|
147
168
|
alive = false;
|
|
148
169
|
};
|
|
149
|
-
}, [selfFetch, baseUrl, fetchImpl]);
|
|
170
|
+
}, [selfFetch, baseUrl, publishableKey, fetchImpl]);
|
|
150
171
|
|
|
151
172
|
const list = Array.isArray(plans) ? plans : fetched;
|
|
152
173
|
if (!Array.isArray(list) || list.length === 0) {
|