A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
mobile: fix unable to switch between yearly/monthly plans
Ammar Ahmed1696ec5c7213
diff --git a/apps/mobile/app/components/paywall/index.tsx b/apps/mobile/app/components/paywall/index.tsxindex e1da6a404..d0d1ecdf4 100644--- a/apps/mobile/app/components/paywall/index.tsx+++ b/apps/mobile/app/components/paywall/index.tsx@@ -18,13 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.*/import { getFeaturesTable } from "@notesnook/common";-import {- EVENTS,- Plan,- SKUResponse,- SubscriptionPlan,- User-} from "@notesnook/core";+import { EVENTS, Plan, SubscriptionPlan, User } from "@notesnook/core";import { strings } from "@notesnook/intl";import { useThemeColors } from "@notesnook/theme";import React, { useEffect, useState } from "react";@@ -54,6 +48,7 @@ import {TECHLORE_SVG,XDA_SVG} from "../../assets/images/assets";+import { db } from "../../common/database";import { useNavigationFocus } from "../../hooks/use-navigation-focus";import usePricingPlans, {PlanOverView,@@ -75,7 +70,6 @@ import { IconButton } from "../ui/icon-button";import { SvgView } from "../ui/svg";import Heading from "../ui/typography/heading";import Paragraph from "../ui/typography/paragraph";-import { db } from "../../common/database";const Steps = {select: 1,@@ -935,7 +929,10 @@ const PricingPlanCard = ({setStep: (step: number) => void;}) => {const { colors } = useThemeColors();- const [regionalDiscount, setRegionaDiscount] = useState<SKUResponse>();+ const regionalDiscount =+ annualBilling && plan.id === "pro"+ ? pricingPlans?.regionalDiscount+ : undefined;const { width } = useWindowDimensions();const isTablet = width > 600;@@ -958,26 +955,6 @@ const PricingPlanCard = ({annualBilling);- useEffect(() => {- if (pricingPlans?.isGithubRelease || !annualBilling) return;- pricingPlans- ?.getRegionalDiscount(- plan.id,- pricingPlans.isGithubRelease- ? (WebPlan?.period as string)- : `notesnook.${plan.id}.${annualBilling ? "yearly" : "monthly"}`- )- .then((value) => {- setRegionaDiscount(value);- });- }, [WebPlan?.period, annualBilling, plan.id, pricingPlans]);-- useEffect(() => {- if (!annualBilling) {- setRegionaDiscount(undefined);- }- }, [annualBilling]);-const isSubscribed =product?.productId &&pricingPlans?.user?.subscription?.productId?.includes(plan.id) &&diff --git a/apps/mobile/app/components/sheets/buy-plan/index.tsx b/apps/mobile/app/components/sheets/buy-plan/index.tsxindex d8c2b1823..50eed6e07 100644--- a/apps/mobile/app/components/sheets/buy-plan/index.tsx+++ b/apps/mobile/app/components/sheets/buy-plan/index.tsx@@ -16,11 +16,11 @@ GNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/-import { Plan, SKUResponse } from "@notesnook/core";+import { Plan } from "@notesnook/core";import { strings } from "@notesnook/intl";import { useThemeColors } from "@notesnook/theme";import dayjs from "dayjs";-import React, { useEffect, useState } from "react";+import React, { useState } from "react";import {Linking,Platform,@@ -330,7 +330,10 @@ const ProductItem = (props: {productId: string;}) => {const { colors } = useThemeColors();- const [regionalDiscount, setRegionaDiscount] = useState<SKUResponse>();+ const regionalDiscount =+ props.productId === "notesnook.pro.yearly"+ ? props.pricingPlans.regionalDiscount+ : undefined;const product =props.pricingPlans?.currentPlan?.subscriptions?.[regionalDiscount?.sku || props.productId@@ -367,30 +370,6 @@ const ProductItem = (props: {props.pricingPlans.user?.subscription?.productId ===(product as Plan)?.id);- useEffect(() => {- if (product) {- props.pricingPlans- ?.getRegionalDiscount(- props.pricingPlans.currentPlan?.id as string,- props.pricingPlans.isGithubRelease- ? ((product as Plan)?.period as string)- : props.productId- )- .then((value) => {- if (- value &&- value.sku?.startsWith(- (props.pricingPlans.selectedProduct as RNIap.Subscription)- ?.productId- )- ) {- props.pricingPlans.selectProduct(value?.sku as string);- }- setRegionaDiscount(value);- });- }- }, [product, props.pricingPlans, props.productId]);-const discountValue =(isAnnual && !isGithubRelease) ||(isGithubRelease && (product as Plan)?.discount?.amount)diff --git a/apps/mobile/app/hooks/use-pricing-plans.ts b/apps/mobile/app/hooks/use-pricing-plans.tsindex b52fe8e4b..f0b9b749d 100644--- a/apps/mobile/app/hooks/use-pricing-plans.ts+++ b/apps/mobile/app/hooks/use-pricing-plans.ts@@ -16,16 +16,17 @@ GNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/-import { Plan, SubscriptionPlan, SubscriptionPlanId } from "@notesnook/core";-import { useEffect, useState } from "react";+import { Plan, SubscriptionPlan } from "@notesnook/core";+import React, { useEffect, useState } from "react";+import { useAsync } from "react-async-hook";import { Platform } from "react-native";import Config from "react-native-config";import * as RNIap from "react-native-iap";import { DatabaseLogger, db } from "../common/database";import PremiumService from "../services/premium";+import SettingsService from "../services/settings";import { useSettingStore } from "../stores/use-setting-store";import { useUserStore } from "../stores/use-user-store";-import SettingsService from "../services/settings";function numberWithCommas(x: string) {const parts = x.toString().split(".");parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");@@ -155,9 +156,18 @@ const planIdToIndex = (planId: string) => {return planIndex;};let WebPlanCache: Plan[];+const isGithubRelease = Config.GITHUB_RELEASE === "true";const usePricingPlans = (options?: PricingPlansOptions) => {- const isGithubRelease = Config.GITHUB_RELEASE === "true";const user = useUserStore((state) => state.user);+ const regionalDiscount = useAsync(+ () =>+ db.pricing.sku(+ Platform.OS === "android" ? "google" : "apple",+ "yearly",+ "pro"+ ),+ []+ );const [currentPlan, setCurrentPlan] = useState<string>(options?.planId || pricingPlans[2].id);@@ -172,17 +182,20 @@ const usePricingPlans = (options?: PricingPlansOptions) => {const [userCanRequestTrial, setUserCanRequestTrial] = useState(false);const [webPricingPlans, setWebPricingPlans] = useState<Plan[]>([]);- const getProduct = (planId: string, skuId: string) => {- if (isGithubRelease)- return webPricingPlans.find(- (plan) => planIdToIndex(planId) === plan.plan && skuId === plan.period- );+ const getProduct = React.useCallback(+ (planId: string, skuId: string) => {+ if (isGithubRelease)+ return webPricingPlans.find(+ (plan) => planIdToIndex(planId) === plan.plan && skuId === plan.period+ );- return (- plans.find((p) => p.id === planId)?.subscriptions?.[skuId] ||- plans.find((p) => p.id === planId)?.products?.[skuId]- );- };+ return (+ plans.find((p) => p.id === planId)?.subscriptions?.[skuId] ||+ plans.find((p) => p.id === planId)?.products?.[skuId]+ );+ },+ [plans, webPricingPlans]+ );const getProductAndroid = (planId: string, skuId: string) => {if (isGithubRelease)@@ -196,37 +209,45 @@ const usePricingPlans = (options?: PricingPlansOptions) => {return getProduct(planId, skuId) as RNIap.SubscriptionIOS;};- const hasTrialOffer = (planId?: string, productId?: string) => {- if (!selectedProductSku && !productId) return false;+ const hasTrialOffer = React.useCallback(+ (planId?: string, productId?: string) => {+ if (!selectedProductSku && !productId) return false;- if (productId?.includes("5year")) return false;- if (isGithubRelease) {- if (- user?.subscription?.trialsAvailed?.some(- (plan) => plan === planIdToIndex(planId || currentPlan)- )- ) {- return false;- } else {- return true;+ if (productId?.includes("5year")) return false;+ if (isGithubRelease) {+ if (+ user?.subscription?.trialsAvailed?.some(+ (plan) => plan === planIdToIndex(planId || currentPlan)+ )+ ) {+ return false;+ } else {+ return true;+ }}- }- return Platform.OS === "ios"- ? (- getProduct(- planId || currentPlan,- productId || selectedProductSku- ) as RNIap.SubscriptionIOS- )?.introductoryPricePaymentModeIOS === "FREETRIAL"- : (- getProduct(- planId || currentPlan,- productId || selectedProductSku- ) as RNIap.SubscriptionAndroid- )?.subscriptionOfferDetails?.[0]?.pricingPhases?.pricingPhaseList- ?.length > 1;- };+ return Platform.OS === "ios"+ ? (+ getProduct(+ planId || currentPlan,+ productId || selectedProductSku+ ) as RNIap.SubscriptionIOS+ )?.introductoryPricePaymentModeIOS === "FREETRIAL"+ : (+ getProduct(+ planId || currentPlan,+ productId || selectedProductSku+ ) as RNIap.SubscriptionAndroid+ )?.subscriptionOfferDetails?.[0]?.pricingPhases?.pricingPhaseList+ ?.length > 1;+ },+ [+ currentPlan,+ getProduct,+ selectedProductSku,+ user?.subscription?.trialsAvailed+ ]+ );// user && (!user.subscription || !user.subscription.expiry) ? true : false;@@ -649,21 +670,6 @@ const usePricingPlans = (options?: PricingPlansOptions) => {);};- async function getRegionalDiscount(plan: string, productId: string) {- if (productId !== "notesnook.pro.yearly") {- return;- }- try {- return await db.pricing.sku(- Platform.OS === "android" ? "google" : "apple",- "yearly",- plan as SubscriptionPlanId- );- } catch (e) {- console.log(e);- }- }-function isSubscribedToPlan(planId: string) {if (!PremiumService.get()) return false;return user?.subscription?.productId?.includes(planId);@@ -734,7 +740,7 @@ const usePricingPlans = (options?: PricingPlansOptions) => {(plan) => plan.plan === planIndex && plan.period === period);},- getRegionalDiscount,+ regionalDiscount: regionalDiscount.result,isGithubRelease: isGithubRelease,isSubscribed: () => user?.subscription?.plan !== SubscriptionPlan.FREE,finish: () => options?.onBuy?.()