@stigg/react-sdk 4.4.0-beta.4 → 4.4.0-beta.5

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.4.0-beta.4",
2
+ "version": "4.4.0-beta.5",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -37,6 +37,11 @@ export const usePreviewSubscriptionAction = () => {
37
37
  let subscriptionPreview: SubscriptionPreview | null = null;
38
38
  let errorMessage: string | null = null;
39
39
 
40
+ const isValid = !subscription.billableFeatures.some(({ quantity }) => quantity === null);
41
+ if (!isValid) {
42
+ return { subscriptionPreview };
43
+ }
44
+
40
45
  try {
41
46
  if (customer?.id && plan?.id) {
42
47
  const previewSubscriptionProps: PreviewSubscription = {
@@ -92,7 +97,9 @@ export const usePreviewSubscription = () => {
92
97
  setIsFetchingSubscriptionPreview(true);
93
98
 
94
99
  const { subscriptionPreview } = await previewSubscriptionAction();
95
- setSubscriptionPreview(subscriptionPreview);
100
+ if (subscriptionPreview) {
101
+ setSubscriptionPreview(subscriptionPreview);
102
+ }
96
103
 
97
104
  setIsFetchingSubscriptionPreview(false);
98
105
  };
@@ -33,43 +33,36 @@ const StyledPlanCharge = styled.div`
33
33
 
34
34
  export function PlanCharge({
35
35
  charge,
36
+ isValid,
36
37
  setBillableFeature,
37
38
  billableFeature,
38
39
  onValidationChange,
39
40
  }: {
40
41
  charge: Price;
42
+ isValid: boolean;
41
43
  billableFeature?: BillableFeatureInput;
42
44
  setBillableFeature: UsePlanStepModel['setBillableFeature'];
43
45
  onValidationChange: ({ featureId, isValid }: { featureId: string; isValid: boolean }) => void;
44
46
  }) {
45
- const [isValid, setIsValid] = React.useState(true);
46
47
  const featureId = charge.feature?.featureId;
47
48
  const isBaseCharge = !featureId;
48
49
  const isPayAsYouGo = charge.pricingModel === BillingModel.UsageBased;
49
50
  const displayName = isBaseCharge ? 'Base charge' : charge.feature?.displayName;
50
51
  const hasQuantityRestrictions = !!(charge?.minUnitQuantity || charge?.maxUnitQuantity);
51
52
 
52
- useEffect(() => {
53
- if (!featureId) {
54
- return;
55
- }
56
-
57
- onValidationChange({ featureId, isValid });
58
- }, [featureId, isValid, onValidationChange]);
59
-
60
53
  const handleQuantityChange = (event: React.ChangeEvent<HTMLInputElement>) => {
61
54
  if (isBaseCharge || !featureId) return;
62
55
 
63
56
  const value = event?.target?.value ? Number(event?.target?.value) : charge.minUnitQuantity;
64
57
  if (!value || value <= 0) {
65
- setIsValid(false);
58
+ onValidationChange({ featureId, isValid: false });
66
59
  // Reset the input value to null
67
60
  // @ts-ignore
68
61
  setBillableFeature(featureId, null);
69
62
  return;
70
63
  }
71
64
 
72
- setIsValid(true);
65
+ onValidationChange({ featureId, isValid: true });
73
66
  const quantity = getValidPriceQuantity(charge, value);
74
67
  setBillableFeature(featureId, quantity);
75
68
  };
@@ -112,7 +105,7 @@ export function PlanCharge({
112
105
  error={!isValid}
113
106
  helperText={!isValid ? 'Not a valid value' : ''}
114
107
  FormHelperTextProps={{ sx: { margin: '4px' } }}
115
- value={billableFeature?.quantity || charge.minUnitQuantity}
108
+ value={billableFeature?.quantity || charge.minUnitQuantity || ''}
116
109
  onChange={handleQuantityChange}
117
110
  />
118
111
  );
@@ -142,7 +135,12 @@ export function CheckoutChargeList({ plan, billingPeriod }: CheckoutChargeListPr
142
135
  const { billableFeatures, setBillableFeature } = usePlanStepModel();
143
136
  const { setIsDisabled } = useProgressBarModel();
144
137
  const planCharges = useChargesSort(plan?.pricePoints?.filter((p) => p.billingPeriod === billingPeriod) || []);
145
- const [chargesValidation, setChargesValidation] = useState({});
138
+ const [chargesValidation, setChargesValidation] = useState(
139
+ planCharges?.reduce<Record<string, boolean>>((acc, curr) => {
140
+ acc[curr.feature?.featureId || 'base-charge'] = true;
141
+ return acc;
142
+ }, {}),
143
+ );
146
144
 
147
145
  useEffect(() => {
148
146
  const isDisabled = Object.values(chargesValidation).some((x) => !x);
@@ -159,6 +157,7 @@ export function CheckoutChargeList({ plan, billingPeriod }: CheckoutChargeListPr
159
157
  charge={charge}
160
158
  setBillableFeature={setBillableFeature}
161
159
  billableFeature={billableFeature}
160
+ isValid={chargesValidation[charge.feature?.featureId || 'base-charge']}
162
161
  onValidationChange={({ featureId, isValid }: { featureId: string; isValid: boolean }) =>
163
162
  setChargesValidation((prev) => ({ ...prev, [featureId]: isValid }))
164
163
  }