feat: add topup management features including topup creation, cancellation, and countdown timer

This commit is contained in:
2025-07-04 23:07:02 +05:00
parent ee461bbbf8
commit f3328f7a7b
7 changed files with 657 additions and 21 deletions

View File

@ -1,4 +1,11 @@
"use client";
import { useAtom } from "jotai";
import { CircleDollarSign, Loader2, Wallet2 } from "lucide-react";
import millify from "millify";
import { usePathname, useRouter } from "next/navigation";
import { useState } from "react";
import { toast } from "sonner";
import { createTopup } from "@/actions/payment";
import { Button } from "@/components/ui/button";
import {
Drawer,
@ -12,12 +19,6 @@ import {
} from "@/components/ui/drawer";
import { WalletDrawerOpenAtom, walletTopUpValue } from "@/lib/atoms";
import type { TopupType } from "@/lib/types";
import { useAtom } from "jotai";
import { CircleDollarSign, Loader2, Wallet2 } from "lucide-react";
import millify from "millify";
import { useSession } from "next-auth/react";
import { usePathname } from "next/navigation";
import { useState } from "react";
import NumberInput from "./number-input";
export function Wallet({
@ -25,21 +26,18 @@ export function Wallet({
}: {
walletBalance: number;
}) {
const session = useSession();
const pathname = usePathname();
const [amount, setAmount] = useAtom(walletTopUpValue);
const [isOpen, setIsOpen] = useAtom(WalletDrawerOpenAtom);
const [disabled, setDisabled] = useState(false);
// const router = useRouter();
const router = useRouter();
if (pathname === "/payment") {
return null;
}
const data: TopupType = {
userId: session?.data?.user?.id ?? "",
amount: Number.parseFloat(amount.toFixed(2)),
paid: false,
};
return (
@ -85,23 +83,20 @@ export function Wallet({
onClick={async () => {
console.log(data);
setDisabled(true);
// const payment = await createPayment(data)
const topup = await createTopup(data)
setDisabled(false);
// setMonths(1)
// if (payment) {
// router.push(`/payments/${payment.id}`);
// setIsOpen(!isOpen);
// } else {
// toast.error("Something went wrong.")
// }
if (topup) {
router.push(`/top-ups/${topup.id}`);
setIsOpen(!isOpen);
} else {
toast.error("Something went wrong.")
}
}}
className="w-full"
disabled={amount === 0 || disabled}
>
{disabled ? (
<>
<Loader2 className="ml-2 animate-spin" />
</>
<Loader2 className="ml-2 animate-spin" />
) : (
<>
Go to payment