import { useEffect, useState } from "react"; import { FaCheck } from "react-icons/fa"; import { PiDotsThreeOutlineVerticalFill } from "react-icons/pi"; import { FaDatabase } from "react-icons/fa"; import AccountNav from "./AccountNav"; import api from "../../config/api"; import { AccountData, AccountStatus, UserRole } from "../../utlity/types"; import { useNavigate } from "react-router-dom"; import Cards from "../home/Cards"; import AddAccountModal from "./AddAccountModal"; import { Bounce, toast } from "react-toastify"; import Loader from "../../helper/Loader"; // import { UserRole, AccountStatus, AccountData } from "../../types/account"; const roleColors: Record = { [UserRole.OWNER]: "bg-purple-100 text-purple-700", [UserRole.ADMIN]: "bg-blue-100 text-blue-700", [UserRole.EDITOR]: "bg-green-100 text-green-700", [UserRole.VIEWER]: "bg-gray-100 text-gray-600", }; const getStatusLabel = ( status: AccountStatus, role: UserRole ): { text: string; color: string } => { if (status === AccountStatus.ACTIVE && role !== UserRole.OWNER) return { text: "Accepted", color: "text-green-600 bg-green-100" }; if (status === AccountStatus.REJECTED) return { text: "Rejected", color: "text-red-600 bg-red-100" }; if (status === AccountStatus.INVITED) return { text: "Pending", color: "text-yellow-600 bg-yellow-100" }; return { text: "Active", color: "text-blue-600 bg-blue-100" }; }; const Accounts = () => { const [allAccounts, setAllAccounts] = useState([]); const [selectedAccounts, setSelectedAccounts] = useState([]); const [isModalOpen, setIsModalOpen] = useState(false); const [accounts, setAccounts] = useState([]); const [openMenuId, setOpenMenuId] = useState(null); const handleOpenModal = () => { setIsModalOpen(true); }; const navigate = useNavigate(); const fetchAccounts = async () => { try { const response = await api("/api/user/accounts"); const data: AccountData[] = response.data || []; setAllAccounts(data); } catch (error) { console.error("Error fetching accounts:", error); } }; useEffect(() => { fetchAccounts(); }, []); const handleSelect = (id: number) => { navigate(`/accounts/${id}/transactions`); }; const handleAddAccount = async (accountName: string) => { try { const payloadAccount = { name: accountName } let res = await api.post(`/api/user/accounts`, payloadAccount) if (res?.data) { toast.success("Account Add Successfully...!", { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: false, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", transition: Bounce, }); await fetchAccounts() setIsModalOpen(false); } } catch (error) { console.log(error) } finally { } }; const handleMenuToggle = (accountId: string) => { setOpenMenuId(openMenuId === accountId ? null : accountId); }; const handleAction = async (accountId: string, action: 'accept' | 'reject') => { try { let response = await api.post(`/api/accounts/${accountId}/accept/invite`) console.log(response) if (response.data.status === 202) { toast.success("Login Successful", { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: false, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", transition: Bounce, }); } } catch (error) { console.error('Error posting data:', error); } finally { setOpenMenuId(null); // close menu } }; return ( <> {allAccounts.length > 0 && (
{/* */} {allAccounts.map((account) => { const status = getStatusLabel(account.status, account.role); return ( handleSelect(account.accountId)} className="hover:bg-gray-50 cursor-pointer" > {/* */} ); })}
Account Name Account ID Role Status
{selectedAccounts.includes(account.accountId) && ( )}
{account.accountName}
{account.accountId} {account.role} {status.text} {/* Dropdown menu */} {openMenuId === account.accountId && (
e.stopPropagation()} // prevent row click closing >
)}
)} {isModalOpen && setIsModalOpen(false)} onAddAccount={handleAddAccount} />} ); }; export default Accounts;