Skip to content

Commit

Permalink
Compute max discount instead of using hardcoded value (#6488)
Browse files Browse the repository at this point in the history
Don't show in changelog
  • Loading branch information
AlexandreSi authored Mar 28, 2024
1 parent 7e8b44a commit 5637642
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
50 changes: 46 additions & 4 deletions newIDE/app/src/Profile/Subscription/SubscriptionDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,37 @@ const getSubscriptionPricingSystemPeriod = (
return subscriptionPricingSystem.period;
};

const getMaximumYearlyDiscountOverPlans = ({
subscriptionPlansWithPricingSystems,
}: {|
subscriptionPlansWithPricingSystems: ?Array<SubscriptionPlanWithPricingSystems>,
|}): number => {
if (!subscriptionPlansWithPricingSystems) return 0;
let maximumDiscount = 0;
subscriptionPlansWithPricingSystems.forEach(
subscriptionPlanWithPricingSystems => {
if (subscriptionPlanWithPricingSystems.isLegacy) return;
const { pricingSystems } = subscriptionPlanWithPricingSystems;
const monthlyPricingSystem = pricingSystems.find(
pricingSystem => pricingSystem.period === 'month'
);
const yearlyPricingSystem = pricingSystems.find(
pricingSystem => pricingSystem.period === 'year'
);
if (!monthlyPricingSystem || !yearlyPricingSystem) return;
const discount =
100 -
(yearlyPricingSystem.amountInCents /
(monthlyPricingSystem.amountInCents * 12)) *
100;
if (discount > maximumDiscount) {
maximumDiscount = discount;
}
}
);
return maximumDiscount;
};

const getPlanSpecificRequirements = (
i18n: I18nType,
subscriptionPlansWithPricingSystems: ?Array<SubscriptionPlanWithPricingSystems>
Expand Down Expand Up @@ -399,6 +430,9 @@ export default function SubscriptionDialog({
: displayedSubscriptionPlanWithPricingSystems.length < 5
? 'xl'
: false;
const maximumDiscount = getMaximumYearlyDiscountOverPlans({
subscriptionPlansWithPricingSystems,
});

return (
<I18n>
Expand Down Expand Up @@ -447,12 +481,17 @@ export default function SubscriptionDialog({
onChange={setPeriod}
/>
</Line>
{period !== 'year' && (
{period !== 'year' && maximumDiscount > 0 && (
<HotMessage
title={<Trans>Up to 40% discount</Trans>}
title={
<Trans>
Up to {maximumDiscount.toFixed(0)}% discount
</Trans>
}
message={
<Trans>
Get a yearly subscription and enjoy discounts up to 40%!
Get a yearly subscription and enjoy discounts up to
{maximumDiscount.toFixed(0)}%!
</Trans>
}
onClickRightButton={() => setPeriod('year')}
Expand Down Expand Up @@ -687,7 +726,10 @@ export default function SubscriptionDialog({
i18n,
displayedSubscriptionPlanWithPricingSystems
).map(planSpecificRequirements => (
<AlertMessage kind="info">
<AlertMessage
kind="info"
key={planSpecificRequirements.substring(0, 25)}
>
{planSpecificRequirements}
</AlertMessage>
))}
Expand Down
19 changes: 12 additions & 7 deletions newIDE/app/src/Profile/Subscription/SubscriptionPendingDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ export default function SubscriptionPendingDialog({
onClose,
authenticatedUser,
}: Props) {
const hasPlan =
!!authenticatedUser &&
!!authenticatedUser.subscription &&
!!authenticatedUser.subscription.planId;
const userPlanIdAtOpening = React.useRef<?string>(
!!authenticatedUser.subscription
? authenticatedUser.subscription.planId
: null
);
const userPlanId = !!authenticatedUser.subscription
? authenticatedUser.subscription.planId
: null;
const hasUserPlanChanged = userPlanId !== userPlanIdAtOpening.current;
const canUserBenefitFromDiscordRole =
!!authenticatedUser &&
canBenefitFromDiscordRole(authenticatedUser.subscription);
Expand All @@ -41,7 +46,7 @@ export default function SubscriptionPendingDialog({
// Ignore any error, will be retried anyway.
});
},
hasPlan ? null : 3900
hasUserPlanChanged ? null : 3900
);
const currentDiscordUsername =
!!authenticatedUser && !!authenticatedUser.profile
Expand Down Expand Up @@ -92,7 +97,7 @@ export default function SubscriptionPendingDialog({
<Dialog
title={<Trans>Confirming your subscription</Trans>}
actions={[
hasPlan ? (
hasUserPlanChanged ? (
<LeftLoader isLoading={isLoading} key="close">
<DialogPrimaryButton
label={<Trans>Done!</Trans>}
Expand All @@ -115,7 +120,7 @@ export default function SubscriptionPendingDialog({
maxWidth="sm"
open
>
{!hasPlan ? (
{!hasUserPlanChanged ? (
<Column noMargin>
<Line expand alignItems="center" justifyContent="center">
<Text>
Expand Down

0 comments on commit 5637642

Please sign in to comment.