@stigg/react-sdk 4.2.1 → 4.2.3

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": "4.2.1",
2
+ "version": "4.2.3",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -87,7 +87,7 @@ export function BillingPeriodPicker({
87
87
  >
88
88
  Annual
89
89
  </PeriodText>
90
- <DiscountRate discount={discountRate} disabled={isMonthlyBillingPeriod} />
90
+ {discountRate !== 0 && <DiscountRate discount={discountRate} disabled={isMonthlyBillingPeriod} />}
91
91
  </PickerContainer>
92
92
  );
93
93
  }
@@ -40,6 +40,7 @@ export function useLoadPaywallData({
40
40
  });
41
41
  const { availableBillingPeriods, defaultBillingPeriod } = computeBillingPeriods(
42
42
  paywallResult.plans,
43
+ paywallResult.activeSubscriptions,
43
44
  preferredBillingPeriod,
44
45
  );
45
46
 
@@ -41,14 +41,13 @@ export function getSelectedTier(
41
41
  const tieredPrice = currentSubscription.prices.find(
42
42
  subscriptionPrice =>
43
43
  subscriptionPrice.pricingModel == BillingModel.PerUnit &&
44
- subscriptionPrice.billingPeriod == billingPeriod &&
45
44
  subscriptionPrice.tiersMode &&
46
45
  subscriptionPrice.feature?.featureId === featureId,
47
46
  );
48
47
 
49
48
  if (tieredPrice) {
50
- const customerTier = tieredPrice.tiers!.find(tier => tier.upTo === tieredPrice.feature!.unitQuantity);
51
- currentTier = customerTier || tieredPrice.tiers![0];
49
+ const customerTier = price.tiers!.find(tier => tier.upTo === tieredPrice.feature!.unitQuantity);
50
+ currentTier = customerTier || price.tiers![0];
52
51
  }
53
52
  }
54
53
 
@@ -1,19 +1,28 @@
1
- import { BillingPeriod, Plan } from '@stigg/js-client-sdk';
1
+ import { BillingPeriod, Plan, Subscription, SubscriptionStatus } from '@stigg/js-client-sdk';
2
2
  import { countBy, uniq } from 'lodash';
3
3
 
4
4
  export function computeBillingPeriods(
5
5
  plans: Plan[],
6
+ activeSubscriptions?: Subscription[] | null,
6
7
  preferredBillingPeriod?: BillingPeriod,
7
8
  ): { defaultBillingPeriod: BillingPeriod; availableBillingPeriods: BillingPeriod[] } {
8
9
  const billingPeriods = plans.flatMap(x => x.pricePoints).map(x => x.billingPeriod);
9
10
  const counts = countBy(billingPeriods);
10
11
 
11
12
  const availableBillingPeriods = uniq(billingPeriods);
12
- let defaultBillingPeriod: BillingPeriod;
13
+ let defaultBillingPeriod: BillingPeriod | undefined;
13
14
 
14
15
  if (preferredBillingPeriod && availableBillingPeriods.includes(preferredBillingPeriod)) {
15
16
  defaultBillingPeriod = preferredBillingPeriod;
16
- } else {
17
+ } else if (activeSubscriptions) {
18
+ const activeSubscription = activeSubscriptions.find(x => x.status == SubscriptionStatus.Active);
19
+
20
+ if (activeSubscription && activeSubscription.prices.length > 0) {
21
+ defaultBillingPeriod = activeSubscription?.prices[0].billingPeriod;
22
+ }
23
+ }
24
+
25
+ if (!defaultBillingPeriod) {
17
26
  defaultBillingPeriod =
18
27
  (counts[BillingPeriod.Monthly] || 0) > (counts[BillingPeriod.Annually] || 0)
19
28
  ? BillingPeriod.Monthly
@@ -1,4 +1,4 @@
1
- import { BillingPeriod } from '@stigg/js-client-sdk';
1
+ import { BillingPeriod, PaywallCalculatedPricePoint, Price } from '@stigg/js-client-sdk';
2
2
  import isNil from 'lodash/isNil';
3
3
  import { PaywallPlan } from '../paywall';
4
4
 
@@ -11,20 +11,32 @@ function calculateDiscountRate(monthlyPrice?: number | null, annuallyPrice?: num
11
11
  return null;
12
12
  }
13
13
 
14
+ function getPlanBillingPeriodAmount(plan: PaywallPlan, billingPeriod: BillingPeriod) {
15
+ let pricePoint: PaywallCalculatedPricePoint | Price | undefined;
16
+
17
+ pricePoint = plan.paywallCalculatedPricePoints?.find(price => price.billingPeriod === billingPeriod);
18
+
19
+ if (!pricePoint) {
20
+ pricePoint = plan.pricePoints.find(price => price.billingPeriod === billingPeriod);
21
+ }
22
+
23
+ if (!pricePoint?.amount) {
24
+ const tieredPrice = plan.pricePoints.find(price => {
25
+ return price.isTieredPrice && price.billingPeriod === billingPeriod;
26
+ });
27
+
28
+ if (tieredPrice) {
29
+ return tieredPrice.tiers![0].unitPrice.amount;
30
+ }
31
+ }
32
+
33
+ return pricePoint?.amount;
34
+ }
35
+
14
36
  export function calculatePaywallDiscountRate(plans: PaywallPlan[]) {
15
37
  return plans.reduce((maxDiscount, plan) => {
16
- let monthlyAmount;
17
- let annuallyAmount;
18
-
19
- if (plan.paywallCalculatedPricePoints?.length) {
20
- monthlyAmount = plan.paywallCalculatedPricePoints?.find(price => price.billingPeriod === BillingPeriod.Monthly)
21
- ?.amount;
22
- annuallyAmount = plan.paywallCalculatedPricePoints?.find(price => price.billingPeriod === BillingPeriod.Annually)
23
- ?.amount;
24
- } else {
25
- monthlyAmount = plan.pricePoints.find(price => price.billingPeriod === BillingPeriod.Monthly)?.amount;
26
- annuallyAmount = plan.pricePoints.find(price => price.billingPeriod === BillingPeriod.Annually)?.amount;
27
- }
38
+ const monthlyAmount = getPlanBillingPeriodAmount(plan, BillingPeriod.Monthly);
39
+ const annuallyAmount = getPlanBillingPeriodAmount(plan, BillingPeriod.Annually);
28
40
 
29
41
  const discountRate = calculateDiscountRate(monthlyAmount, annuallyAmount);
30
42
  if (discountRate) {
@@ -40,6 +40,7 @@ const Template: ComponentStory<any> = args => (
40
40
  productId={args.productId}
41
41
  resourceId={args.resourceId}
42
42
  billingCountryCode={args.billingCountryCode}
43
+ preferredBillingPeriod={args.preferredBillingPeriod}
43
44
  />
44
45
  );
45
46