feat: enhance billing upgrade process with confirmation dialog and toast notifications

- Integrated a confirmation dialog for upgrading plans, providing users with clear details about their current and new plans.
- Added toast notifications to inform users of successful upgrades or errors during the process.
- Updated UI elements for better styling and user experience in the billing section.
This commit is contained in:
Mauricio Siu
2026-02-19 12:11:27 -06:00
parent 32a14be564
commit 28fc58d898
@@ -12,6 +12,8 @@ import {
import Link from "next/link"; import Link from "next/link";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { toast } from "sonner";
import { DialogAction } from "@/components/shared/dialog-action";
import { Badge } from "@/components/ui/badge"; import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { import {
@@ -196,7 +198,7 @@ export const ShowBilling = () => {
{useNewPricing && {useNewPricing &&
data?.currentPlan === "legacy" && data?.currentPlan === "legacy" &&
data?.subscriptions?.length > 0 && ( data?.subscriptions?.length > 0 && (
<div className="rounded-xl border border-primary/30 bg-primary/5 p-4 space-y-4"> <div className="rounded-xl border border-border bg-primary/5 p-4 space-y-4 max-w-2xl">
<h3 className="text-lg font-medium">Upgrade your plan</h3> <h3 className="text-lg font-medium">Upgrade your plan</h3>
<p className="text-sm text-muted-foreground"> <p className="text-sm text-muted-foreground">
You’re on the legacy plan. Switch to Hobby or Startup You’re on the legacy plan. Switch to Hobby or Startup
@@ -311,34 +313,73 @@ export const ShowBilling = () => {
? `$${calculatePriceHobby(upgradeServerQty, updateFormAnnual).toFixed(2)} per ${updateFormAnnual ? "year" : "month"}` ? `$${calculatePriceHobby(upgradeServerQty, updateFormAnnual).toFixed(2)} per ${updateFormAnnual ? "year" : "month"}`
: `$${calculatePriceStartup(upgradeServerQty, updateFormAnnual).toFixed(2)} per ${updateFormAnnual ? "year" : "month"}`} : `$${calculatePriceStartup(upgradeServerQty, updateFormAnnual).toFixed(2)} per ${updateFormAnnual ? "year" : "month"}`}
</p> </p>
<Button <DialogAction
className="w-full sm:w-auto" title="Confirm upgrade"
disabled={ description={
isUpgrading || <div className="space-y-2">
(upgradeTier === "startup" && <p className="font-medium text-foreground">
upgradeServerQty < STARTUP_SERVERS_INCLUDED) Current plan: Legacy
</p>
<p className="font-medium text-foreground">
New plan:{" "}
{upgradeTier === "startup"
? "Startup"
: "Hobby"}{" "}
· {upgradeServerQty} server
{upgradeServerQty !== 1 ? "s" : ""} · $
{upgradeTier === "hobby"
? calculatePriceHobby(
upgradeServerQty,
updateFormAnnual,
).toFixed(2)
: calculatePriceStartup(
upgradeServerQty,
updateFormAnnual,
).toFixed(2)}
/{updateFormAnnual ? "yr" : "mo"} (
{updateFormAnnual ? "annual" : "monthly"})
</p>
<p className="text-sm text-muted-foreground">
Stripe will prorate the change.
</p>
</div>
} }
type="default"
onClick={async () => { onClick={async () => {
if (!upgradeTier) return; if (!upgradeTier) return;
await upgradeSubscription({ try {
tier: upgradeTier, await upgradeSubscription({
serverQuantity: upgradeServerQty, tier: upgradeTier,
isAnnual: updateFormAnnual, serverQuantity: upgradeServerQty,
}); isAnnual: updateFormAnnual,
await utils.stripe.getProducts.invalidate(); });
await utils.user.get.invalidate(); await utils.stripe.getProducts.invalidate();
setUpgradeTier(null); await utils.user.get.invalidate();
setUpgradeTier(null);
toast.success("Plan upgraded successfully");
} catch {
toast.error("Error upgrading plan");
}
}} }}
> >
{isUpgrading ? ( <Button
<> className="w-full sm:w-auto"
<Loader2 className="mr-2 h-4 w-4 animate-spin" /> disabled={
Upgrading… isUpgrading ||
</> (upgradeTier === "startup" &&
) : ( upgradeServerQty < STARTUP_SERVERS_INCLUDED)
"Upgrade plan" }
)} >
</Button> {isUpgrading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Upgrading…
</>
) : (
"Upgrade plan"
)}
</Button>
</DialogAction>
</div> </div>
)} )}
</div> </div>
@@ -348,7 +389,7 @@ export const ShowBilling = () => {
(data?.currentPlan === "hobby" || (data?.currentPlan === "hobby" ||
data?.currentPlan === "startup") && data?.currentPlan === "startup") &&
data?.subscriptions?.length > 0 && ( data?.subscriptions?.length > 0 && (
<div className="rounded-xl border border-primary/30 bg-primary/5 p-4 space-y-4 max-w-2xl"> <div className="rounded-xl border border-border bg-primary/5 p-4 space-y-4 max-w-2xl">
<h3 className="text-lg font-medium"> <h3 className="text-lg font-medium">
Change plan or number of servers Change plan or number of servers
</h3> </h3>
@@ -366,8 +407,7 @@ export const ShowBilling = () => {
<> <>
{" · "} {" · "}
<span className="font-medium text-foreground"> <span className="font-medium text-foreground">
$ ${data.currentPriceAmount.toFixed(2)}/
{data.currentPriceAmount.toFixed(2)}/
{data?.isAnnualCurrent ? "yr" : "mo"} {data?.isAnnualCurrent ? "yr" : "mo"}
</span> </span>
</> </>
@@ -487,35 +527,92 @@ export const ShowBilling = () => {
? `$${calculatePriceHobby(upgradeServerQty, updateFormAnnual).toFixed(2)} per ${updateFormAnnual ? "year" : "month"}` ? `$${calculatePriceHobby(upgradeServerQty, updateFormAnnual).toFixed(2)} per ${updateFormAnnual ? "year" : "month"}`
: `$${calculatePriceStartup(upgradeServerQty, updateFormAnnual).toFixed(2)} per ${updateFormAnnual ? "year" : "month"}`} : `$${calculatePriceStartup(upgradeServerQty, updateFormAnnual).toFixed(2)} per ${updateFormAnnual ? "year" : "month"}`}
</p> </p>
<Button <DialogAction
className="w-auto" title="Confirm plan change"
disabled={ description={
isUpgrading || <div className="space-y-2">
(upgradeTier === "startup" && <p className="font-medium text-foreground">
upgradeServerQty < STARTUP_SERVERS_INCLUDED) Current plan:{" "}
{data?.currentPlan === "startup"
? "Startup"
: "Hobby"}{" "}
· {admin?.user.serversQuantity ?? 0} server
{(admin?.user.serversQuantity ?? 0) !== 1
? "s"
: ""}{" "}
·{" "}
{data?.currentPriceAmount != null
? `$${data.currentPriceAmount.toFixed(2)}/${data?.isAnnualCurrent ? "yr" : "mo"}`
: ""}{" "}
({data?.isAnnualCurrent ? "annual" : "monthly"})
</p>
<p className="font-medium text-foreground">
New plan:{" "}
{upgradeTier === "startup"
? "Startup"
: "Hobby"}{" "}
· {upgradeServerQty} server
{upgradeServerQty !== 1 ? "s" : ""} · $
{upgradeTier === "hobby"
? calculatePriceHobby(
upgradeServerQty,
updateFormAnnual,
).toFixed(2)
: calculatePriceStartup(
upgradeServerQty,
updateFormAnnual,
).toFixed(2)}
/{updateFormAnnual ? "yr" : "mo"} (
{updateFormAnnual ? "annual" : "monthly"})
</p>
<p className="text-sm text-muted-foreground">
Stripe will prorate the change.
</p>
</div>
} }
type="default"
onClick={async () => { onClick={async () => {
if (!upgradeTier) return; if (!upgradeTier) return;
await upgradeSubscription({ try {
tier: upgradeTier, await upgradeSubscription({
serverQuantity: upgradeServerQty, tier: upgradeTier,
isAnnual: updateFormAnnual, serverQuantity: upgradeServerQty,
}); isAnnual: updateFormAnnual,
await utils.stripe.getProducts.invalidate(); });
await utils.user.get.invalidate(); await utils.stripe.getProducts.invalidate();
setUpgradeTier(null);
// window.location.reload(); // add delay of 3 seconds
await new Promise((resolve) =>
setTimeout(resolve, 3000),
);
await utils.user.get.invalidate();
setUpgradeTier(null);
toast.success(
"Subscription updated successfully",
);
} catch {
toast.error("Error updating subscription");
}
}} }}
> >
{isUpgrading ? ( <Button
<> className="w-auto"
<Loader2 className="mr-2 h-4 w-4 animate-spin" /> disabled={
Updating… isUpgrading ||
</> (upgradeTier === "startup" &&
) : ( upgradeServerQty < STARTUP_SERVERS_INCLUDED)
"Update subscription" }
)} >
</Button> {isUpgrading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Updating…
</>
) : (
"Update subscription"
)}
</Button>
</DialogAction>
</div> </div>
)} )}
</div> </div>