Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store monthly/yearly state higher in the arch tree and remember it #1915

Merged
merged 2 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useState } from "react"
import React, { useCallback, useEffect, useMemo, useState } from "react"
import { makeStyles, createStyles, useThemeSwitcher } from "@chainsafe/common-theme"
import { CSFTheme } from "../../../../../Themes/types"
import { Modal } from "@chainsafe/common-components"
Expand Down Expand Up @@ -59,6 +59,15 @@ const ChangeProductModal = ({ onClose }: IChangeProductModal) => {
const [isLoadingChangeSubscription, setIsLoadingChangeSubscription] = useState(false)
const [isSubscriptionError, setIsSubscriptionError] = useState(false)
const didSelectFreePlan = useMemo(() => !!selectedPlan && getPrice(selectedPlan, "month") === 0, [selectedPlan])
const monthlyPrice = useMemo(() => selectedPlan?.prices.find((price) => price.recurring.interval === "month"), [selectedPlan])
const yearlyPrice = useMemo(() => selectedPlan?.prices.find((price) => price.recurring.interval === "year"), [selectedPlan])
const [billingPeriod, setBillingPeriod] = useState<ProductPriceRecurringInterval | undefined>()

useEffect(() => {
if(selectedPlan && !billingPeriod){
setBillingPeriod(monthlyPrice ? "month" : "year")
}
}, [billingPeriod, monthlyPrice, selectedPlan])

useEffect(() => {
if(!slide){
Expand All @@ -77,7 +86,7 @@ const ChangeProductModal = ({ onClose }: IChangeProductModal) => {
}
}, [getAvailablePlans, plans])

const handleChangeSubscription = () => {
const handleChangeSubscription = useCallback(() => {
if (selectedPrice) {
setIsLoadingChangeSubscription(true)
changeSubscription(selectedPrice.id)
Expand All @@ -89,7 +98,28 @@ const ChangeProductModal = ({ onClose }: IChangeProductModal) => {
})
.finally(() => setIsLoadingChangeSubscription(false))
}
}
}, [changeSubscription, selectedPrice])

const onSelectPlanPrice = useCallback(() => {
if(billingPeriod === "month" && monthlyPrice) {
setSelectedPrice(monthlyPrice)
} else if (yearlyPrice) {
setSelectedPrice(yearlyPrice)
}
setSlide("paymentMethod")
}, [billingPeriod, monthlyPrice, yearlyPrice])

const onSelectPlan = useCallback((plan: Product) => {
setSelectedPlan(plan)
const currentPrice = currentSubscription?.product?.price?.unit_amount
const currentRecurrence = currentSubscription?.product.price.recurring.interval
const newPrice = getPrice(plan, currentRecurrence)
const isDowngrade = (currentPrice || 0) > newPrice

isDowngrade
? setSlide("downgradeDetails")
: setSlide("planDetails")
}, [currentSubscription])

return (
<Modal
Expand All @@ -106,17 +136,7 @@ const ChangeProductModal = ({ onClose }: IChangeProductModal) => {
>
{slide === "select" && (
<SelectPlan
onSelectPlan={(plan: Product) => {
setSelectedPlan(plan)
const currentPrice = currentSubscription?.product?.price?.unit_amount
const currentRecurrence = currentSubscription?.product.price.recurring.interval
const newPrice = getPrice(plan, currentRecurrence)
const isDowngrade = (currentPrice || 0) > newPrice

isDowngrade
? setSlide("downgradeDetails")
: setSlide("planDetails")
}}
onSelectPlan={onSelectPlan}
plans={plans}
/>
)}
Expand All @@ -129,14 +149,18 @@ const ChangeProductModal = ({ onClose }: IChangeProductModal) => {
onClose={onClose}
/>
)}
{slide === "planDetails" && selectedPlan && (
{slide === "planDetails" && selectedPlan && billingPeriod && (
<PlanDetails
plan={selectedPlan}
goToSelectPlan={() => setSlide("select")}
onSelectPlanPrice={(planPrice: ProductPrice) => {
setSelectedPrice(planPrice)
setSlide("paymentMethod")
goToSelectPlan={() => {
setBillingPeriod(undefined)
setSlide("select")
}}
onSelectPlanPrice={onSelectPlanPrice}
onChangeBillingPeriod={setBillingPeriod}
billingPeriod={billingPeriod}
monthlyPrice={monthlyPrice}
yearlyPrice={yearlyPrice}
/>
)}
{slide === "paymentMethod" && selectedPrice &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from "react"
import React, { useMemo } from "react"
import { makeStyles, createStyles } from "@chainsafe/common-theme"
import { CSFTheme } from "../../../../../Themes/types"
import { Product, ProductPrice } from "@chainsafe/files-api-client"
import { Product, ProductPrice, ProductPriceRecurringInterval } from "@chainsafe/files-api-client"
import { Button, Divider, formatBytes, ToggleSwitch, Typography } from "@chainsafe/common-components"
import { t, Trans } from "@lingui/macro"
import dayjs from "dayjs"
Expand Down Expand Up @@ -67,28 +67,27 @@ const useStyles = makeStyles(({ constants }: CSFTheme) =>
interface IPlanDetails {
plan: Product
goToSelectPlan: () => void
onSelectPlanPrice: (planPrice: ProductPrice) => void
onSelectPlanPrice: () => void
billingPeriod: ProductPriceRecurringInterval
onChangeBillingPeriod: (paymentPeriod: ProductPriceRecurringInterval) => void
monthlyPrice?: ProductPrice
yearlyPrice?: ProductPrice
}

const PlanDetails = ({ plan, goToSelectPlan, onSelectPlanPrice }: IPlanDetails) => {
const PlanDetails = ({
plan,
goToSelectPlan,
onSelectPlanPrice,
billingPeriod,
onChangeBillingPeriod,
monthlyPrice,
yearlyPrice
}: IPlanDetails) => {
const classes = useStyles()
const monthlyPrice = plan.prices.find((price) => price.recurring.interval === "month")
const yearlyPrice = plan.prices.find((price) => price.recurring.interval === "year")
const currentPlanStorage = formatBytes(Number(monthlyPrice?.metadata?.storage_size_bytes), 2)

const [billingPeriod, setBillingPeriod] = useState<"monthly" | "yearly">(monthlyPrice ? "monthly" : "yearly")

const handleSelectPlan = () => {
if(billingPeriod === "monthly" && monthlyPrice) {
onSelectPlanPrice(monthlyPrice)
} else if (yearlyPrice) {
onSelectPlanPrice(yearlyPrice)
}
}

const percentageOff = monthlyPrice && yearlyPrice
? ((((monthlyPrice.unit_amount * 12) - yearlyPrice.unit_amount) * 100) / (monthlyPrice.unit_amount * 12))
: null
const currentPlanStorage = useMemo(() => formatBytes(Number(monthlyPrice?.metadata?.storage_size_bytes), 2), [monthlyPrice])
const percentageOff = useMemo(() => monthlyPrice && yearlyPrice &&
(((monthlyPrice.unit_amount * 12) - yearlyPrice.unit_amount) * 100) / (monthlyPrice.unit_amount * 12)
, [monthlyPrice, yearlyPrice])

return (
<article className={classes.root}>
Expand Down Expand Up @@ -165,10 +164,11 @@ const PlanDetails = ({ plan, goToSelectPlan, onSelectPlanPrice }: IPlanDetails)
</Typography>
<div className={classes.pushRightBox}>
<ToggleSwitch
left={{ value: "monthly" }}
right={{ value: "yearly" }}
left={{ value: "month" }}
right={{ value: "year" }}
testId="duration"
onChange={() => setBillingPeriod(billingPeriod === "monthly" ? "yearly" : "monthly")}
onChange={onChangeBillingPeriod}
value={billingPeriod}
/>
</div>
</div>
Expand All @@ -190,11 +190,11 @@ const PlanDetails = ({ plan, goToSelectPlan, onSelectPlanPrice }: IPlanDetails)
className={classes.boldText}
data-cy="label-total-cost"
>
{billingPeriod === "monthly"
{billingPeriod === "month"
? `${monthlyPrice?.unit_amount ? monthlyPrice?.currency : ""} ${monthlyPrice?.unit_amount}`
: `${yearlyPrice?.unit_amount ? yearlyPrice?.currency : ""} ${yearlyPrice?.unit_amount}`
}
<span className={classes.normalWeightText}>{billingPeriod === "monthly" ? t`/month` : t`/year`}</span>
<span className={classes.normalWeightText}>{billingPeriod === "month" ? t`/month` : t`/year`}</span>
</Typography>
</div>
</div>
Expand All @@ -209,7 +209,7 @@ const PlanDetails = ({ plan, goToSelectPlan, onSelectPlanPrice }: IPlanDetails)
</Button>
<Button
variant="primary"
onClick={handleSelectPlan}
onClick={onSelectPlanPrice}
testId="select-this-plan"
>
<Trans>Select this plan</Trans>
Expand Down