page.tsx203 lines, 6.6 KB
| 1 | "use client"; |
| 2 | |
| 3 | import React, { useState } from "react"; |
| 4 | import { |
| 5 | useUserSubscription, |
| 6 | useCancelSubscription, |
| 7 | } from "@/lib/hooks/queries/subscriptions"; |
| 8 | import { Button } from "@/components/ui/button"; |
| 9 | import { |
| 10 | Card, |
| 11 | CardContent, |
| 12 | CardDescription, |
| 13 | CardFooter, |
| 14 | CardHeader, |
| 15 | CardTitle, |
| 16 | } from "@/components/ui/card"; |
| 17 | import { Progress } from "@/components/ui/progress"; |
| 18 | import { Loader2 } from "lucide-react"; |
| 19 | import { format } from "date-fns"; |
| 20 | import { useRouter } from "next/navigation"; |
| 21 | import { toast } from "sonner"; |
| 22 | |
| 23 | export default function ManageSubscriptionPage() { |
| 24 | const router = useRouter(); |
| 25 | const [confirmCancel, setConfirmCancel] = useState(false); |
| 26 | const [isCancelling, setIsCancelling] = useState(false); |
| 27 | const { data: userSubscription, isLoading } = useUserSubscription(); |
| 28 | const { mutate: cancelSubscription } = useCancelSubscription(); |
| 29 | |
| 30 | // Check if user has an active subscription |
| 31 | const hasActiveSubscription = |
| 32 | userSubscription?.isSubscribed && |
| 33 | userSubscription.subscription?.status === "active"; |
| 34 | |
| 35 | // Get the current plan if user is subscribed |
| 36 | const subscription = hasActiveSubscription |
| 37 | ? userSubscription.subscription |
| 38 | : null; |
| 39 | const plan = subscription?.plan; |
| 40 | |
| 41 | // Handle cancel subscription |
| 42 | const handleCancelSubscription = () => { |
| 43 | if (confirmCancel) { |
| 44 | cancelSubscription(undefined, { |
| 45 | onSuccess: () => { |
| 46 | toast.success("Your subscription has been cancelled"); |
| 47 | setConfirmCancel(false); |
| 48 | }, |
| 49 | onError: (error) => { |
| 50 | toast.error("Failed to cancel subscription"); |
| 51 | }, |
| 52 | }); |
| 53 | } else { |
| 54 | setConfirmCancel(true); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | if (isLoading) { |
| 59 | return ( |
| 60 | <div className="container mx-auto px-4 py-8 flex justify-center items-center h-64"> |
| 61 | <Loader2 className="h-8 w-8 animate-spin text-primary" /> |
| 62 | </div> |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | if (!hasActiveSubscription) { |
| 67 | return ( |
| 68 | <div className="container mx-auto px-4 py-8"> |
| 69 | <h1 className="text-3xl font-bold mb-6">Manage Subscription</h1> |
| 70 | <Card> |
| 71 | <CardHeader> |
| 72 | <CardTitle>No Active Subscription</CardTitle> |
| 73 | <CardDescription> |
| 74 | You don't have an active subscription. |
| 75 | </CardDescription> |
| 76 | </CardHeader> |
| 77 | <CardFooter> |
| 78 | <Button onClick={() => router.push("/subscriptions")}> |
| 79 | View Subscription Plans |
| 80 | </Button> |
| 81 | </CardFooter> |
| 82 | </Card> |
| 83 | </div> |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | // Calculate usage percentages |
| 88 | const videoUsagePercent = |
| 89 | plan!.videoLimit === -1 |
| 90 | ? 0 |
| 91 | : Math.min(100, (subscription!.videosUsed / plan!.videoLimit) * 100); |
| 92 | |
| 93 | const minutesUsagePercent = |
| 94 | plan!.minutesLimit === -1 |
| 95 | ? 0 |
| 96 | : Math.min(100, (subscription!.minutesUsed / plan!.minutesLimit) * 100); |
| 97 | |
| 98 | return ( |
| 99 | <div className="container mx-auto px-4 py-8"> |
| 100 | <h1 className="text-3xl font-bold mb-6">Manage Subscription</h1> |
| 101 | |
| 102 | <Card className="mb-8"> |
| 103 | <CardHeader> |
| 104 | <CardTitle>{plan!.name} Plan</CardTitle> |
| 105 | <CardDescription>{plan!.description}</CardDescription> |
| 106 | </CardHeader> |
| 107 | <CardContent className="space-y-6"> |
| 108 | <div> |
| 109 | <div className="flex justify-between mb-1"> |
| 110 | <span className="text-sm font-medium">Subscription Status</span> |
| 111 | <span className="text-sm font-medium capitalize"> |
| 112 | {subscription!.status} |
| 113 | </span> |
| 114 | </div> |
| 115 | <div className="flex justify-between mb-1"> |
| 116 | <span className="text-sm font-medium">Billing Cycle</span> |
| 117 | <span className="text-sm font-medium capitalize"> |
| 118 | {plan!.billingInterval} |
| 119 | </span> |
| 120 | </div> |
| 121 | <div className="flex justify-between mb-1"> |
| 122 | <span className="text-sm font-medium">Price</span> |
| 123 | <span className="text-sm font-medium"> |
| 124 | ${plan!.price}/{plan!.billingInterval} |
| 125 | </span> |
| 126 | </div> |
| 127 | <div className="flex justify-between mb-1"> |
| 128 | <span className="text-sm font-medium">Current Period</span> |
| 129 | <span className="text-sm font-medium"> |
| 130 | {format( |
| 131 | new Date(subscription!.currentPeriodStart), |
| 132 | "MMM d, yyyy" |
| 133 | )}{" "} |
| 134 | -{" "} |
| 135 | {format( |
| 136 | new Date(subscription!.currentPeriodEnd), |
| 137 | "MMM d, yyyy" |
| 138 | )} |
| 139 | </span> |
| 140 | </div> |
| 141 | </div> |
| 142 | |
| 143 | <div> |
| 144 | <h3 className="text-lg font-medium mb-3">Usage</h3> |
| 145 | |
| 146 | <div className="space-y-3"> |
| 147 | <div> |
| 148 | <div className="flex justify-between mb-1"> |
| 149 | <span className="text-sm font-medium">Videos</span> |
| 150 | <span className="text-sm font-medium"> |
| 151 | {subscription!.videosUsed} /{" "} |
| 152 | {plan!.videoLimit === -1 ? "Unlimited" : plan!.videoLimit} |
| 153 | </span> |
| 154 | </div> |
| 155 | {plan!.videoLimit !== -1 && ( |
| 156 | <Progress value={videoUsagePercent} className="h-2" /> |
| 157 | )} |
| 158 | </div> |
| 159 | |
| 160 | <div> |
| 161 | <div className="flex justify-between mb-1"> |
| 162 | <span className="text-sm font-medium">Minutes</span> |
| 163 | <span className="text-sm font-medium"> |
| 164 | {subscription!.minutesUsed} /{" "} |
| 165 | {plan!.minutesLimit === -1 |
| 166 | ? "Unlimited" |
| 167 | : plan!.minutesLimit} |
| 168 | </span> |
| 169 | </div> |
| 170 | {plan!.minutesLimit !== -1 && ( |
| 171 | <Progress value={minutesUsagePercent} className="h-2" /> |
| 172 | )} |
| 173 | </div> |
| 174 | </div> |
| 175 | </div> |
| 176 | </CardContent> |
| 177 | <CardFooter> |
| 178 | <div className="flex flex-col w-full space-y-3"> |
| 179 | <Button |
| 180 | variant="destructive" |
| 181 | onClick={handleCancelSubscription} |
| 182 | disabled={isCancelling} |
| 183 | > |
| 184 | {isCancelling && ( |
| 185 | <Loader2 className="mr-2 h-4 w-4 animate-spin" /> |
| 186 | )} |
| 187 | {confirmCancel ? "Confirm Cancellation" : "Cancel Subscription"} |
| 188 | </Button> |
| 189 | {confirmCancel && ( |
| 190 | <Button variant="outline" onClick={() => setConfirmCancel(false)}> |
| 191 | Keep My Subscription |
| 192 | </Button> |
| 193 | )} |
| 194 | <p className="text-xs text-muted-foreground mt-2"> |
| 195 | Your subscription will remain active until the end of your current |
| 196 | billing period. |
| 197 | </p> |
| 198 | </div> |
| 199 | </CardFooter> |
| 200 | </Card> |
| 201 | </div> |
| 202 | ); |
| 203 | } |