@stigg/react-sdk 4.12.1 → 4.13.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": "4.12.1",
2
+ "version": "4.13.0",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -4,7 +4,13 @@ import styled from '@emotion/styled/macro';
4
4
  import { PlanOffering } from './PlanOffering';
5
5
  import { BillingPeriodPicker } from './BillingPeriodPicker';
6
6
  import { calculatePaywallDiscountRate } from '../utils/calculateDiscountRate';
7
- import { ShouldHidePlanFn, OnPlanSelectedCallbackFn, PaywallPlan, SubscribeIntentionType } from './types';
7
+ import {
8
+ ShouldHidePlanFn,
9
+ OnPlanSelectedCallbackFn,
10
+ PaywallPlan,
11
+ SubscribeIntentionType,
12
+ SelectDefaultTierIndexFn,
13
+ } from './types';
8
14
  import { PaywallLocalization } from './paywallTextOverrides';
9
15
  import { PoweredByStigg } from '../common/PoweredByStigg';
10
16
  import { useStiggContext } from '../..';
@@ -52,6 +58,7 @@ type PaywallProps = {
52
58
  paywallLocale: PaywallLocalization;
53
59
  locale: string;
54
60
  shouldHidePlan?: ShouldHidePlanFn;
61
+ selectDefaultTierIndex?: SelectDefaultTierIndexFn;
55
62
  };
56
63
 
