mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-08 01:06:31 +00:00
feat: update cancelTopup API call to use PATCH method and enhance success message with topup details ✨
This commit is contained in:
@ -164,24 +164,16 @@ export async function getTopup({ id }: { id: string }) {
|
||||
export async function cancelTopup({ id }: { id: string }) {
|
||||
const session = await getServerSession(authOptions);
|
||||
const response = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/topup/${id}/delete/`,
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/topup/${id}/cancel/`,
|
||||
{
|
||||
method: "DELETE",
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${session?.apiToken}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!response.ok) {
|
||||
const errorData = (await response.json()) as ApiError;
|
||||
const errorMessage =
|
||||
errorData.message || errorData.detail || "An error occurred.";
|
||||
const error = new Error(errorMessage);
|
||||
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
||||
throw error;
|
||||
}
|
||||
return { message: "Topup successfully canceled." };
|
||||
return handleApiResponse<Topup>(response, "cancelTopup");
|
||||
}
|
||||
|
||||
export async function cancelPayment({ id }: { id: string }) {
|
||||
|
@ -17,13 +17,15 @@ export default function CancelTopupButton({
|
||||
<Button
|
||||
onClick={async () => {
|
||||
setLoading(true);
|
||||
const [error, x] = await tryCatch(cancelTopup({ id: topupId }));
|
||||
console.log(x);
|
||||
const [error, topup] = await tryCatch(cancelTopup({ id: topupId }));
|
||||
if (error) {
|
||||
toast.error(error.message);
|
||||
setLoading(false);
|
||||
} else {
|
||||
toast.success("Topup cancelled successfully!")
|
||||
toast.success("Topup cancelled successfully!", {
|
||||
description: `Your topup of ${topup?.amount} MVR has been cancelled.`,
|
||||
closeButton: true,
|
||||
})
|
||||
router.replace("/top-ups");
|
||||
}
|
||||
}}
|
||||
|
@ -203,16 +203,18 @@ function MobileTopupDetails({ topup }: { topup: Topup }) {
|
||||
View Details
|
||||
</Button>
|
||||
</Link>
|
||||
<Badge
|
||||
className={cn(
|
||||
topup?.paid
|
||||
? "text-green-500 bg-green-500/20"
|
||||
: "text-yellow-500 bg-yellow-500/20",
|
||||
)}
|
||||
variant={topup.paid ? "outline" : "secondary"}
|
||||
>
|
||||
{topup.paid ? "Paid" : "Unpaid"}
|
||||
</Badge>
|
||||
{topup.status !== "CANCELLED" && (
|
||||
<Badge
|
||||
className={cn(
|
||||
topup?.paid
|
||||
? "text-green-500 bg-green-500/20"
|
||||
: "text-yellow-500 bg-yellow-500/20",
|
||||
)}
|
||||
variant={topup.paid ? "outline" : "secondary"}
|
||||
>
|
||||
{topup.paid ? "Paid" : "Unpaid"}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
<div className="bg-white dark:bg-black p-2 rounded mt-2 w-full border">
|
||||
<div className="block sm:hidden">
|
||||
|
Reference in New Issue
Block a user