refactor(dashboard-v2): make CurrentPlan component use PlansProvider
This commit is contained in:
parent
8eacf13806
commit
f1262fb8f7
|
@ -2,7 +2,7 @@ import dayjs from "dayjs";
|
||||||
import relativeTime from "dayjs/plugin/relativeTime";
|
import relativeTime from "dayjs/plugin/relativeTime";
|
||||||
|
|
||||||
import { useUser } from "../../contexts/user";
|
import { useUser } from "../../contexts/user";
|
||||||
import useSubscriptionPlans from "../../hooks/useSubscriptionPlans";
|
import useActivePlan from "../../hooks/useActivePlan";
|
||||||
|
|
||||||
import LatestPayment from "./LatestPayment";
|
import LatestPayment from "./LatestPayment";
|
||||||
import SuggestedPlan from "./SuggestedPlan";
|
import SuggestedPlan from "./SuggestedPlan";
|
||||||
|
@ -11,7 +11,7 @@ dayjs.extend(relativeTime);
|
||||||
|
|
||||||
const CurrentPlan = () => {
|
const CurrentPlan = () => {
|
||||||
const { user, error: userError } = useUser();
|
const { user, error: userError } = useUser();
|
||||||
const { activePlan, plans, error: plansError } = useSubscriptionPlans(user);
|
const { plans, activePlan, error: plansError } = useActivePlan(user);
|
||||||
|
|
||||||
if (!user || !activePlan) {
|
if (!user || !activePlan) {
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
|
import freeTier from "../lib/tiers";
|
||||||
|
import { usePlans } from "../contexts/plans";
|
||||||
|
|
||||||
|
export default function useActivePlan(user) {
|
||||||
|
const { plans, error } = usePlans();
|
||||||
|
|
||||||
|
const [activePlan, setActivePlan] = useState(freeTier);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (user) {
|
||||||
|
setActivePlan(plans.find((plan) => plan.tier === user.tier));
|
||||||
|
}
|
||||||
|
}, [plans, user]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
error,
|
||||||
|
plans,
|
||||||
|
activePlan,
|
||||||
|
};
|
||||||
|
}
|
|
@ -1,28 +0,0 @@
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import useSWR from "swr";
|
|
||||||
import freeTier from "../lib/tiers";
|
|
||||||
|
|
||||||
export default function useSubscriptionPlans(user) {
|
|
||||||
const { data: paidPlans, error, mutate } = useSWR("stripe/prices");
|
|
||||||
const [plans, setPlans] = useState([freeTier]);
|
|
||||||
const [activePlan, setActivePlan] = useState(freeTier);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (paidPlans) {
|
|
||||||
setPlans((plans) => [...plans, ...paidPlans].sort((planA, planB) => planA.tier - planB.tier));
|
|
||||||
}
|
|
||||||
}, [paidPlans]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (user) {
|
|
||||||
setActivePlan(plans.find((plan) => plan.tier === user.tier));
|
|
||||||
}
|
|
||||||
}, [plans, user]);
|
|
||||||
|
|
||||||
return {
|
|
||||||
error,
|
|
||||||
mutate,
|
|
||||||
plans,
|
|
||||||
activePlan,
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -2,6 +2,7 @@ import * as React from "react";
|
||||||
import { useMedia } from "react-use";
|
import { useMedia } from "react-use";
|
||||||
|
|
||||||
import theme from "../lib/theme";
|
import theme from "../lib/theme";
|
||||||
|
import { PlansProvider } from "../contexts/plans/PlansProvider";
|
||||||
import { ArrowRightIcon } from "../components/Icons";
|
import { ArrowRightIcon } from "../components/Icons";
|
||||||
import { Panel } from "../components/Panel";
|
import { Panel } from "../components/Panel";
|
||||||
import { Tab, TabPanel, Tabs } from "../components/Tabs";
|
import { Tab, TabPanel, Tabs } from "../components/Tabs";
|
||||||
|
@ -16,7 +17,7 @@ const IndexPage = () => {
|
||||||
const showRecentActivity = useMedia(`(min-width: ${theme.screens.md})`);
|
const showRecentActivity = useMedia(`(min-width: ${theme.screens.md})`);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<PlansProvider>
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<Slider
|
<Slider
|
||||||
slides={[
|
slides={[
|
||||||
|
@ -60,7 +61,7 @@ const IndexPage = () => {
|
||||||
<LatestActivity />
|
<LatestActivity />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</>
|
</PlansProvider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Reference in New Issue