From 21aa045d2a27d90f87e511036afc5d2ddd79eb56 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Wed, 3 Mar 2021 15:43:46 +0100 Subject: [PATCH] payments --- .../dashboard/src/pages/api/stubs/user.json | 11 ++++- packages/dashboard/src/pages/payments.js | 48 ++++++++++++------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/packages/dashboard/src/pages/api/stubs/user.json b/packages/dashboard/src/pages/api/stubs/user.json index 3b591617..f41dab0e 100644 --- a/packages/dashboard/src/pages/api/stubs/user.json +++ b/packages/dashboard/src/pages/api/stubs/user.json @@ -1,5 +1,12 @@ { - "sub": "ab776d6d-f324-4fa7-a728-7587d5215481", + "firstName": "John", + "lastName": "Doe", + "email": "john@example.com", + "sub": "ab776d6d-f324-4fa7-4k21-7587d5215481", "tier": 1, - "subscribedUntil": "0001-01-01T00:00:00Z" + "subscribedUntil": "0001-01-01T00:00:00Z", + "subscriptionStatus": "active", + "subscriptionCancelAt": "2021-04-21T00:00:00Z", + "subscriptionCancelAtPeriodEnd": true, + "stripeCustomerId": "cus_J0iYnAp6LRgsTI" } diff --git a/packages/dashboard/src/pages/payments.js b/packages/dashboard/src/pages/payments.js index df8c676b..bc6a70a6 100644 --- a/packages/dashboard/src/pages/payments.js +++ b/packages/dashboard/src/pages/payments.js @@ -1,3 +1,4 @@ +import dayjs from "dayjs"; import Layout from "../components/Layout"; import useSWR from "swr"; import { useEffect } from "react"; @@ -5,9 +6,17 @@ import ky from "ky/umd"; import { useState } from "react"; const plans = [ - { id: "initial_free", stripe: null, name: "Free", price: 0, description: "Unlimited bandwidth with throttled speed" }, + { + id: "initial_free", + tier: 1, + stripe: null, + name: "Free", + price: 0, + description: "Unlimited bandwidth with throttled speed", + }, { id: "initial_plus", + tier: 2, stripe: "price_1IO6FpIzjULiPWN6XHIG5mU9", name: "Skynet Plus", price: 5, @@ -15,6 +24,7 @@ const plans = [ }, { id: "initial_pro", + tier: 3, stripe: "price_1IO6FpIzjULiPWN6xYjmUuGb", name: "Skynet Pro", price: 20, @@ -22,6 +32,7 @@ const plans = [ }, { id: "initial_extreme", + tier: 4, stripe: "price_1IO6FpIzjULiPWN636iFN02j", name: "Skynet Extreme", price: 80, @@ -30,6 +41,7 @@ const plans = [ ]; const fetcher = (url) => fetch(url).then((r) => r.json()); +const apiPrefix = process.env.NODE_ENV === "development" ? "/api/stubs" : ""; const ActiveBadge = () => { return ( @@ -40,11 +52,9 @@ const ActiveBadge = () => { }; export default function Payments() { - const { data: user } = useSWR("/user", fetcher); - const { data: subscription } = useSWR("/api/stripe/subscription", fetcher); - const [selectedPlanId, setSelectedPlanId] = useState("initial_free"); - const [activePlanId, setActivePlanId] = useState(null); - const selectedPlan = plans.find(({ id }) => selectedPlanId === id); + const { data: user } = useSWR(`${apiPrefix}/user`, fetcher); + const [selectedPlan, setSelectedPlan] = useState(plans[0]); + const activePlan = plans.find(({ tier }) => user?.tier === tier); const handleSubscribe = async () => { try { const price = selectedPlan.stripe; @@ -56,12 +66,6 @@ export default function Payments() { } }; - useEffect(() => { - const plan = plans.find(({ stripe }) => subscription?.plan?.id === stripe); - - setActivePlanId(plan); - }, [subscription, setActivePlanId]); - return (
@@ -77,8 +81,17 @@ export default function Payments() {
Subscription status
-
{subscription?.status ?? "—"}
+
+ {user?.subscriptionStatus ?? "—"} +
+ {user?.subscriptionCancelAtPeriodEnd && ( +
+
+ Your plan will be cancelled on {dayjs(user.subscriptionCancelAt).format("D MMM YYYY")}. +
+
+ )}
{/*
@@ -112,6 +125,7 @@ export default function Payments() {
All paid up!
*/} +
asdas
@@ -149,11 +163,11 @@ export default function Payments() { type="radio" className="h-4 w-4 text-orange-500 cursor-pointer focus:ring-gray-900 border-gray-300" aria-describedby="plan-option-pricing-0 plan-option-limit-0" - checked={plan.id === selectedPlanId} - onChange={() => console.log(plan.id) || setSelectedPlanId(plan.id)} + checked={plan === selectedPlan} + onChange={() => console.log(plan.name) || setSelectedPlan(plan)} /> {plan.name} - {activePlanId === plan.id && } + {activePlan === plan && }

{/* On: "text-orange-900", Off: "text-gray-900" */} @@ -175,7 +189,7 @@ export default function Payments() {