From 3b77d79997dd23e537f319507c438c05a6711311 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Thu, 27 Apr 2023 10:04:26 +0500 Subject: [PATCH] web: prevent pro users from resubscribing --- .../dialogs/buy-dialog/buy-dialog.tsx | 53 ++++++++++++++++++- apps/web/src/hooks/use-is-user-premium.ts | 10 ++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/dialogs/buy-dialog/buy-dialog.tsx b/apps/web/src/components/dialogs/buy-dialog/buy-dialog.tsx index baf7b7e56..d64998f71 100644 --- a/apps/web/src/components/dialogs/buy-dialog/buy-dialog.tsx +++ b/apps/web/src/components/dialogs/buy-dialog/buy-dialog.tsx @@ -41,6 +41,8 @@ import { useCheckoutStore } from "./store"; import { getCurrencySymbol } from "./helpers"; import { Theme } from "@notesnook/theme"; import { isMacStoreApp } from "../../../utils/platform"; +import { isUserSubscribed } from "../../../hooks/use-is-user-premium"; +import { SUBSCRIPTION_STATUS } from "../../../common/constants"; type BuyDialogProps = { couponCode?: string; @@ -156,7 +158,7 @@ type SideBarProps = { }; function SideBar(props: SideBarProps) { const { initialPlan, onClose } = props; - const [showPlans, setShowPlans] = useState(!!initialPlan); + const [showPlans, setShowPlans] = useState(false); const onPlanSelected = useCheckoutStore((state) => state.selectPlan); const selectedPlan = useCheckoutStore((state) => state.selectedPlan); const pricingInfo = useCheckoutStore((state) => state.pricingInfo); @@ -176,7 +178,13 @@ function SideBar(props: SideBarProps) { /> ); - if (user && showPlans) + if (user && !showPlans && isUserSubscribed(user)) { + return ( + setShowPlans(true)} /> + ); + } + + if (user && (showPlans || !!initialPlan)) return ( { @@ -347,6 +355,47 @@ function TrialOrUpgrade(props: TrialOrUpgradeProps) { ); } +type AlreadyPremiumProps = { + user: User | undefined; + onShowPlans: () => void; +}; +function AlreadyPremium(props: AlreadyPremiumProps) { + const { user, onShowPlans } = props; + + const isCanceled = + user?.subscription?.type === SUBSCRIPTION_STATUS.PREMIUM_CANCELED; + + return ( + <> + + + Notesnook Pro + + {isCanceled ? ( + <> + + Resubscribing to Notesnook Pro will replace your existing + subscription. + + + + ) : ( + + You are already subscribed to Notesnook Pro. + + )} + + ); +} + type SelectedPlanProps = { plan: Plan; pricingInfo: PricingInfo | undefined; diff --git a/apps/web/src/hooks/use-is-user-premium.ts b/apps/web/src/hooks/use-is-user-premium.ts index d31c16eee..c32323e8b 100644 --- a/apps/web/src/hooks/use-is-user-premium.ts +++ b/apps/web/src/hooks/use-is-user-premium.ts @@ -41,3 +41,13 @@ export function isUserPremium(user?: User) { subStatus === SUBSCRIPTION_STATUS.TRIAL ); } + +export function isUserSubscribed(user?: User) { + if (!user) user = userstore.get().user; + + const subStatus = user?.subscription?.type; + return ( + subStatus === SUBSCRIPTION_STATUS.PREMIUM || + subStatus === SUBSCRIPTION_STATUS.PREMIUM_CANCELED + ); +}