| β Back to README | Documentation Index | Project Structure | API Docs |
This guide explains how the frontend Dashboard connects to the backend API to display real data instead of hardcoded mock values.
βββββββββββββββββββββββββββββββββββββββ
β Frontend (React) β
β Dashboard.tsx β
ββββββββββββββββ¬βββββββββββββββββββββββ
β
Uses hooks (useDashboardData)
β
β
βββββββββββββββββββββββββββββββββββββββ
β React Hooks (useDashboardData) β
β client/hooks/useDashboardData.ts β
ββββββββββββββββ¬βββββββββββββββββββββββ
β
Fetch with Authorization
β
β
βββββββββββββββββββββββββββββββββββββββ
β Backend (Express.js) β
β /api/v1/dashboard/* endpoints β
β server/routes/dashboard.ts β
ββββββββββββββββ¬βββββββββββββββββββββββ
β
Query Database
β
β
βββββββββββββββββββββββββββββββββββββββ
β SQLite Database β
β prisma/dev.db β
βββββββββββββββββββββββββββββββββββββββ
Location: server/routes/dashboard.ts
Endpoints available:
GET /api/v1/dashboard/metrics - Aggregated metricsGET /api/v1/dashboard/chart-data - Chart data (pageviews & visitors)GET /api/v1/dashboard/top-pages - Top pages listGET /api/v1/dashboard/referrers - Referrer sourcesGET /api/v1/dashboard/devices - Device distributionGET /api/v1/dashboard/locations - Geographic dataGET /api/v1/dashboard/all - All data at onceAll endpoints require: Authorization: Bearer {token}
Location: client/hooks/useDashboardData.ts
Available hooks:
useDashboardData(dateRange); // Fetch all data
useDashboardMetrics(dateRange); // Fetch metrics only
useChartData(dateRange, type); // Fetch chart data
useTopPages(); // Fetch top pages
useReferrers(); // Fetch referrers
useDeviceDistribution(); // Fetch device data
useTopLocations(); // Fetch location data
Each hook returns:
{
data: T | null, // The fetched data
loading: boolean, // Loading state
error: string | null // Error message if failed
}
Location: shared/types/dashboard.ts
DashboardMetrics - Metrics interfaceDashboardData - Complete dashboard dataModified: server/index.ts
Dashboard routes mounted at /api/v1/dashboard
export default function Dashboard() {
const [darkMode, setDarkMode] = useState(false)
return (
<MetricCard
title="Page Views"
value="45,231" // β Hardcoded
trend={12}
// ...
/>
)
}
import { useDashboardData } from "@/hooks/useDashboardData"
export default function Dashboard() {
const [darkMode, setDarkMode] = useState(false)
const [dateRange, setDateRange] = useState("7d")
// Fetch data from API
const { data, loading, error } = useDashboardData(dateRange)
if (loading) return <DashboardSkeleton />
if (error) return <ErrorState message={error} />
if (!data) return <EmptyState />
// Format metrics for display
const pageViewsFormatted = Math.floor(data.metrics.pageViews).toLocaleString()
const sessionDurationFormatted = formatSeconds(data.metrics.sessionDuration)
return (
<MetricCard
title="Page Views"
value={pageViewsFormatted} // β From API
trend={Math.round(data.metrics.pageViewsTrend)}
// ...
/>
)
}
import { useDashboardData } from "@/hooks/useDashboardData";
const { data, loading, error } = useDashboardData(dateRange);
if (loading) {
return (
<div className="flex items-center justify-center min-h-screen">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary" />
</div>
)
}
if (error) {
return (
<div className="bg-destructive/10 border border-destructive rounded-lg p-4">
<p className="text-destructive font-medium">Error loading dashboard</p>
<p className="text-sm text-destructive/80">{error}</p>
</div>
)
}
// Before
<MetricCard value="45,231" />
// After
<MetricCard value={Math.floor(data.metrics.pageViews).toLocaleString()} />
All dashboard endpoints require authentication. The hooks automatically:
localStorage.getItem('accessToken')Authorization: Bearer {token} headerMake sure users are logged in before viewing dashboard!
1. Dashboard component renders
2. useDashboardData("7d") hook is called
3. Hook checks for token in localStorage
4. Hook makes GET request to /api/v1/dashboard/all
5. Request includes: Authorization: Bearer {token}
6. Backend receives request
7. Backend authenticates user via authMiddleware
8. Backend queries database for metrics
9. Backend returns JSON with data
10. Hook updates state with data
11. Component re-renders with real metrics
12. User sees actual data
npm run build
npm run start
# Register or login to get access token
curl -X POST http://localhost:3000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"testuser@example.com","password":"SecurePass123"}'
# Copy the accessToken from response
curl http://localhost:3000/api/v1/dashboard/metrics \
-H "Authorization: Bearer YOUR_TOKEN"
Should return:
{
"pageViews": 45231,
"uniqueVisitors": 12847,
"sessionDuration": 222,
"bounceRate": 32.4,
"pageViewsTrend": 12,
...
}
npm run dev
Navigate to /dashboard - should display real data from API.
{
"pageViews": 45231,
"uniqueVisitors": 12847,
"sessionDuration": 222,
"bounceRate": 32.4,
"pageViewsTrend": 12,
"visitorsTrend": 8,
"sessionDurationTrend": -2,
"bounceRateTrend": 5
}
{
"pageViewsChart": {
"data": [
{ "date": "2026-01-22", "value": 4523 },
{ "date": "2026-01-23", "value": 5231 },
{ "date": "2026-01-24", "value": 4891 },
...
]
},
"visitorsChart": {
"data": [
{ "date": "2026-01-22", "value": 1234 },
{ "date": "2026-01-23", "value": 1456 },
...
]
}
}
loading state before renderingerror state gracefullyProblem: Getting 401 Unauthorized Solution: Make sure user is logged in. Token might be expired.
Problem: Dashboard shows no data Solution: Backend might be returning null. Check:
Problem: Canβt reach API Solution: Make sure backend is running on port 3000
Problem: Cross-origin request blocked Solution: CORS middleware is already configured. Check browser console.
server/routes/dashboard.tsclient/hooks/useDashboardData.tsshared/types/dashboard.tsclient/pages/Dashboard.tsxserver/index.tsReady to integrate? Follow the implementation steps above! π