@schematichq/schematic-components 2.26.0 → 2.28.0
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 +91 -0
- package/dist/schematic-components-elements.cjs.js +324 -10
- package/dist/schematic-components-elements.d.ts +169 -0
- package/dist/schematic-components-elements.esm.js +317 -2
- package/dist/schematic-components.browser.cjs.js +665 -7
- package/dist/schematic-components.browser.esm.js +665 -7
- package/dist/schematic-components.cjs.js +665 -7
- package/dist/schematic-components.d.ts +34 -0
- package/dist/schematic-components.esm.js +665 -7
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -93,6 +93,97 @@ notice that appears in the same spot.
|
|
|
93
93
|
The Plans and add-ons available to the checkout flows must be live in your
|
|
94
94
|
Schematic account [Catalog configuration](https://docs.schematichq.com/catalog/overview).
|
|
95
95
|
|
|
96
|
+
To offer an add-on that isn't live, see
|
|
97
|
+
[Rehydrating With Params](#rehydrating-with-params).
|
|
98
|
+
|
|
99
|
+
## Rehydrating With Params
|
|
100
|
+
|
|
101
|
+
We provide a function `rehydrateWithParams` for re-fetching the embed's data
|
|
102
|
+
with params the standard load doesn't send. Like `initializeWithPlan`, it's
|
|
103
|
+
suitable for click handlers and must be extracted from the library's embedded
|
|
104
|
+
context.
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
const { rehydrateWithParams } = useEmbed();
|
|
108
|
+
|
|
109
|
+
const data = await rehydrateWithParams({ includeAddOnIds: ['plan_BQx8kWjSx4'] });
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
It re-fetches the component your `SchematicEmbed` loaded and applies the
|
|
113
|
+
result in place:
|
|
114
|
+
|
|
115
|
+
- The embed's data updates without the embed showing its loading state.
|
|
116
|
+
- It isn't debounced: every call makes a request, and the promise resolves
|
|
117
|
+
with the new data once it's applied.
|
|
118
|
+
- The promise rejects if the request fails, leaving the embed's data and
|
|
119
|
+
error state as they were, so your UI decides how to handle it. It also
|
|
120
|
+
rejects if no `SchematicEmbed` has loaded yet.
|
|
121
|
+
- The params apply to this fetch only. The embed's next refresh, such as the
|
|
122
|
+
one after a purchase, goes back to the standard load.
|
|
123
|
+
|
|
124
|
+
Await it before anything that depends on the new data, such as opening a
|
|
125
|
+
checkout with `initializeWithPlan`.
|
|
126
|
+
|
|
127
|
+
### Params
|
|
128
|
+
|
|
129
|
+
| Param | Type | Effect |
|
|
130
|
+
| ----------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
131
|
+
| `includeAddOnIds` | `string[]` | Add-ons to offer in the checkout even though they aren't live. Each must be an add-on with a billing product; any other ID is left out, with a console warning. |
|
|
132
|
+
|
|
133
|
+
### Example: selling an add-on that isn't live
|
|
134
|
+
|
|
135
|
+
Say your backend creates an add-on for a single purchase, such as a one-off
|
|
136
|
+
service, and deletes it afterward, so it's never live in your catalog. A "Buy
|
|
137
|
+
now" button can create it, include it, and open the checkout with it
|
|
138
|
+
selected:
|
|
139
|
+
|
|
140
|
+
```tsx
|
|
141
|
+
import { useState } from 'react';
|
|
142
|
+
import { useEmbed } from '@schematichq/schematic-components';
|
|
143
|
+
|
|
144
|
+
function BuyServiceButton({ serviceId }: { serviceId: string }) {
|
|
145
|
+
const { rehydrateWithParams, initializeWithPlan } = useEmbed();
|
|
146
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
147
|
+
const [error, setError] = useState<string>();
|
|
148
|
+
|
|
149
|
+
const handleClick = async () => {
|
|
150
|
+
setIsLoading(true);
|
|
151
|
+
setError(undefined);
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
// Your backend creates the add-on and returns its Schematic ID
|
|
155
|
+
const { addOnId } = await createServiceAddOn(serviceId);
|
|
156
|
+
|
|
157
|
+
// Fetch the embed's data again, this time with the add-on in it
|
|
158
|
+
await rehydrateWithParams({ includeAddOnIds: [addOnId] });
|
|
159
|
+
|
|
160
|
+
// Open the checkout. Including an add-on only makes it available;
|
|
161
|
+
// list it in `addOnIds` to pre-select it.
|
|
162
|
+
initializeWithPlan({
|
|
163
|
+
addOnIds: [addOnId],
|
|
164
|
+
skipped: { addOnStage: true }, // it's already chosen
|
|
165
|
+
});
|
|
166
|
+
} catch {
|
|
167
|
+
setError("We couldn't start the checkout. Please try again.");
|
|
168
|
+
} finally {
|
|
169
|
+
setIsLoading(false);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
return (
|
|
174
|
+
<>
|
|
175
|
+
<button onClick={handleClick} disabled={isLoading}>
|
|
176
|
+
{isLoading ? 'Preparing checkout…' : 'Buy now'}
|
|
177
|
+
</button>
|
|
178
|
+
{error && <p role="alert">{error}</p>}
|
|
179
|
+
</>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
The add-on still has to be compatible with the selected plan and priced in
|
|
185
|
+
the checkout's currency and billing period to appear.
|
|
186
|
+
|
|
96
187
|
## Programmatic Unsubscribe
|
|
97
188
|
|
|
98
189
|
We provide a function `requestUnsubscribe` for starting the unsubscribe flow
|
|
@@ -20,18 +20,22 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/elements/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
BillingDataProvider: () =>
|
|
23
|
+
BillingDataProvider: () => import_schematic_react4.BillingDataProvider,
|
|
24
24
|
DEFAULT_STRINGS: () => DEFAULT_STRINGS,
|
|
25
25
|
Invoices: () => Invoices,
|
|
26
26
|
SCHEMATIC_TOKENS: () => SCHEMATIC_TOKENS,
|
|
27
|
-
SchematicI18nProvider: () =>
|
|
27
|
+
SchematicI18nProvider: () => import_schematic_react4.SchematicI18nProvider,
|
|
28
28
|
SchematicStyles: () => SchematicStyles,
|
|
29
|
+
UpcomingBill: () => UpcomingBill,
|
|
30
|
+
billingResources: () => billingResources,
|
|
29
31
|
deriveInvoiceList: () => deriveInvoiceList,
|
|
32
|
+
deriveUpcomingInvoice: () => deriveUpcomingInvoice,
|
|
30
33
|
featureName: () => featureName,
|
|
31
34
|
formatConsumptionRate: () => formatConsumptionRate,
|
|
32
35
|
formatCurrency: () => formatCurrency,
|
|
33
36
|
formatDate: () => formatDate,
|
|
34
37
|
formatNumber: () => formatNumber,
|
|
38
|
+
formatPercent: () => formatPercent,
|
|
35
39
|
formatShortDate: () => formatShortDate,
|
|
36
40
|
httpStatus: () => httpStatus,
|
|
37
41
|
interpolate: () => interpolate,
|
|
@@ -40,18 +44,19 @@ __export(index_exports, {
|
|
|
40
44
|
resolveLocale: () => resolveLocale,
|
|
41
45
|
schematicStylesCss: () => schematicStylesCss,
|
|
42
46
|
schematicTokensCss: () => schematicTokensCss,
|
|
43
|
-
useInvalidateBillingData: () =>
|
|
44
|
-
useInvoices: () =>
|
|
47
|
+
useInvalidateBillingData: () => import_schematic_react4.useInvalidateBillingData,
|
|
48
|
+
useInvoices: () => import_schematic_react4.useInvoices,
|
|
45
49
|
useResolvedLocale: () => useResolvedLocale,
|
|
46
|
-
useSchematicI18n: () =>
|
|
47
|
-
useSchematicLocale: () =>
|
|
48
|
-
useSchematicStrings: () =>
|
|
49
|
-
useSchematicTranslate: () =>
|
|
50
|
+
useSchematicI18n: () => import_schematic_react4.useSchematicI18n,
|
|
51
|
+
useSchematicLocale: () => import_schematic_react4.useSchematicLocale,
|
|
52
|
+
useSchematicStrings: () => import_schematic_react4.useSchematicStrings,
|
|
53
|
+
useSchematicTranslate: () => import_schematic_react4.useSchematicTranslate,
|
|
50
54
|
useTranslator: () => useTranslator,
|
|
55
|
+
useUpcomingInvoice: () => import_schematic_react4.useUpcomingInvoice,
|
|
51
56
|
viewerLocale: () => viewerLocale
|
|
52
57
|
});
|
|
53
58
|
module.exports = __toCommonJS(index_exports);
|
|
54
|
-
var
|
|
59
|
+
var import_schematic_react4 = require("@schematichq/schematic-react");
|
|
55
60
|
|
|
56
61
|
// src/elements/model/errors.ts
|
|
57
62
|
function httpStatus(error) {
|
|
@@ -511,6 +516,12 @@ function formatConsumptionRate(rate, locale) {
|
|
|
511
516
|
maximumFractionDigits: MAXIMUM_FRACTION_DIGITS
|
|
512
517
|
});
|
|
513
518
|
}
|
|
519
|
+
function formatPercent(fraction, locale) {
|
|
520
|
+
return new Intl.NumberFormat(usableLocale(locale), {
|
|
521
|
+
style: "percent",
|
|
522
|
+
maximumFractionDigits: 2
|
|
523
|
+
}).format(fraction);
|
|
524
|
+
}
|
|
514
525
|
function usableDate(date) {
|
|
515
526
|
if (date === null || date === void 0) {
|
|
516
527
|
return void 0;
|
|
@@ -600,6 +611,64 @@ function deriveInvoiceList(page, options) {
|
|
|
600
611
|
};
|
|
601
612
|
}
|
|
602
613
|
|
|
614
|
+
// src/elements/model/upcomingBill.ts
|
|
615
|
+
function deriveUpcomingInvoice(invoice, options) {
|
|
616
|
+
const { format, locale } = options;
|
|
617
|
+
const currency = invoice.currency.toUpperCase();
|
|
618
|
+
const money = (amountMinor) => format?.amount?.(amountMinor, currency, locale) ?? formatCurrency(amountMinor, currency, locale, {
|
|
619
|
+
preserveSubUnitPrecision: false
|
|
620
|
+
});
|
|
621
|
+
const line = (amountMinor) => ({
|
|
622
|
+
amountMinor,
|
|
623
|
+
amountText: money(amountMinor)
|
|
624
|
+
});
|
|
625
|
+
const applied = invoice.customerBalanceApplied;
|
|
626
|
+
const remaining = invoice.customerBalanceRemaining;
|
|
627
|
+
const dueDate = usableDate(invoice.dueDate) ?? null;
|
|
628
|
+
return {
|
|
629
|
+
amountDueMinor: invoice.amountDue,
|
|
630
|
+
amountDueText: money(invoice.amountDue),
|
|
631
|
+
subtotalMinor: invoice.subtotal,
|
|
632
|
+
subtotalText: money(invoice.subtotal),
|
|
633
|
+
currency,
|
|
634
|
+
dueAt: dueDate === null ? null : {
|
|
635
|
+
date: dueDate,
|
|
636
|
+
text: format?.date?.(dueDate, locale) ?? formatDate(dueDate, locale)
|
|
637
|
+
},
|
|
638
|
+
// A deduction: the sign is the locale's, so a host never assembles one.
|
|
639
|
+
balanceApplied: applied > 0 ? line(-applied) : null,
|
|
640
|
+
// Zero remaining is worth saying when a balance was spent; no balance at
|
|
641
|
+
// all is not.
|
|
642
|
+
balanceRemaining: applied > 0 || remaining > 0 ? line(remaining) : null,
|
|
643
|
+
discounts: invoice.discounts.flatMap(
|
|
644
|
+
(discount) => discountLine(discount, currency, locale, money)
|
|
645
|
+
)
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
function discountLine(discount, invoiceCurrency, locale, money) {
|
|
649
|
+
const percentOff = discount.percentOff ?? null;
|
|
650
|
+
const amountOff = discount.amountOff ?? null;
|
|
651
|
+
const isPercent = percentOff !== null && percentOff > 0;
|
|
652
|
+
if (!isPercent && (amountOff === null || amountOff <= 0)) {
|
|
653
|
+
return [];
|
|
654
|
+
}
|
|
655
|
+
const currency = discount.currency?.toUpperCase() ?? invoiceCurrency;
|
|
656
|
+
return [
|
|
657
|
+
{
|
|
658
|
+
couponName: discount.couponName,
|
|
659
|
+
code: discount.customerFacingCode ?? null,
|
|
660
|
+
kind: isPercent ? "percent" : "amount",
|
|
661
|
+
valueText: isPercent ? formatPercent(percentOff / 100, locale) : currency === invoiceCurrency ? money(amountOff ?? 0) : formatCurrency(amountOff ?? 0, currency, locale, {
|
|
662
|
+
preserveSubUnitPrecision: false
|
|
663
|
+
}),
|
|
664
|
+
percentOff: isPercent ? percentOff : null,
|
|
665
|
+
amountOffMinor: isPercent ? null : amountOff,
|
|
666
|
+
months: discount.duration === "repeating" ? discount.durationInMonths ?? null : null,
|
|
667
|
+
duration: discount.duration
|
|
668
|
+
}
|
|
669
|
+
];
|
|
670
|
+
}
|
|
671
|
+
|
|
603
672
|
// src/elements/strings.ts
|
|
604
673
|
var DEFAULT_STRINGS_LOCALE = "en";
|
|
605
674
|
var DEFAULT_STRINGS = {
|
|
@@ -614,7 +683,21 @@ var DEFAULT_STRINGS = {
|
|
|
614
683
|
invoicesLoadMore: "Load more",
|
|
615
684
|
invoicesChargeTooltip: "Charge \u2014 you were billed this amount",
|
|
616
685
|
invoicesCreditTooltip: "Credit \u2014 this amount was returned to your account, typically due to a plan change or proration",
|
|
617
|
-
invoicesUndated: "View invoice"
|
|
686
|
+
invoicesUndated: "View invoice",
|
|
687
|
+
upcomingBillLoading: "Loading your next bill",
|
|
688
|
+
upcomingBillHeader: "Next bill due {{date}}",
|
|
689
|
+
upcomingBillError: "There was a problem retrieving your upcoming invoice.",
|
|
690
|
+
upcomingBillUnavailable: "Your upcoming invoice is not available for this account.",
|
|
691
|
+
upcomingBillEmpty: "No upcoming invoice",
|
|
692
|
+
upcomingBillEstimate: "Estimated bill",
|
|
693
|
+
upcomingBillBalanceApplied: "Applied balance towards next invoice",
|
|
694
|
+
upcomingBillBalanceRemaining: "Remaining balance after next invoice",
|
|
695
|
+
upcomingBillDiscount: "Discount",
|
|
696
|
+
upcomingBillDiscountValue: "{{value}} off",
|
|
697
|
+
// Looked up by the bare key with `{ count }`; English has two forms.
|
|
698
|
+
upcomingBillDiscountRepeating: "{{value}} off for next {{count}} months",
|
|
699
|
+
upcomingBillDiscountRepeating_one: "{{value}} off for next month",
|
|
700
|
+
upcomingBillDiscountRepeating_other: "{{value}} off for next {{count}} months"
|
|
618
701
|
};
|
|
619
702
|
var MISSING_STRING = "\0schematic:missing";
|
|
620
703
|
function interpolate(template, vars) {
|
|
@@ -781,6 +864,83 @@ ${Object.entries(SCHEMATIC_TOKENS).map(([name, value]) => ` ${name}: ${value};`
|
|
|
781
864
|
}
|
|
782
865
|
`;
|
|
783
866
|
|
|
867
|
+
// src/elements/styles/upcoming-bill.ts
|
|
868
|
+
var upcomingBillCss = `
|
|
869
|
+
.schematic-upcoming-bill {
|
|
870
|
+
display: flex;
|
|
871
|
+
flex-direction: column;
|
|
872
|
+
gap: var(--schematic-space);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
.schematic-upcoming-bill .schematic-header {
|
|
876
|
+
margin-bottom: 0;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
.schematic-upcoming-bill .schematic-header__title {
|
|
880
|
+
margin-bottom: 0;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
.schematic-upcoming-bill__amount {
|
|
884
|
+
align-items: flex-start;
|
|
885
|
+
display: flex;
|
|
886
|
+
gap: var(--schematic-space);
|
|
887
|
+
justify-content: space-between;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
.schematic-upcoming-bill__total {
|
|
891
|
+
font-family: var(--schematic-font-heading);
|
|
892
|
+
font-size: 1.8125rem;
|
|
893
|
+
font-variant-numeric: tabular-nums;
|
|
894
|
+
font-weight: 800;
|
|
895
|
+
line-height: 1;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
.schematic-upcoming-bill__estimate {
|
|
899
|
+
font-weight: 600;
|
|
900
|
+
max-width: 10rem;
|
|
901
|
+
text-align: end;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
.schematic-upcoming-bill__rows {
|
|
905
|
+
display: flex;
|
|
906
|
+
flex-direction: column;
|
|
907
|
+
gap: var(--schematic-space);
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
.schematic-upcoming-bill .schematic-row__value {
|
|
911
|
+
font-variant-numeric: tabular-nums;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
.schematic-upcoming-bill__discounts {
|
|
915
|
+
align-items: flex-end;
|
|
916
|
+
display: flex;
|
|
917
|
+
flex-direction: column;
|
|
918
|
+
gap: calc(var(--schematic-space) / 2);
|
|
919
|
+
list-style: none;
|
|
920
|
+
margin: 0;
|
|
921
|
+
padding: 0;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
.schematic-upcoming-bill__discount {
|
|
925
|
+
align-items: center;
|
|
926
|
+
display: inline-flex;
|
|
927
|
+
gap: calc(var(--schematic-space) / 2);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
.schematic-upcoming-bill__empty {
|
|
931
|
+
padding: calc(var(--schematic-space) / 2) 0;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
.schematic-upcoming-bill .schematic-skeleton__cell[data-column="amount"] {
|
|
935
|
+
height: 1.8125rem;
|
|
936
|
+
width: 7rem;
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
.schematic-upcoming-bill .schematic-skeleton__cell[data-column="row"] {
|
|
940
|
+
width: 12rem;
|
|
941
|
+
}
|
|
942
|
+
`;
|
|
943
|
+
|
|
784
944
|
// src/elements/styles/index.tsx
|
|
785
945
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
786
946
|
var rulesCss = `
|
|
@@ -824,6 +984,12 @@ var rulesCss = `
|
|
|
824
984
|
gap: var(--schematic-space);
|
|
825
985
|
}
|
|
826
986
|
|
|
987
|
+
/* The message may run long; the action beside it stays one line. */
|
|
988
|
+
.schematic-status__retry {
|
|
989
|
+
flex-shrink: 0;
|
|
990
|
+
white-space: nowrap;
|
|
991
|
+
}
|
|
992
|
+
|
|
827
993
|
/* Visually hidden, still read by assistive technology. */
|
|
828
994
|
.schematic-hidden {
|
|
829
995
|
border: 0;
|
|
@@ -1090,6 +1256,22 @@ var rulesCss = `
|
|
|
1090
1256
|
margin: 0 0 calc(var(--schematic-space) / 2);
|
|
1091
1257
|
}
|
|
1092
1258
|
|
|
1259
|
+
/* A label on the left, its value on the right. */
|
|
1260
|
+
.schematic-row {
|
|
1261
|
+
align-items: flex-start;
|
|
1262
|
+
display: flex;
|
|
1263
|
+
gap: var(--schematic-space);
|
|
1264
|
+
justify-content: space-between;
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
.schematic-row__label {
|
|
1268
|
+
font-weight: 600;
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
.schematic-row__value {
|
|
1272
|
+
text-align: end;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1093
1275
|
.schematic-feature-list {
|
|
1094
1276
|
display: flex;
|
|
1095
1277
|
flex-direction: column;
|
|
@@ -1276,6 +1458,7 @@ var rulesCss = `
|
|
|
1276
1458
|
}
|
|
1277
1459
|
|
|
1278
1460
|
${invoicesCss}
|
|
1461
|
+
${upcomingBillCss}
|
|
1279
1462
|
`;
|
|
1280
1463
|
var schematicStylesCss = withTokenDefaults(rulesCss);
|
|
1281
1464
|
var SchematicStyles = () => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { "data-schematic-styles": "", children: schematicStylesCss });
|
|
@@ -1289,6 +1472,17 @@ var import_schematic_react = require("@schematichq/schematic-react");
|
|
|
1289
1472
|
var import_react = require("react");
|
|
1290
1473
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
1291
1474
|
var cx = (...names) => names.filter((name) => typeof name === "string" && name !== "").join(" ");
|
|
1475
|
+
function billingResources(...elements) {
|
|
1476
|
+
const names = [];
|
|
1477
|
+
for (const element of elements) {
|
|
1478
|
+
for (const name of element.resources) {
|
|
1479
|
+
if (!names.includes(name)) {
|
|
1480
|
+
names.push(name);
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
return names;
|
|
1485
|
+
}
|
|
1292
1486
|
function useResolvedLocale(locale) {
|
|
1293
1487
|
const configured = (0, import_schematic_react.useSchematicLocale)();
|
|
1294
1488
|
const viewer = (0, import_react.useSyncExternalStore)(
|
|
@@ -1543,3 +1737,123 @@ function InvoiceDate({ row, t }) {
|
|
|
1543
1737
|
}
|
|
1544
1738
|
);
|
|
1545
1739
|
}
|
|
1740
|
+
Invoices.resources = ["invoices"];
|
|
1741
|
+
|
|
1742
|
+
// src/elements/UpcomingBill.tsx
|
|
1743
|
+
var import_schematic_react3 = require("@schematichq/schematic-react");
|
|
1744
|
+
var import_react3 = require("react");
|
|
1745
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
1746
|
+
function UpcomingBill({
|
|
1747
|
+
className,
|
|
1748
|
+
headingLevel = 2,
|
|
1749
|
+
locale: localeProp,
|
|
1750
|
+
showAmount = true,
|
|
1751
|
+
showBalance = true,
|
|
1752
|
+
showDiscounts = true,
|
|
1753
|
+
showHeader = true,
|
|
1754
|
+
strings
|
|
1755
|
+
}) {
|
|
1756
|
+
const { data: invoice, error, isPending, refetch } = (0, import_schematic_react3.useUpcomingInvoice)();
|
|
1757
|
+
const locale = useResolvedLocale(localeProp);
|
|
1758
|
+
const t = useTranslator(strings, localeProp);
|
|
1759
|
+
const bill = (0, import_react3.useMemo)(
|
|
1760
|
+
() => invoice === void 0 || invoice === null ? null : deriveUpcomingInvoice(invoice, { locale }),
|
|
1761
|
+
[invoice, locale]
|
|
1762
|
+
);
|
|
1763
|
+
const Heading = `h${headingLevel}`;
|
|
1764
|
+
const balanceRows = showBalance && bill !== null && (bill.balanceApplied !== null || bill.balanceRemaining !== null);
|
|
1765
|
+
const discountRow = showDiscounts && bill !== null && bill.discounts.length > 0;
|
|
1766
|
+
const unavailable = invoice === void 0 && httpStatus(error) === 404;
|
|
1767
|
+
const errorMessage = error === void 0 ? void 0 : unavailable ? t("upcomingBillUnavailable") : t("upcomingBillError");
|
|
1768
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
1769
|
+
StatusFrame,
|
|
1770
|
+
{
|
|
1771
|
+
className: cx("schematic-card", "schematic-upcoming-bill", className),
|
|
1772
|
+
error,
|
|
1773
|
+
errorMessage,
|
|
1774
|
+
hasData: invoice !== void 0,
|
|
1775
|
+
isPending,
|
|
1776
|
+
loadingLabel: t("upcomingBillLoading"),
|
|
1777
|
+
onRetry: refetch,
|
|
1778
|
+
retryText: t("retry"),
|
|
1779
|
+
skeleton: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(UpcomingBillSkeleton, { showHeader }),
|
|
1780
|
+
children: bill === null ? invoice !== void 0 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { className: "schematic-muted schematic-upcoming-bill__empty", children: t("upcomingBillEmpty") }) : /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
|
|
1781
|
+
showHeader && bill.dueAt !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "schematic-header", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Heading, { className: "schematic-header__title", children: t("upcomingBillHeader", { date: bill.dueAt.text }) }) }),
|
|
1782
|
+
showAmount && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "schematic-upcoming-bill__amount", children: [
|
|
1783
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
1784
|
+
"span",
|
|
1785
|
+
{
|
|
1786
|
+
className: "schematic-upcoming-bill__total",
|
|
1787
|
+
"data-testid": "schematic-upcoming-total",
|
|
1788
|
+
children: bill.amountDueText
|
|
1789
|
+
}
|
|
1790
|
+
),
|
|
1791
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "schematic-small schematic-upcoming-bill__estimate", children: t("upcomingBillEstimate") })
|
|
1792
|
+
] }),
|
|
1793
|
+
(balanceRows || discountRow) && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "schematic-upcoming-bill__rows", children: [
|
|
1794
|
+
balanceRows && bill.balanceApplied !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
1795
|
+
"div",
|
|
1796
|
+
{
|
|
1797
|
+
className: "schematic-row schematic-upcoming-bill__balance-applied",
|
|
1798
|
+
"data-testid": "schematic-balance-applied",
|
|
1799
|
+
children: [
|
|
1800
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "schematic-row__label", children: t("upcomingBillBalanceApplied") }),
|
|
1801
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "schematic-row__value", children: bill.balanceApplied.amountText })
|
|
1802
|
+
]
|
|
1803
|
+
}
|
|
1804
|
+
),
|
|
1805
|
+
balanceRows && bill.balanceRemaining !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
1806
|
+
"div",
|
|
1807
|
+
{
|
|
1808
|
+
className: "schematic-row schematic-upcoming-bill__balance-remaining",
|
|
1809
|
+
"data-testid": "schematic-balance-remaining",
|
|
1810
|
+
children: [
|
|
1811
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "schematic-row__label", children: t("upcomingBillBalanceRemaining") }),
|
|
1812
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "schematic-row__value", children: bill.balanceRemaining.amountText })
|
|
1813
|
+
]
|
|
1814
|
+
}
|
|
1815
|
+
),
|
|
1816
|
+
discountRow && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
1817
|
+
"div",
|
|
1818
|
+
{
|
|
1819
|
+
className: "schematic-row schematic-upcoming-bill__discount-row",
|
|
1820
|
+
"data-testid": "schematic-discounts",
|
|
1821
|
+
children: [
|
|
1822
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "schematic-row__label", children: t("upcomingBillDiscount") }),
|
|
1823
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("ul", { className: "schematic-row__value schematic-upcoming-bill__discounts", children: bill.discounts.map((discount, index) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
1824
|
+
"li",
|
|
1825
|
+
{
|
|
1826
|
+
className: "schematic-upcoming-bill__discount",
|
|
1827
|
+
"data-testid": "schematic-discount",
|
|
1828
|
+
children: [
|
|
1829
|
+
discount.code !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "schematic-chip schematic-upcoming-bill__code", children: discount.code }),
|
|
1830
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "schematic-upcoming-bill__discount-value", children: discountText(discount, t) })
|
|
1831
|
+
]
|
|
1832
|
+
},
|
|
1833
|
+
`${discount.couponName}-${index}`
|
|
1834
|
+
)) })
|
|
1835
|
+
]
|
|
1836
|
+
}
|
|
1837
|
+
)
|
|
1838
|
+
] })
|
|
1839
|
+
] })
|
|
1840
|
+
}
|
|
1841
|
+
);
|
|
1842
|
+
}
|
|
1843
|
+
function UpcomingBillSkeleton({ showHeader }) {
|
|
1844
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "schematic-skeleton", children: [
|
|
1845
|
+
showHeader && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "schematic-skeleton__heading" }),
|
|
1846
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "schematic-skeleton__row", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "schematic-skeleton__cell", "data-column": "amount" }) }),
|
|
1847
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "schematic-skeleton__row", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "schematic-skeleton__cell", "data-column": "row" }) })
|
|
1848
|
+
] });
|
|
1849
|
+
}
|
|
1850
|
+
function discountText(discount, t) {
|
|
1851
|
+
if (discount.months === null) {
|
|
1852
|
+
return t("upcomingBillDiscountValue", { value: discount.valueText });
|
|
1853
|
+
}
|
|
1854
|
+
return t("upcomingBillDiscountRepeating", {
|
|
1855
|
+
count: discount.months,
|
|
1856
|
+
value: discount.valueText
|
|
1857
|
+
});
|
|
1858
|
+
}
|
|
1859
|
+
UpcomingBill.resources = ["upcomingInvoice"];
|