settings.tsx159 lines, 5.0 KB
| 1 | import { useCurrentUser } from "@/hooks/use-current-user"; |
| 2 | import { useSubscription } from "@/hooks/use-subscription"; |
| 3 | import { api } from "@/lib/api"; |
| 4 | import stripePromise from "@/lib/stripe"; |
| 5 | import { toast } from "sonner"; |
| 6 | import { |
| 7 | Card, |
| 8 | CardContent, |
| 9 | CardDescription, |
| 10 | CardFooter, |
| 11 | CardHeader, |
| 12 | CardTitle, |
| 13 | } from "../ui/card"; |
| 14 | import { Label } from "../ui/label"; |
| 15 | import { Input } from "../ui/input"; |
| 16 | import { Check } from "lucide-react"; |
| 17 | import { Separator } from "../ui/separator"; |
| 18 | import { Button } from "../ui/button"; |
| 19 | |
| 20 | export default function Settings() { |
| 21 | const { |
| 22 | subscriptionStatus, |
| 23 | isSubscriptionLoading, |
| 24 | isSubscriptionError, |
| 25 | setLoading, |
| 26 | } = useSubscription(); |
| 27 | const { user } = useCurrentUser(); |
| 28 | console.log(subscriptionStatus); |
| 29 | |
| 30 | if (!subscriptionStatus) { |
| 31 | return null; |
| 32 | } |
| 33 | |
| 34 | const isActive = subscriptionStatus.status === "active"; |
| 35 | |
| 36 | const handleUpgrade = async () => { |
| 37 | setLoading(true); |
| 38 | if (!isActive) { |
| 39 | try { |
| 40 | const response = await api.get("/payments/create-checkout-session"); |
| 41 | const stripe = await stripePromise; |
| 42 | await stripe?.redirectToCheckout({ |
| 43 | sessionId: response.data.sessionId, |
| 44 | }); |
| 45 | } catch (error) { |
| 46 | toast.error("Please try again or login to your account"); |
| 47 | } finally { |
| 48 | setLoading(false); |
| 49 | } |
| 50 | } else { |
| 51 | toast.error("You are already a premium member"); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | if (!user) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | return ( |
| 60 | <div className="container mx-auto py-10"> |
| 61 | <div className="grid gap-8 md:grid-cols-2"> |
| 62 | <Card> |
| 63 | <CardHeader> |
| 64 | <CardTitle>Personal Information</CardTitle> |
| 65 | <CardDescription>Your personal information</CardDescription> |
| 66 | </CardHeader> |
| 67 | <CardContent className="space-y-2"> |
| 68 | <div className="space-y-2"> |
| 69 | <Label>Full Name</Label> |
| 70 | <Input |
| 71 | value={user.displayName} |
| 72 | id="name" |
| 73 | readOnly |
| 74 | className="bg-gray-100" |
| 75 | /> |
| 76 | </div> |
| 77 | <div className="space-y-2"> |
| 78 | <Label>Full Name</Label> |
| 79 | <Input |
| 80 | value={user.email} |
| 81 | id="email" |
| 82 | readOnly |
| 83 | className="bg-gray-100" |
| 84 | /> |
| 85 | </div> |
| 86 | <p className="text-sm text-muted-foreground"> |
| 87 | Your account is managed through Google. If you want to change your |
| 88 | email, please contact us. |
| 89 | </p> |
| 90 | </CardContent> |
| 91 | </Card> |
| 92 | |
| 93 | {isActive ? ( |
| 94 | <Card> |
| 95 | <CardHeader> |
| 96 | <CardTitle>Premium</CardTitle> |
| 97 | <CardDescription>Your membership details</CardDescription> |
| 98 | </CardHeader> |
| 99 | <CardContent className="space-y-4"> |
| 100 | <div className="flex items-center justify-between"> |
| 101 | <div className="flex items-center gap-2"> |
| 102 | <div className="flex items-center gap-1 rounded-md bg-green-600/10 p-1 pr-2 text-xs font-medium text-green-600"> |
| 103 | <div className="m-0.5 rounded-full bg-green-600/10 p-[3px]"> |
| 104 | <Check size={16} className="text-foreground" /> |
| 105 | </div> |
| 106 | Active membership |
| 107 | </div> |
| 108 | <p className="text-sm text-muted-foreground"> |
| 109 | Lifetime membership granted |
| 110 | </p> |
| 111 | </div> |
| 112 | </div> |
| 113 | <Separator /> |
| 114 | <div className="space-y-2"> |
| 115 | <p> |
| 116 | Thank you for your support. Enjoy the benefits of premium. |
| 117 | </p> |
| 118 | </div> |
| 119 | </CardContent> |
| 120 | </Card> |
| 121 | ) : ( |
| 122 | <Card className="border-primary border-2 shadow-lg"> |
| 123 | <CardHeader> |
| 124 | <CardTitle>Get unlimted access forever</CardTitle> |
| 125 | <CardDescription> |
| 126 | Upgrade to premium and enjoy unlimited access to all features |
| 127 | </CardDescription> |
| 128 | </CardHeader> |
| 129 | <CardContent className="space-y-4"> |
| 130 | <ul className="space-y-2"> |
| 131 | <li className="flex items-center gap-2"> |
| 132 | <Check size={16} className="text-foreground" /> |
| 133 | <p>Unlimited access to all features</p> |
| 134 | </li> |
| 135 | <li className="flex items-center gap-2"> |
| 136 | <Check size={16} className="text-foreground" /> |
| 137 | <p>Unlimited access to all features</p> |
| 138 | </li> |
| 139 | <li className="flex items-center gap-2"> |
| 140 | <Check size={16} className="text-foreground" /> |
| 141 | <p>Unlimited access to all features</p> |
| 142 | </li> |
| 143 | </ul> |
| 144 | </CardContent> |
| 145 | <CardFooter> |
| 146 | <Button |
| 147 | className="w-full" |
| 148 | onClick={handleUpgrade} |
| 149 | variant={"outline"} |
| 150 | > |
| 151 | Purchase Lifetime Membership |
| 152 | </Button> |
| 153 | </CardFooter> |
| 154 | </Card> |
| 155 | )} |
| 156 | </div> |
| 157 | </div> |
| 158 | ); |
| 159 | } |