57
64
  export const Paywall = ({
@@ -67,6 +74,7 @@ export const Paywall = ({
67
74
  paywallLocale,
68
75
  locale,
69
76
  shouldHidePlan,
77
+ selectDefaultTierIndex,
70
78
  }: PaywallProps) => {
71
79
  const { stigg } = useStiggContext();
72
80
  const discountRate = calculatePaywallDiscountRate(plans);
@@ -158,6 +166,7 @@ export const Paywall = ({
158
166
  locale={locale}
159
167
  customer={customer}
160
168
  isCustomerInCustomPlan={isCustomerInCustomPlan}
169
+ selectDefaultTierIndex={selectDefaultTierIndex}
161
170
  />
162
171
  ))}
163
172
  </PaywallPlansContainer>
@@ -2,7 +2,7 @@ import React from 'react';
2
2
  import { BillingPeriod } from '@stigg/js-client-sdk';
3
3
  import { Paywall } from './Paywall';
4
4
  import { useLoadPaywallData } from './hooks/useLoadPaywallData';
5
- import { ShouldHidePlanFn, OnPlanSelectedCallbackFn } from './types';
5
+ import { ShouldHidePlanFn, OnPlanSelectedCallbackFn, SelectDefaultTierIndexFn } from './types';
6
6
  import { getResolvedPaywallLocalize, PaywallLocalization } from './paywallTextOverrides';
7
7
  import { DeepPartial } from '../../types';
8
8
  import { PaywallLoader } from './PaywallLoader';
@@ -24,6 +24,7 @@ export type PaywallContainerProps = {
24
24
  textOverrides?: DeepPartial<PaywallLocalization>;
25
25
  billingCountryCode?: string;
26
26
  shouldHidePlan?: ShouldHidePlanFn;
27
+ selectDefaultTierIndex?: SelectDefaultTierIndexFn;
27
28
  };
28
29
 
29
30
  export const PaywallContainer = ({
@@ -37,6 +38,7 @@ export const PaywallContainer = ({
37
38
  onBillingPeriodChange,
38
39
  billingCountryCode,
39
40
  shouldHidePlan,
41
+ selectDefaultTierIndex,
40
42
  }: PaywallContainerProps) => {
41
43
  const hasCustomerPortalContext = useCheckContextExists(CustomerPortalContext);
42
44
  let isCustomerPortalLoading = false;
@@ -89,6 +91,7 @@ export const PaywallContainer = ({
89
91
  paywallLocale={paywallLocale}
90
92
  locale={locale}
91
93
  shouldHidePlan={shouldHidePlan}
94
+ selectDefaultTierIndex={selectDefaultTierIndex}
92
95
  />
93
96
  );
94
97
 
@@ -12,7 +12,7 @@ import classNames from 'classnames';
12
12
  import Grid from '@mui/material/Grid';
13
13
  import { PlanEntitlements } from './PlanEntitlements';
14
14
  import { PlanOfferingButton } from './PlanOfferingButton';
15
- import { PaywallPlan, SubscribeIntentionType } from './types';
15
+ import { PaywallPlan, SelectDefaultTierIndexFn, SubscribeIntentionType } from './types';
16
16
  import { PaywallLocalization } from './paywallTextOverrides';
17
17
  import { flexLayoutMapper } from '../../theme/getResolvedTheme';
18
18
  import { Typography } from '../common/Typography';
@@ -92,6 +92,7 @@ type PlanOfferingProps = {
92
92
  locale: string;
93
93
  withStartingAtRow: boolean;
94
94
  isCustomerInCustomPlan: boolean;
95
+ selectDefaultTierIndex?: SelectDefaultTierIndexFn;
95
96
  };
96
97
 
97
98
  const NextPlanTagContainer = styled.div`
@@ -143,6 +144,7 @@ export function PlanOffering({
143
144
  locale,
144
145
  withStartingAtRow,
145
146
  isCustomerInCustomPlan,
147
+ selectDefaultTierIndex,
146
148
  }: PlanOfferingProps) {
147
149
  const isNextPlan = plan.isNextPlan && plan.isNextPlan(billingPeriod);
148
150
  const planPrices = plan.pricePoints.filter((pricePoint) => pricePoint.billingPeriod === billingPeriod);
@@ -166,7 +168,7 @@ export function PlanOffering({
166
168
  }
167
169
 
168
170
  const [selectedTierByFeature, setSelectedTierByFeature] = React.useState<Record<string, PriceTierFragment>>(
169
- getSelectedTier(plan, billingPeriod, currentSubscription, {}),
171
+ getSelectedTier(plan, billingPeriod, currentSubscription, {}, selectDefaultTierIndex),
170
172
  );
171
173
 
172
174
  useEffect(() => {
@@ -63,3 +63,5 @@ export type OnPlanSelectedCallbackFn = ({
63
63
  }) => void | Promise<void>;
64
64
 
65
65
  export type ShouldHidePlanFn = ({ plan }: { plan: PaywallPlan }) => boolean | Promise<boolean>;
66
+
67
+ export type SelectDefaultTierIndexFn = ({ plan }: { plan: PaywallPlan }) => number;
@@ -1,5 +1,6 @@
1
1
  import { BillingModel, TiersMode, BillingPeriod, Price, PriceTierFragment, Subscription } from '@stigg/js-client-sdk';
2
- import { PaywallPlan } from '../paywall/types';
2
+ import { PaywallPlan } from '../paywall';
3
+ import { SelectDefaultTierIndexFn } from '../paywall/types';
3
4
 
4
5
  export function getPriceFeatureUnit(price: Price) {
5
6
  if (!price.feature) {
@@ -49,6 +50,7 @@ export function getSelectedTier(
49
50
  billingPeriod: BillingPeriod,
50
51
  currentSubscription: Subscription | null,
51
52
  selectedTierByFeature: Record<string, PriceTierFragment>,
53
+ selectDefaultTierIndex?: SelectDefaultTierIndexFn,
52
54
  ): Record<string, PriceTierFragment> {
53
55
  const planTierPrices = plan.pricePoints.filter(
54
56
  (price) => price.billingPeriod === billingPeriod && price.isTieredPrice,
@@ -57,8 +59,8 @@ export function getSelectedTier(
57
59
  if (planTierPrices.length === 1) {
58
60
  const [price] = planTierPrices;
59
61
  const { featureId } = price.feature!;
60
-
61
- let currentTier = price.tiers![0];
62
+ const selectedDefaultTierIndex = selectDefaultTierIndex ? selectDefaultTierIndex({ plan }) : 0;
63
+ let currentTier = price.tiers![selectedDefaultTierIndex];
62
64
 
63
65
  if (selectedTierByFeature[featureId]) {
64
66
  currentTier = price.tiers?.find((tier) => tier.upTo === selectedTierByFeature[featureId].upTo) || currentTier;