Skip to content

Commit

Permalink
buy credits component
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgsolomon committed Oct 29, 2024
1 parent 8a9f587 commit 0604683
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 91 deletions.
121 changes: 121 additions & 0 deletions src/app/buy-credits-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"use client";
import Image from "next/image";
import { Button, buttonVariants } from "@/components/ui/button";
import { useCreateVideo } from "./usecreatevideo";
import { useYourVideos } from "./useyourvideos";
import { useEffect, useState } from "react";
import { toast } from "sonner";
import {
ArrowUpRight,
Coins,
Crown,
Folder,
Github,
Loader2,
Minus,
Plus,
Star,
Wand,
X,
Zap,
} from "lucide-react";
import { useUser } from "@clerk/nextjs";
import { Skeleton } from "@/components/ui/skeleton";
import { trpc } from "@/trpc/client";
import Link from "next/link";
import Credits from "./credits";
import { useRouter } from "next/navigation";
import { Progress } from "@/components/ui/progress";
import ProButton from "./ProButton";
import NumberTicker from "@/components/magicui/number-ticker";
import { useGenerationType } from "./usegenerationtype";
import ClientTweetCard from "@/components/magicui/client-tweet-card";
import XIcon from "@/components/svg/XIcon";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Separator } from "@/components/ui/separator";
import {
Card,
CardContent,
CardDescription,
CardTitle,
CardHeader,
} from "@/components/ui/card";

export default function BuyCreditsDialog() {
const { mutate: createStripeSession } =
trpc.user.createCreditPackSession.useMutation({
onSuccess: ({ url }) => {
if (url) window.location.href = url;
},
});

const [showCredits, setShowCredits] = useState(false);
const [creditPacks, setCreditPacks] = useState(1);
const cost = creditPacks * 5;
const totalCredits = creditPacks * 25;

return (
<Dialog>
<DialogTrigger asChild>
<Button
size="lg"
className="mt-2 flex w-full flex-row items-center justify-center gap-2"
variant="outline"
>
Buy Credits <Coins className="size-4" />
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle className="text-2xl">Purchase Credits</DialogTitle>
</DialogHeader>
<div className="grid gap-4 py-2">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<Coins className="h-5 w-5 text-yellow-500" />
<p className="text-lg font-bold">{creditPacks * 25} credits</p>
</div>
<div className="flex items-center gap-2">
<Button
variant="outline"
size="icon"
disabled={creditPacks <= 1}
onClick={() => setCreditPacks((prev) => Math.max(1, prev - 1))}
>
<Minus className="h-4 w-4" />
</Button>
<span className="w-8 text-center">{creditPacks}</span>
<Button
variant="outline"
size="icon"
disabled={creditPacks >= 10}
onClick={() => setCreditPacks((prev) => Math.min(10, prev + 1))}
>
<Plus className="h-4 w-4" />
</Button>
</div>
</div>
<p className="text-sm text-muted-foreground">
Each pack: 25 credits (~2-3 videos)
</p>
<div>
<p className="text-lg font-bold">Total: ${creditPacks * 5}</p>
<Button
variant="default"
onClick={() => createStripeSession({ creditPacks })}
className="mt-2 flex w-full flex-row items-center justify-center gap-2"
>
Purchase Credits <Zap className="h-4" />
</Button>
</div>
</div>
</DialogContent>
</Dialog>
);
}
8 changes: 3 additions & 5 deletions src/app/generationtype.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { useAuth } from "@clerk/nextjs";
import { useCreateVideo } from "./usecreatevideo";
import { Avatar, AvatarImage } from "@/components/ui/avatar";
import ProButton from "./ProButton";
import BuyCreditsDialog from "./buy-credits-dialog";

export default function GenerationType() {
const [typeSelected, _] = useState<"math" | "brainrot" | "">("");
Expand Down Expand Up @@ -198,11 +199,7 @@ export default function GenerationType() {
<div className="flex max-w-[300px] flex-col gap-0 rounded-lg border border-border bg-card/80 p-4 text-center text-sm shadow-sm">
<div className="flex flex-col gap-2 font-bold">
<div className="flex flex-col gap-1">
{" "}
go pro to generate videos!
<span className="text-xs font-normal italic">
(and to support me 🥹🫶)
</span>
Go Pro or Buy Credits to Generate Videos!
</div>

<ProButton searchQueryString={searchQueryString}>
Expand All @@ -214,6 +211,7 @@ export default function GenerationType() {
GO PRO <Crown className="size-4" />
</Button>
</ProButton>
<BuyCreditsDialog />
</div>
</div>
</div>
Expand Down
88 changes: 2 additions & 86 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
CardTitle,
CardHeader,
} from "@/components/ui/card";
import BuyCreditsDialog from "./buy-credits-dialog";

export default function Home({
searchParams,
Expand Down Expand Up @@ -216,18 +217,6 @@ export default function Home({
}
}, [user.isSignedIn, videoStatus.data?.videos]);

const { mutate: createStripeSession } =
trpc.user.createCreditPackSession.useMutation({
onSuccess: ({ url }) => {
if (url) window.location.href = url;
},
});

const [showCredits, setShowCredits] = useState(false);
const [creditPacks, setCreditPacks] = useState(1);
const cost = creditPacks * 5;
const totalCredits = creditPacks * 25;

useEffect(() => {
if (isInQueue) {
toast.info("Your video is currently in queue", { icon: "🕒" });
Expand Down Expand Up @@ -335,80 +324,7 @@ export default function Home({
Purchase credits for individual videos
</p>
</div>
<Dialog>
<DialogTrigger asChild>
<Button
size="lg"
className="mt-2 flex w-full flex-row items-center justify-center gap-2"
variant="outline"
>
Buy Credits <Coins className="size-4" />
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle className="text-2xl">
Purchase Credits
</DialogTitle>
</DialogHeader>
<div className="grid gap-4 py-2">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<Coins className="h-5 w-5 text-yellow-500" />
<p className="text-lg font-bold">
{creditPacks * 25} credits
</p>
</div>
<div className="flex items-center gap-2">
<Button
variant="outline"
size="icon"
disabled={creditPacks <= 1}
onClick={() =>
setCreditPacks((prev) =>
Math.max(1, prev - 1),
)
}
>
<Minus className="h-4 w-4" />
</Button>
<span className="w-8 text-center">
{creditPacks}
</span>
<Button
variant="outline"
size="icon"
disabled={creditPacks >= 10}
onClick={() =>
setCreditPacks((prev) =>
Math.min(10, prev + 1),
)
}
>
<Plus className="h-4 w-4" />
</Button>
</div>
</div>
<p className="text-sm text-muted-foreground">
Each pack: 25 credits (~2-3 videos)
</p>
<div>
<p className="text-lg font-bold">
Total: ${creditPacks * 5}
</p>
<Button
variant="default"
onClick={() =>
createStripeSession({ creditPacks })
}
className="mt-2 flex w-full flex-row items-center justify-center gap-2"
>
Purchase Credits <Zap className="h-4" />
</Button>
</div>
</div>
</DialogContent>
</Dialog>
<BuyCreditsDialog />
</div>
</div>
</CardContent>
Expand Down

0 comments on commit 0604683

Please sign in to comment.