From ac8f6e2808aae0fe2cb5e94c59a84e4751f16ded Mon Sep 17 00:00:00 2001 From: Karrix Date: Sun, 21 Jan 2024 15:35:14 +0800 Subject: [PATCH] update: pricing plan with usage base --- .../pricing/components/pricePlanList.tsx | 82 +++++++++++++++++-- web/src/app/(app)/pricing/const/Icon.tsx | 22 +++++ web/src/components/Navbar.tsx | 7 ++ web/src/db/schema.ts | 15 ++++ web/src/server/linkToPricing.ts | 33 ++++++++ 5 files changed, 152 insertions(+), 7 deletions(-) diff --git a/web/src/app/(app)/pricing/components/pricePlanList.tsx b/web/src/app/(app)/pricing/components/pricePlanList.tsx index 19d30c9..d097741 100644 --- a/web/src/app/(app)/pricing/components/pricePlanList.tsx +++ b/web/src/app/(app)/pricing/components/pricePlanList.tsx @@ -1,6 +1,11 @@ -import { checkMarkIcon } from "../const/Icon"; +import { checkMarkIcon, crossMarkIcon } from "../const/Icon"; import { cn } from "@/lib/utils"; -import { getPricing } from "@/server/linkToPricing"; +import { + getPricing, + getSubscriptionItem, + getUsage, + setUsage, +} from "@/server/linkToPricing"; import { useEffect, useState } from "react"; type Tier = { @@ -11,10 +16,18 @@ type Tier = { description: string; features: string[]; featured: boolean; + priority?: TierPriority; }; +enum TierPriority { + Free = "free", + Pro = "pro", + Enterprise = "enterprise", +} + export default function PricingList() { const [productTiers, setProductTiers] = useState(); + const [userUsageId, setUserUsageId] = useState(0); useEffect(() => { (async () => { @@ -40,26 +53,81 @@ export default function PricingList() { name: item.attributes.name, id: item.id, href: item.attributes.buy_now_url, - priceMonthly: item.attributes.price_formatted.split("/")[0], + priceMonthly: + item.attributes.price_formatted.split("/")[0] == "Usage-based" + ? "$20.00" + : item.attributes.price_formatted.split("/")[0], description: description, features: features, // if name contains pro, it's featured featured: item.attributes.name.toLowerCase().includes("pro"), + + // give priority if name contain in enum + priority: Object.values(TierPriority).find((priority) => + item.attributes.name.toLowerCase().includes(priority) + ), }; }); - console.log(newProductTiers); + // sort newProductTiers by priority + newProductTiers.sort((a, b) => { + if (!a.priority) return 1; + if (!b.priority) return -1; + return ( + Object.values(TierPriority).indexOf(a.priority) - + Object.values(TierPriority).indexOf(b.priority) + ); + }); + setProductTiers(newProductTiers); })(); }, []); + useEffect(() => { + (async () => { + // const currentUser = await getUserData(); + + const userUsage = await getUsage(); + const userSubscription = await getSubscriptionItem(); + + // const setUserUsage = await setUsage(236561, 10); + + // console.log(currentUser); + console.log(userSubscription); + })(); + }, []); + + const setUserUsage = async (id: number, quantity: number) => { + try { + const response = await setUsage(id, quantity); + console.log(response); + } catch (error) { + console.error(error); + } + }; + return (

Pricing

+ +
+ + +
+

The right price for you, whoever you are

@@ -68,7 +136,7 @@ export default function PricingList() { Qui iusto aut est earum eos quae. Eligendi est at nam aliquid ad quo reprehenderit in aliquid fugiat dolorum voluptatibus.

-
+
{productTiers && productTiers.map((tier, tierIdx) => (
(
  • - {checkMarkIcon} + {feature.includes("[x]") ? crossMarkIcon : checkMarkIcon}
    - {feature} + {feature.replace("[x]", "")}
  • ))} diff --git a/web/src/app/(app)/pricing/const/Icon.tsx b/web/src/app/(app)/pricing/const/Icon.tsx index 2142282..2257aee 100644 --- a/web/src/app/(app)/pricing/const/Icon.tsx +++ b/web/src/app/(app)/pricing/const/Icon.tsx @@ -13,3 +13,25 @@ export const checkMarkIcon = ( /> ); + +export const crossMarkIcon = ( + + + + +); diff --git a/web/src/components/Navbar.tsx b/web/src/components/Navbar.tsx index 52be0c8..fa5bdf0 100644 --- a/web/src/components/Navbar.tsx +++ b/web/src/components/Navbar.tsx @@ -85,6 +85,13 @@ export function Navbar() {
    {isDesktop && } +