"use client"; import React, { useEffect, useState } from "react"; import { Center, Group, Loader, Paper, ScrollArea, Table, Text, Title } from "@mantine/core"; import { notifications } from "@mantine/notifications"; import { useRouter } from "next/navigation"; import Image from "next/image"; import { getBankLogo } from "@/app/_util/getBankLogo"; import { IconTrash } from "@tabler/icons-react"; interface Beneficiary { accountNo: string; name: string; accountType: string; ifscCode: string; bankName: string; branchName: string; } export default function ViewBeneficiary() { const router = useRouter(); const [authorized, setAuthorized] = useState(null); const [beneficiaries, setBeneficiaries] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { const token = localStorage.getItem("access_token"); if (!token) { setAuthorized(false); router.push("/login"); } else { setAuthorized(true); } }, []); useEffect(() => { const fetchBeneficiaries = async () => { try { const token = localStorage.getItem("access_token"); const response = await fetch(`/api/beneficiary`, { method: "GET", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, }); if (!response.ok) throw new Error("Failed to fetch beneficiaries"); const data = await response.json(); setBeneficiaries(data); } catch (error) { notifications.show({ title: "Error", message: "Unable to fetch beneficiaries. Please try again later.", color: "red", }); } finally { setLoading(false); } }; fetchBeneficiaries(); }, []); if (loading) { return (
); } if (!authorized) return null; return ( My Beneficiaries {beneficiaries.length === 0 ? ( No beneficiaries found. ) : ( Bank Account No Name Type IFSC Action Icon {beneficiaries.map((b, i) => ( {b.bankName} {b.accountNo} {b.name} {b.accountType} {b.ifscCode} ))}
)}
); }