mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-10 17:16:33 +00:00
refactor: improve state management and layout in Topup components 🔨
Some checks are pending
Build and Push Docker Images / Build and Push Docker Images (push) Has started running
Some checks are pending
Build and Push Docker Images / Build and Push Docker Images (push) Has started running
This commit is contained in:
@ -20,14 +20,13 @@ export default async function TopupPage({
|
|||||||
return <ClientErrorMessage message={error.message} />;
|
return <ClientErrorMessage message={error.message} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className="flex justify-between items-center border rounded-md border-dashed font-bold title-bg py-4 px-4 mb-4 mx-2">
|
<div className="flex justify-between items-center border rounded-md border-dashed font-bold title-bg py-4 px-4 mb-4 mx-2">
|
||||||
<h3 className="text-sarLinkOrange text-2xl">Topup</h3>
|
<h3 className="text-sarLinkOrange text-2xl">Topup</h3>
|
||||||
<div className="flex flex-col gap-4 items-end w-full">
|
<div className="flex flex-col gap-4 items-end w-full">
|
||||||
{!topup.is_expired && (
|
{!topup.is_expired ||
|
||||||
|
(topup.status !== "PENDING" && (
|
||||||
<Button
|
<Button
|
||||||
disabled
|
disabled
|
||||||
className={cn(
|
className={cn(
|
||||||
@ -37,36 +36,46 @@ export default async function TopupPage({
|
|||||||
// : "text-inherit bg-yellow-400",
|
// : "text-inherit bg-yellow-400",
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{topup?.paid ? <span>Paid</span> : <TextShimmer>Payment Pending</TextShimmer>}
|
{topup?.paid ? <span>Paid</span> : ""}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
{topup.status === "PENDING" && !topup.is_expired && (
|
||||||
|
<Button>
|
||||||
|
<TextShimmer>Payment Pending</TextShimmer>{" "}
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{!topup.paid && (
|
{!topup.paid &&
|
||||||
topup.is_expired ? (
|
(topup.is_expired ? (
|
||||||
<Button
|
<Button
|
||||||
disabled
|
disabled
|
||||||
className="rounded-md opacity-100! uppercase font-semibold text-red-500 bg-red-500/20"
|
className="rounded-md opacity-100! uppercase font-semibold text-red-500 bg-red-500/20"
|
||||||
>
|
>
|
||||||
Topup Expired
|
Topup Expired
|
||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : topup.status === "PENDING" ? (
|
||||||
<CancelTopupButton topupId={topupId} />
|
<CancelTopupButton topupId={topupId} />
|
||||||
)
|
) : topup.status === "CANCELLED" ? (
|
||||||
)}
|
<Button disabled>Topup Cancelled</Button>
|
||||||
|
) : (
|
||||||
|
""
|
||||||
|
))}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{!topup.paid && (
|
{
|
||||||
|
(!topup.paid && topup.status === "PENDING" && !topup.is_expired) && (
|
||||||
<ExpiryCountDown expiryLabel="Top up" expiresAt={topup.expires_at} />
|
<ExpiryCountDown expiryLabel="Top up" expiresAt={topup.expires_at} />
|
||||||
)}
|
)
|
||||||
|
}
|
||||||
<div
|
<div
|
||||||
id="user-topup-details"
|
id="user-topup-details"
|
||||||
className="pb-4 gap-4 flex sm:flex-row flex-col items-start justify-start"
|
className="pb-4 gap-4 flex sm:flex-row flex-col items-start justify-start"
|
||||||
>
|
>
|
||||||
<TopupToPay
|
<TopupToPay
|
||||||
disabled={topup.paid || topup.is_expired}
|
disabled={topup.paid || topup.is_expired || topup.status === "CANCELLED"}
|
||||||
topup={topup || undefined}
|
topup={topup || undefined}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div >
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,10 @@ import {
|
|||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useActionState, useEffect, useState } from "react";
|
import { useActionState, useEffect, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { type VerifyTopupPaymentState, verifyTopupPayment } from "@/actions/payment";
|
import {
|
||||||
|
type VerifyTopupPaymentState,
|
||||||
|
verifyTopupPayment,
|
||||||
|
} from "@/actions/payment";
|
||||||
import {
|
import {
|
||||||
Table,
|
Table,
|
||||||
TableBody,
|
TableBody,
|
||||||
@ -19,15 +22,22 @@ import {
|
|||||||
import type { Topup } from "@/lib/backend-types";
|
import type { Topup } from "@/lib/backend-types";
|
||||||
import { Button } from "./ui/button";
|
import { Button } from "./ui/button";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const initialState: VerifyTopupPaymentState = {
|
const initialState: VerifyTopupPaymentState = {
|
||||||
message: "",
|
message: "",
|
||||||
success: false,
|
success: false,
|
||||||
fieldErrors: {},
|
fieldErrors: {},
|
||||||
};
|
};
|
||||||
export default function TopupToPay({ topup, disabled }: { topup?: Topup, disabled?: boolean }) {
|
export default function TopupToPay({
|
||||||
const [state, formAction, isPending] = useActionState(verifyTopupPayment, initialState);
|
topup,
|
||||||
|
disabled,
|
||||||
|
}: {
|
||||||
|
topup?: Topup;
|
||||||
|
disabled?: boolean;
|
||||||
|
}) {
|
||||||
|
const [state, formAction, isPending] = useActionState(
|
||||||
|
verifyTopupPayment,
|
||||||
|
initialState,
|
||||||
|
);
|
||||||
|
|
||||||
// Handle toast notifications based on state changes
|
// Handle toast notifications based on state changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -38,7 +48,11 @@ export default function TopupToPay({ topup, disabled }: { topup?: Topup, disable
|
|||||||
? `Your topup payment has been verified successfully using ${state.transaction.sourceBank} bank transfer on ${state.transaction.trxDate}.`
|
? `Your topup payment has been verified successfully using ${state.transaction.sourceBank} bank transfer on ${state.transaction.trxDate}.`
|
||||||
: state.message,
|
: state.message,
|
||||||
});
|
});
|
||||||
} else if (!state.success && state.message && state.message !== initialState.message) {
|
} else if (
|
||||||
|
!state.success &&
|
||||||
|
state.message &&
|
||||||
|
state.message !== initialState.message
|
||||||
|
) {
|
||||||
toast.error("Topup Payment Verification Failed", {
|
toast.error("Topup Payment Verification Failed", {
|
||||||
closeButton: true,
|
closeButton: true,
|
||||||
description: state.message,
|
description: state.message,
|
||||||
@ -69,16 +83,18 @@ export default function TopupToPay({ topup, disabled }: { topup?: Topup, disable
|
|||||||
) : (
|
) : (
|
||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
<form action={formAction}>
|
<form action={formAction}>
|
||||||
<input type="hidden" name="topupId" value={topup?.id ?? ""} />
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="topupId"
|
||||||
|
value={topup?.id ?? ""}
|
||||||
|
/>
|
||||||
<Button
|
<Button
|
||||||
disabled={disabled || isPending}
|
disabled={disabled || isPending}
|
||||||
type="submit"
|
type="submit"
|
||||||
size={"lg"}
|
size={"lg"}
|
||||||
className="mb-4"
|
className="mb-4 w-full"
|
||||||
>
|
>
|
||||||
{isPending
|
{isPending ? "Processing payment..." : "I have paid"}
|
||||||
? "Processing payment..."
|
|
||||||
: "I have paid"}
|
|
||||||
{isPending ? (
|
{isPending ? (
|
||||||
<Loader2 className="animate-spin" />
|
<Loader2 className="animate-spin" />
|
||||||
) : (
|
) : (
|
||||||
@ -86,7 +102,6 @@ export default function TopupToPay({ topup, disabled }: { topup?: Topup, disable
|
|||||||
)}
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
@ -150,19 +165,18 @@ function AccountInfomation({
|
|||||||
}) {
|
}) {
|
||||||
const [accNo, setAccNo] = useState(false);
|
const [accNo, setAccNo] = useState(false);
|
||||||
return (
|
return (
|
||||||
<div className="justify-start items-start border my-4 flex flex-col gap-2 p-2 rounded-md">
|
<div className="justify-center items-center border my-4 flex flex-col gap-2 p-2 rounded-md">
|
||||||
<h6 className="title-bg uppercase p-2 border rounded w-full font-semibold">
|
<h6 className="title-bg uppercase p-2 border rounded w-full font-semibold">
|
||||||
Account Information
|
Account Information
|
||||||
</h6>
|
</h6>
|
||||||
<div className="border justify-start flex flex-col items-start bg-white/10 w-full p-2 rounded">
|
<div className="border justify-center flex flex-col items-center bg-white/10 w-full p-2 rounded">
|
||||||
<div className="text-sm font-semibold">Account Name</div>
|
<div className="text-sm font-semibold">Account Name</div>
|
||||||
<span>{accName}</span>
|
<span>{accName}</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="border flex justify-between items-start gap-2 bg-white/10 w-full p-2 rounded">
|
<div className="border flex justify-between items-center gap-2 w-full p-2 rounded">
|
||||||
<div className="flex flex-col items-start justify-start">
|
<div className="flex flex-col items-center justify-center w-full">
|
||||||
<p className="text-sm font-semibold">Account No</p>
|
<p className="text-sm font-semibold">Account No</p>
|
||||||
<span>{accountNo}</span>
|
<span>{accountNo}</span>
|
||||||
</div>
|
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@ -172,11 +186,23 @@ function AccountInfomation({
|
|||||||
toast.success("Account number copied!");
|
toast.success("Account number copied!");
|
||||||
setAccNo((prev) => !prev);
|
setAccNo((prev) => !prev);
|
||||||
}}
|
}}
|
||||||
variant={"link"}
|
className="mt-2 w-full"
|
||||||
|
variant={"secondary"}
|
||||||
>
|
>
|
||||||
{accNo ? <Clipboard /> : <ClipboardCheck color="green" />}
|
{accNo ? (
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span>Copy Account Number</span>
|
||||||
|
<ClipboardCheck />
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span>Copy Account Number</span>
|
||||||
|
<ClipboardCheck color="green" />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user