diff --git a/app/(dashboard)/top-ups/[topupId]/page.tsx b/app/(dashboard)/top-ups/[topupId]/page.tsx new file mode 100644 index 0000000..bba63f6 --- /dev/null +++ b/app/(dashboard)/top-ups/[topupId]/page.tsx @@ -0,0 +1,72 @@ +import { redirect } from "next/navigation"; +import { getTopup } from "@/actions/payment"; +import CancelTopupButton from "@/components/billing/cancel-topup-button"; +import ExpiryCountDown from "@/components/billing/expiry-time-countdown"; +import ClientErrorMessage from "@/components/client-error-message"; +import TopupToPay from "@/components/topup-to-pay"; +import { Button } from "@/components/ui/button"; +import { TextShimmer } from "@/components/ui/text-shimmer"; +import { cn } from "@/lib/utils"; +import { tryCatch } from "@/utils/tryCatch"; +export default async function TopupPage({ + params, +}: { + params: Promise<{ topupId: string }>; +}) { + const topupId = (await params).topupId; + const [error, topup] = await tryCatch(getTopup({ id: topupId })); + if (error) { + if (error.message === "Invalid token.") redirect("/auth/signin"); + return ; + } + + + return ( +
+
+

Topup

+
+ {!topup.is_expired && ( + + + )} + + {!topup.paid && ( + topup.is_expired ? ( + + ) : ( + + ) + )} +
+
+ {!topup.paid && ( + + )} +
+ +
+
+ ); +} diff --git a/app/(dashboard)/top-ups/page.tsx b/app/(dashboard)/top-ups/page.tsx new file mode 100644 index 0000000..8b27ed9 --- /dev/null +++ b/app/(dashboard)/top-ups/page.tsx @@ -0,0 +1,63 @@ +import { Suspense } from "react"; +import DynamicFilter from "@/components/generic-filter"; +import { TopupsTable } from "@/components/topups-table"; + +export default async function Topups({ + searchParams, +}: { + searchParams: Promise<{ + query: string; + page: number; + sortBy: string; + status: string; + }>; +}) { + const query = (await searchParams)?.query || ""; + + return ( +
+
+

My Topups

+
+
+ +
+ + + +
+ ); +} diff --git a/components/billing/cancel-topup-button.tsx b/components/billing/cancel-topup-button.tsx new file mode 100644 index 0000000..a6ca438 --- /dev/null +++ b/components/billing/cancel-topup-button.tsx @@ -0,0 +1,37 @@ +"use client"; + +import { Loader2, Trash2 } from "lucide-react"; +import { useRouter } from "next/navigation"; +import React from "react"; +import { toast } from "sonner"; +import { cancelTopup } from "@/actions/payment"; +import { tryCatch } from "@/utils/tryCatch"; +import { Button } from "../ui/button"; + +export default function CancelTopupButton({ + topupId, +}: { topupId: string }) { + const router = useRouter(); + const [loading, setLoading] = React.useState(false); + return ( + + ); +} diff --git a/components/billing/expiry-time-countdown.tsx b/components/billing/expiry-time-countdown.tsx new file mode 100644 index 0000000..a5a7188 --- /dev/null +++ b/components/billing/expiry-time-countdown.tsx @@ -0,0 +1,57 @@ +'use client' +import { usePathname, useRouter } from 'next/navigation' +import { useEffect, useState } from 'react' +import { Progress } from '@/components/ui/progress' + +const calculateTimeLeft = (expiresAt: string) => { + const now = Date.now() + const expirationTime = new Date(expiresAt).getTime() + return Math.max(0, Math.floor((expirationTime - now) / 1000)) +} + +const HumanizeTimeLeft = (seconds: number) => { + const minutes = Math.floor(seconds / 60) + const remainingSeconds = seconds % 60 + return `${minutes}m ${remainingSeconds}s` +} + +export default function ExpiryCountDown({ expiresAt }: { expiresAt: string }) { + const [timeLeft, setTimeLeft] = useState(calculateTimeLeft(expiresAt)) + const [mounted, setMounted] = useState(false) + const router = useRouter() + const pathname = usePathname() + useEffect(() => { + setMounted(true) + }, []) + + useEffect(() => { + const timer = setInterval(() => { + setTimeLeft(calculateTimeLeft(expiresAt)) + }, 1000) + + return () => clearInterval(timer) + }, [expiresAt]) + + useEffect(() => { + if (timeLeft <= 0) { + router.replace(pathname) + } + }, [timeLeft, router, pathname]) + + if (!mounted) { + return null + } + return ( +
+
+ {timeLeft ? ( + Time left: {HumanizeTimeLeft(timeLeft)} + ) : ( + Top up has expired. Please make another topup to add balance to your wallet. + )} + {timeLeft > 0 && ( + + )} +
+ ) +} diff --git a/components/topup-to-pay.tsx b/components/topup-to-pay.tsx new file mode 100644 index 0000000..e3d160d --- /dev/null +++ b/components/topup-to-pay.tsx @@ -0,0 +1,177 @@ +"use client"; +import { + BadgeDollarSign, + Clipboard, + ClipboardCheck, + Loader2, +} from "lucide-react"; +import { useState } from "react"; +import { toast } from "sonner"; +import { verifyTopupPayment } from "@/actions/payment"; +import { + Table, + TableBody, + TableCaption, + TableCell, + TableFooter, + TableRow, +} from "@/components/ui/table"; +import type { Topup } from "@/lib/backend-types"; +import { Button } from "./ui/button"; + +export default function TopupToPay({ topup, disabled }: { topup?: Topup, disabled?: boolean }) { + const [verifyingTransferPayment, setVerifyingTransferPayment] = + useState(false); + + return ( +
+
+ + +
+

Please send the following amount to the payment address

+ + {topup?.paid ? ( + + ) : ( +
+ +
+ )} +
+
+ + + Topup created at + + {new Date(topup?.created_at ?? "").toLocaleDateString("en-US", { + month: "short", + day: "2-digit", + year: "numeric", + minute: "2-digit", + hour: "2-digit", + second: "2-digit", + })} + + + + Topup expires at + + {new Date(topup?.expires_at ?? "").toLocaleDateString("en-US", { + month: "short", + day: "2-digit", + year: "numeric", + minute: "2-digit", + hour: "2-digit", + second: "2-digit", + })} + + + + MIB Reference + + {topup?.mib_reference ? topup.mib_reference : "N/A"} + + + + + + Total Due + + {topup?.amount?.toFixed(2)} + + + +
+
+
+ ); +} + +function AccountInfomation({ + accountNo, + accName, +}: { + accountNo: string; + accName: string; +}) { + const [accNo, setAccNo] = useState(false); + return ( +
+
+ Account Information +
+
+
Account Name
+ {accName} +
+
+
+

Account No

+ {accountNo} +
+ +
+
+ ); +} diff --git a/components/topups-table.tsx b/components/topups-table.tsx new file mode 100644 index 0000000..9b01783 --- /dev/null +++ b/components/topups-table.tsx @@ -0,0 +1,235 @@ +import { Calendar } from "lucide-react"; +import Link from "next/link"; +import { redirect } from "next/navigation"; +import { getTopups } from "@/actions/payment"; +import { + Table, + TableBody, + TableCaption, + TableCell, + TableFooter, + TableHead, + TableHeader, + TableRow, +} from "@/components/ui/table"; +import type { Topup } from "@/lib/backend-types"; +import { cn } from "@/lib/utils"; +import { tryCatch } from "@/utils/tryCatch"; +import Pagination from "./pagination"; +import { Badge } from "./ui/badge"; +import { Button } from "./ui/button"; + +export async function TopupsTable({ + searchParams, +}: { + searchParams: Promise<{ + [key: string]: unknown; + }>; +}) { + const resolvedParams = await searchParams; + + // Build params object + const apiParams: Record = {}; + for (const [key, value] of Object.entries(resolvedParams)) { + if (value !== undefined && value !== "") { + apiParams[key] = typeof value === "number" ? value : String(value); + } + } + + const [error, topups] = await tryCatch(getTopups(apiParams)); + + if (error) { + if (error.message.includes("Unauthorized")) { + redirect("/auth/signin"); + } else { + return
{JSON.stringify(error, null, 2)}
; + } + } + const { data, meta } = topups; + return ( +
+ {data?.length === 0 ? ( +
+

No topups yet.

+
+ ) : ( + <> +
+ + Table of all topups. + + + Details + Expires at + Expired + Amount + + + + {topups?.data?.map((topup) => ( + + +
+
+ + + {new Date(topup.created_at).toLocaleDateString( + "en-US", + { + month: "short", + day: "2-digit", + year: "numeric", + minute: "2-digit", + hour: "2-digit", + }, + )} + +
+ +
+ + + + {!topup.is_expired && ( + + + {topup.paid ? "Paid" : "Unpaid"} + + )} +
+
+
+ + + {new Date(topup.expires_at).toLocaleDateString("en-US", { + month: "short", + day: "2-digit", + year: "numeric", + minute: "2-digit", + hour: "2-digit", + })} + + + + + {topup.is_expired ? Yes : No} + + + + + {topup.amount.toFixed(2)} + + MVR + +
+ ))} +
+ + + + {meta?.total === 1 ? ( +

+ Total {meta?.total} topup. +

+ ) : ( +

+ Total {meta?.total} topups. +

+ )} +
+
+
+
+ +
+
+ {data.map((topup) => ( + + ))} +
+ + + )} +
+ ); +} + +function MobileTopupDetails({ topup }: { topup: Topup }) { + return ( +
+
+ + + {new Date(topup.created_at).toLocaleDateString("en-US", { + month: "short", + day: "2-digit", + year: "numeric", + })} + +
+ +
+ + + + + {topup.paid ? "Paid" : "Unpaid"} + +
+
+
+

Amount

+ + {topup.amount.toFixed(2)} MVR + +
+
+
+ ); +} diff --git a/components/wallet.tsx b/components/wallet.tsx index bb9526d..d41ec28 100644 --- a/components/wallet.tsx +++ b/components/wallet.tsx @@ -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 ? ( - <> - - + ) : ( <> Go to payment