import { useEffect, useState, useCallback } from "react"; import { getUsers, removeUser, getTransactions, removeTransaction, getProducts, removeProduct, getCategories, removeCategory, } from "../api/admin"; import { MdDelete } from "react-icons/md"; import { IoAddCircleSharp } from "react-icons/io5"; import { FaHome } from "react-icons/fa"; import Pagination from "../components/Pagination"; import CategoryForm from "../components/CategoryForm"; import DashboardNav from "../components/DashboardNav"; import { useNavigate } from "react-router-dom"; // Spinner Component const Spinner = () => (
); // Empty State Component const EmptyState = () => (

No data found

No records are currently available.

); // Generic Dashboard Component const Dashboard = ({ fetchDataFn, deleteFn, columns, idKey, refreshKey = 0, headerAction = null, }) => { const navigate = useNavigate(); const [items, setItems] = useState([]); const [total, setTotal] = useState(0); const [currentPage, setCurrentPage] = useState(1); const [loading, setLoading] = useState(true); const pageLimit = 10; const fetchItems = useCallback( (page = 1, limit = 10) => { setLoading(true); fetchDataFn(page, limit) .then((res) => { const data = res.users || res.products || res.transactions || res.data || []; setItems(data); setTotal(res.total); }) .catch((error) => { console.error("Error fetching data:", error); setItems([]); setTotal(0); }) .finally(() => setLoading(false)); }, [fetchDataFn], ); const handleRemove = (id) => { if (window.confirm("Are you sure you want to delete this item?")) { setLoading(true); deleteFn(id) .then(() => fetchItems(currentPage)) .catch((error) => { console.error("Error removing item:", error); setLoading(false); }); } }; useEffect(() => { fetchItems(currentPage); }, [fetchItems, currentPage, refreshKey]); const onChangePage = (page) => { setCurrentPage(page); }; if (loading) return ; return (

Total: {total}

{headerAction &&
{headerAction}
}
{items.length > 0 ? (
{columns.map((col) => ( ))} {items.map((item) => ( {columns.map((col) => ( ))} ))}
{col.label} Action
{item[col.key] || "—"}
) : ( )} {total > pageLimit && (
)}
); }; // Main Admin Tabs export default function AdminDashboardTabs() { const [activeTab, setActiveTab] = useState(0); const [tabData, setTabData] = useState({ users: { loaded: false, data: null }, products: { loaded: false, data: null }, transactions: { loaded: false, data: null }, categories: { loaded: false, data: null }, }); const [visible, setVisible] = useState(false); const [categoryRefreshKey, setCategoryRefreshKey] = useState(0); const toggleForm = () => setVisible((v) => !v); // Preload all tab data useEffect(() => { const tabKeys = ["users", "products", "transactions", "categories"]; const loadTabData = async (index) => { if (index === tabKeys.length) return; setTabData((prev) => ({ ...prev, [tabKeys[index]]: { ...prev[tabKeys[index]], loaded: true }, })); // Load next tab after a short delay setTimeout(() => loadTabData(index + 1), 100); }; loadTabData(0); }, []); const tabs = [ { title: "Users", icon: "👥", key: "users", component: () => ( ), }, { title: "Products", icon: "📦", key: "products", component: () => ( ), }, { title: "Transactions", icon: "💰", key: "transactions", component: () => ( ), }, { title: "Categories", icon: "🏷️", key: "categories", component: () => ( <> Add Category } /> { setCategoryRefreshKey((prev) => prev + 1); toggleForm(); }} /> ), }, ]; return (

Admin Dashboard

{/* Mobile Tabs */}
{/* Desktop Tabs */}
{tabs.map((tab, index) => ( ))}
{tabData[tabs[activeTab].key].loaded && tabs[activeTab].component()}
); }