From 23e674ca783e109db95a5ef6d621a05186c7362b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B5=D0=BD=D0=B8=D1=81=20=D0=9A=D0=B0=D1=82=D0=B0?= =?UTF-8?q?=D0=B5=D0=B2?= Date: Wed, 21 Feb 2024 21:15:03 +0000 Subject: [PATCH] Preview/discount pages --- .../MarketingTrialPayment/index.tsx | 24 +++++++++- src/components/pages/TrialChoice/index.tsx | 46 +++++++++++++------ .../components/PaymentModal/index.tsx | 14 ++++-- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/src/components/pages/EmailLetters/MarketingTrialPayment/index.tsx b/src/components/pages/EmailLetters/MarketingTrialPayment/index.tsx index 43113f9..fbc5f27 100644 --- a/src/components/pages/EmailLetters/MarketingTrialPayment/index.tsx +++ b/src/components/pages/EmailLetters/MarketingTrialPayment/index.tsx @@ -4,10 +4,30 @@ import ReservedTimer from "./components/ReservedTimer"; import MainButton from "@/components/MainButton"; import Modal from "@/components/Modal"; import PaymentModal from "../../TrialPayment/components/PaymentModal"; -import { useState } from "react"; +import { useEffect, useState } from "react"; +import { ISubscriptionPlan } from "@/api/resources/SubscriptionPlans"; +import { useApi } from "@/api"; +import { useTranslation } from "react-i18next"; function MarketingTrialPayment() { + const { i18n } = useTranslation(); + const locale = i18n.language; + const api = useApi(); const [isOpenPaymentModal, setIsOpenPaymentModal] = useState(false); + const [freeTrialPlan, setFreeTrialPlan] = useState< + ISubscriptionPlan | undefined + >(); + + // get free trial plan + useEffect(() => { + (async () => { + const { sub_plans } = await api.getSubscriptionPlans({ locale }); + const _freeTrialPlan = sub_plans.find( + (subPlan) => subPlan.trial?.is_free + ); + setFreeTrialPlan(_freeTrialPlan); + })(); + }, [api, locale]); const openStripeModal = () => { setIsOpenPaymentModal(true); @@ -24,7 +44,7 @@ function MarketingTrialPayment() { open={isOpenPaymentModal} onClose={handleCloseModal} > - +
diff --git a/src/components/pages/TrialChoice/index.tsx b/src/components/pages/TrialChoice/index.tsx index 8685ad0..8799ef0 100755 --- a/src/components/pages/TrialChoice/index.tsx +++ b/src/components/pages/TrialChoice/index.tsx @@ -11,6 +11,10 @@ import routes from "@/routes"; import EmailsList from "@/components/EmailsList"; import MainButton from "@/components/MainButton"; +interface IPlanKey { + [key: string]: number; +} + function TrialChoicePage() { const { i18n } = useTranslation(); const locale = i18n.language; @@ -25,20 +29,36 @@ function TrialChoicePage() { useEffect(() => { (async () => { const { sub_plans } = await api.getSubscriptionPlans({ locale }); - const plans = sub_plans - .filter((plan: ISubscriptionPlan) => plan.provider === "stripe") - .sort((a, b) => { - if (!a.trial || !b.trial) { - return 0; - } - if (a.trial?.price_cents < b.trial?.price_cents) { - return -1; - } - if (a.trial?.price_cents > b.trial?.price_cents) { - return 1; - } + const plansWithoutTest = sub_plans.filter( + (plan: ISubscriptionPlan) => !plan.name.includes("(test)") + ); + const plansKeys: IPlanKey = {}; + const plans: ISubscriptionPlan[] = []; + for (const plan of plansWithoutTest) { + plansKeys[plan.name] = plansKeys[plan.name] + ? plansKeys[plan.name] + 1 + : 1; + if (plansKeys[plan.name] > 1 && !plan.trial?.is_free) { + const targetPlan = plansWithoutTest.find( + (item) => item.name === plan.name && item.id.includes("stripe") + ); + plans.push(targetPlan as ISubscriptionPlan); + } + } + + plans.sort((a, b) => { + if (!a.trial || !b.trial) { return 0; - }); + } + if (a.trial?.price_cents < b.trial?.price_cents) { + return -1; + } + if (a.trial?.price_cents > b.trial?.price_cents) { + return 1; + } + return 0; + }); + setSubPlans(plans); })(); }, [api, locale]); diff --git a/src/components/pages/TrialPayment/components/PaymentModal/index.tsx b/src/components/pages/TrialPayment/components/PaymentModal/index.tsx index 7739d59..50c6f0f 100644 --- a/src/components/pages/TrialPayment/components/PaymentModal/index.tsx +++ b/src/components/pages/TrialPayment/components/PaymentModal/index.tsx @@ -20,13 +20,20 @@ import { getPriceFromTrial } from "@/services/price"; import SecurityPayments from "../SecurityPayments"; import PayPalButton from "./components/PayPalButton"; -function PaymentModal() { +interface IPaymentModalProps { + activeSubscriptionPlan?: ISubscriptionPlan; +} + +function PaymentModal({ activeSubscriptionPlan }: IPaymentModalProps) { const { i18n } = useTranslation(); const locale = i18n.language; const api = useApi(); const { token } = useAuth(); const navigate = useNavigate(); - const activeSubPlan = useSelector(selectors.selectActiveSubPlan); + const activeSubPlanFromStore = useSelector(selectors.selectActiveSubPlan); + const activeSubPlan = activeSubscriptionPlan + ? activeSubscriptionPlan + : activeSubPlanFromStore; const [payPalSubPlan, setPayPalSubPlan] = useState(); const [stripePromise, setStripePromise] = useState | null>(null); @@ -40,7 +47,7 @@ function PaymentModal() { const [isError, setIsError] = useState(false); const [selectedPaymentMethod, setSelectedPaymentMethod] = useState( - EPaymentMethod.CREDIT_CARD + EPaymentMethod.PAYPAL_OR_APPLE_PAY ); const onSelectPaymentMethod = (method: EPaymentMethod) => { @@ -125,6 +132,7 @@ function PaymentModal() { } setIsLoadingPayPal(false); window.location.href = link.href; + // window.open(link.href, '_blank'); }; if (isLoading) {