@stigg/react-sdk 5.7.0 → 5.8.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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "5.7.0",
2
+ "version": "5.8.0",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -1,25 +1,19 @@
1
1
  import React from 'react';
2
2
  import styled from '@emotion/styled/macro';
3
3
  import Grid from '@mui/material/Grid';
4
- import {
5
- BillingModel,
6
- BillingPeriod,
7
- Price,
8
- SubscriptionPreviewInvoice,
9
- SubscriptionPreviewV2,
10
- } from '@stigg/js-client-sdk';
4
+ import { BillingModel, Price, SubscriptionPreviewInvoice, SubscriptionPreviewV2 } from '@stigg/js-client-sdk';
11
5
  import { Typography } from '../../../common/Typography';
12
6
  import { currencyPriceFormatter } from '../../../utils/currencyUtils';
13
- import { calculateTierPrice } from '../../../utils/priceTierUtils';
14
7
  import { WithSkeleton } from './WithSkeleton';
15
8
  import { Skeleton } from '../../components/Skeletons.style';
16
9
  import { CheckoutLocalization } from '../../configurations/textOverrides';
17
10
  import { Icon } from '../../../common/Icon';
18
11
  import { InformationTooltip } from '../../../common/InformationTooltip';
12
+ import { getPriceBreakdownString } from './getPriceBreakdownString';
19
13
 
20
14
  export const LineItemContainer = styled.div`
21
15
  & + & {
22
- margin-top: 8px;
16
+ margin-top: 16px;
23
17
  }
24
18
  `;
25
19
 
@@ -29,26 +23,6 @@ export const LineItemRow = styled.div`
29
23
  justify-content: space-between;
30
24
  `;
31
25
 
32
- export const getPriceString = ({ amount, price, quantity }: { amount: number; price: Price; quantity: number }) => {
33
- const { billingPeriod } = price;
34
- let billingPeriodString = null;
35
-
36
- if (quantity) {
37
- amount /= quantity;
38
- }
39
-
40
- if (billingPeriod === BillingPeriod.Annually) {
41
- amount /= 12;
42
- billingPeriodString = '12 months';
43
- }
44
-
45
- const addonPriceFormat = currencyPriceFormatter({ amount, currency: price.currency, minimumFractionDigits: 2 });
46
-
47
- return `${quantity > 1 ? `${quantity} x ${addonPriceFormat} each` : addonPriceFormat}${
48
- billingPeriodString ? ` x ${billingPeriodString}` : ''
49
- }`;
50
- };
51
-
52
26
  const PayAsYouGoPriceTooltip = ({ checkoutLocalization }: { checkoutLocalization: CheckoutLocalization }) => {
53
27
  const title = <Typography variant="body1">{checkoutLocalization.summary.payAsYouGoTooltipText}</Typography>;
54
28
  return (
@@ -69,16 +43,8 @@ export const BilledPriceLineItem = ({
69
43
  quantity: number;
70
44
  price: Price;
71
45
  }) => {
72
- const { billingPeriod } = price;
73
46
  const isPayAsYouGo = price.pricingModel === BillingModel.UsageBased;
74
47
 
75
- let amount;
76
- if (price.isTieredPrice) {
77
- amount = calculateTierPrice(price, quantity);
78
- } else {
79
- amount = price.amount! * quantity;
80
- }
81
-
82
48
  return (
83
49
  <LineItemContainer>
84
50
  <LineItemRow style={{ alignItems: 'flex-start' }}>
@@ -86,20 +52,11 @@ export const BilledPriceLineItem = ({
86
52
  <Typography variant="body1" color="secondary">
87
53
  {label}
88
54
  </Typography>
89
- {(quantity > 1 || billingPeriod === BillingPeriod.Annually) && (
90
- <Typography variant="body1" color="secondary">
91
- {getPriceString({ amount, price, quantity })}
92
- </Typography>
93
- )}
94
55
  </Grid>
95
56
  <Grid item display="flex" gap={1} alignItems="center">
96
57
  {isPayAsYouGo && <PayAsYouGoPriceTooltip checkoutLocalization={checkoutLocalization} />}
97
58
  <Typography variant="body1" color="secondary" style={{ wordBreak: 'break-word' }}>
98
- {currencyPriceFormatter({
99
- amount,
100
- currency: price.currency,
101
- minimumFractionDigits: 2,
102
- })}
59
+ {getPriceBreakdownString({ price, quantity })}
103
60
  {isPayAsYouGo && ' / unit'}
104
61
  </Typography>
105
62
  </Grid>
@@ -0,0 +1,45 @@
1
+ import { BillingModel, BillingPeriod, Price, TiersMode } from '@stigg/js-client-sdk';
2
+ import { currencyPriceFormatter } from '../../../utils/currencyUtils';
3
+ import { calculateTierPrice, isBulkTiers } from '../../../utils/priceTierUtils';
4
+
5
+ function formatPricePerUnit({ price, quantity, totalAmount }: { price: Price; quantity: number; totalAmount: number }) {
6
+ const { currency, pricingModel, billingPeriod } = price;
7
+ const isPerUnit = pricingModel === BillingModel.PerUnit;
8
+ const featureUnits = quantity && (isPerUnit || quantity > 1) ? `${new Intl.NumberFormat().format(quantity)} x ` : '';
9
+ const billingPeriodString = billingPeriod === BillingPeriod.Annually ? ' x 12 months' : '';
10
+
11
+ const unitPrice = totalAmount / quantity / (billingPeriod === BillingPeriod.Annually ? 12 : 1);
12
+ const formattedUnitPrice = currencyPriceFormatter({
13
+ amount: unitPrice,
14
+ currency,
15
+ minimumFractionDigits: 2,
16
+ });
17
+ const formattedTotalPrice = currencyPriceFormatter({
18
+ amount: totalAmount,
19
+ currency,
20
+ minimumFractionDigits: 2,
21
+ });
22
+
23
+ return `${featureUnits}${formattedUnitPrice}${billingPeriodString} ${
24
+ billingPeriodString || featureUnits ? ` = ${formattedTotalPrice}` : ''
25
+ }`;
26
+ }
27
+
28
+ export function getPriceBreakdownString({ price, quantity }: { price: Price; quantity: number }) {
29
+ const amount = price.isTieredPrice ? calculateTierPrice(price, quantity) : (price.amount || 0) * quantity;
30
+
31
+ if (isBulkTiers(price.tiers) || price.tiersMode === TiersMode.Graduated) {
32
+ const formattedTotalPrice = currencyPriceFormatter({
33
+ amount,
34
+ currency: price.currency,
35
+ minimumFractionDigits: 2,
36
+ });
37
+ return `${quantity} for ${formattedTotalPrice}`;
38
+ }
39
+
40
+ return formatPricePerUnit({
41
+ price,
42
+ quantity,
43
+ totalAmount: amount,
44
+ });
45
+ }
@@ -123,6 +123,10 @@ export function hasTierWithUnitPrice(tiers: PriceTierFragment[] | null | undefin
123
123
  return tiers?.some(({ unitPrice, upTo }) => unitPrice && !isNil(upTo));
124
124
  }
125
125
 
126
+ export function isBulkTiers(tiers: PriceTierFragment[] | null | undefined) {
127
+ return tiers?.every(({ unitPrice, upTo }) => !unitPrice || isNil(upTo));
128
+ }
129
+
126
130
  export function getTiersPerUnitQuantities(
127
131
  plan: PaywallPlan,
128
132
  billingPeriod: BillingPeriod,