From c28876705714002bc28082b52e8d7aa72fcf2fdb Mon Sep 17 00:00:00 2001 From: Tiago Yamamoto Date: Mon, 22 Dec 2025 11:21:56 -0300 Subject: [PATCH] Fix admin dashboard stats loading --- marketplace/src/pages/admin/DashboardHome.tsx | 55 +++++++++++++------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/marketplace/src/pages/admin/DashboardHome.tsx b/marketplace/src/pages/admin/DashboardHome.tsx index 69f6649..8b65714 100644 --- a/marketplace/src/pages/admin/DashboardHome.tsx +++ b/marketplace/src/pages/admin/DashboardHome.tsx @@ -1,4 +1,5 @@ import { useEffect, useState } from 'react' +import { apiClient } from '../../services/apiClient' interface DashboardStats { totalUsers: number @@ -20,30 +21,52 @@ export function DashboardHome() { loadStats() }, []) + const extractCount = (payload: unknown): number => { + if (Array.isArray(payload)) { + return payload.length + } + + if (payload && typeof payload === 'object') { + const record = payload as Record + + if (typeof record.total === 'number') { + return record.total + } + + const possibleCollections = [ + record.items, + record.users, + record.companies, + record.products, + record.orders + ] + + for (const collection of possibleCollections) { + if (Array.isArray(collection)) { + return collection.length + } + } + } + + return 0 + } + const loadStats = async () => { setLoading(true) try { // Load counts from each endpoint const [users, companies, products, orders] = await Promise.all([ - fetch('/api/v1/users?page=1&page_size=1', { - headers: { Authorization: `Bearer ${localStorage.getItem('mp-auth-user') ? JSON.parse(localStorage.getItem('mp-auth-user')!).token : ''}` } - }).then(r => r.json()).catch(() => ({ total: 0 })), - fetch('/api/v1/companies?page=1&page_size=1', { - headers: { Authorization: `Bearer ${localStorage.getItem('mp-auth-user') ? JSON.parse(localStorage.getItem('mp-auth-user')!).token : ''}` } - }).then(r => r.json()).catch(() => ({ total: 0 })), - fetch('/api/v1/products?page=1&page_size=1', { - headers: { Authorization: `Bearer ${localStorage.getItem('mp-auth-user') ? JSON.parse(localStorage.getItem('mp-auth-user')!).token : ''}` } - }).then(r => r.json()).catch(() => ({ total: 0 })), - fetch('/api/v1/orders?page=1&page_size=1', { - headers: { Authorization: `Bearer ${localStorage.getItem('mp-auth-user') ? JSON.parse(localStorage.getItem('mp-auth-user')!).token : ''}` } - }).then(r => r.json()).catch(() => ({ total: 0 })) + apiClient.get('/v1/users?page=1&page_size=1').catch(() => ({ total: 0 })), + apiClient.get('/v1/companies').catch(() => ([])), + apiClient.get('/v1/products').catch(() => ([])), + apiClient.get('/v1/orders').catch(() => ([])) ]) setStats({ - totalUsers: users.total || 0, - totalCompanies: companies.total || 0, - totalProducts: products.total || 0, - totalOrders: orders.total || 0 + totalUsers: extractCount(users), + totalCompanies: extractCount(companies), + totalProducts: extractCount(products), + totalOrders: extractCount(orders) }) } catch (err) { console.error('Error loading stats:', err)