From 6ced65fb50dcfd12b21d01abde67f454dba0d2ee Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Wed, 17 Mar 2021 04:08:07 +0530 Subject: [PATCH] updated sidebar and plan selector to manage plans --- src/components/Sidebar.tsx | 29 ++++------ src/pages/gallery/components/PlanSelector.tsx | 53 ++++++++++++++++--- src/pages/gallery/index.tsx | 1 + src/utils/strings/englishConstants.tsx | 1 + 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index 4a000eb54..6003f1e4f 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -83,25 +83,16 @@ export default function Sidebar(props: Props) {

{constants.SUBSCRIPTION_EXPIRED}

)} - {subscriptionService.hasActivePaidPlan() ? ( - - ) : ( - - )} + +
diff --git a/src/pages/gallery/components/PlanSelector.tsx b/src/pages/gallery/components/PlanSelector.tsx index cebe8938b..8b3910142 100644 --- a/src/pages/gallery/components/PlanSelector.tsx +++ b/src/pages/gallery/components/PlanSelector.tsx @@ -2,15 +2,20 @@ import React, { useState } from 'react'; import { Modal, Spinner } from 'react-bootstrap'; import constants from 'utils/strings/constants'; import styled from 'styled-components'; -import subscriptionService, { Plan } from 'services/subscriptionService'; +import subscriptionService, { + Plan, + Subscription, +} from 'services/subscriptionService'; +import { getData, LS_KEYS } from 'utils/storage/localStorage'; -export const PlanIcon = styled.div` +export const PlanIcon = styled.div<{ selected: boolean }>` height: 192px; width: 250px; border: 1px solid #404040; text-align: center; font-size: 20px; - cursor: pointer; + cursor: ${(props) => (props.selected ? 'not-allowed' : 'pointer')}; + background: ${(props) => props.selected && '#404040'}; `; const LoaderOverlay = styled.div` @@ -28,16 +33,35 @@ interface Props { plans: Plan[]; modalView: boolean; closeModal: any; + setBannerErrorCode; } function PlanSelector(props: Props) { const [loading, setLoading] = useState(false); + const subscription: Subscription = getData(LS_KEYS.SUBSCRIPTION); + const selectPlan = async (plan) => { + try { + setLoading(true); + if (subscriptionService.hasActivePaidPlan()) { + if (plan.androidID === subscription.productID) { + return; + } + await subscriptionService.updateSubscription(plan.stripeID); + } else { + await subscriptionService.buySubscription(plan.stripeID); + } + } catch (err) { + props.setBannerErrorCode(err.message); + setLoading(false); + props.closeModal(); + } + }; const PlanIcons: JSX.Element[] = props.plans?.map((plan) => ( { - setLoading(true); - subscriptionService.buySubscription(plan.stripeID); + selectPlan(plan); }} + selected={plan.androidID === subscription.productID} > {`${plan.price} / ${constants.MONTH}`}
+ {plan.androidID === subscription.productID && ( +
+ current plan +
+ )} )); return ( @@ -74,7 +110,9 @@ function PlanSelector(props: Props) { > - {constants.CHOOSE_PLAN} + {subscriptionService.hasActivePaidPlan() + ? constants.MANAGE_PLAN + : constants.CHOOSE_PLAN} {PlanIcons} - {loading && ( + {(!props.plans || loading) && ( diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index beff6f312..42d18c995 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -370,6 +370,7 @@ export default function Gallery(props: Props) { plans={plans} modalView={props.planModalView} closeModal={() => props.setPlanModalView(false)} + setBannerErrorCode={setBannerErrorCode} />