import { useQuery } from "@tanstack/react-query"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { TrendingUp, Package, Truck, Clock } from "lucide-react"; import { useStore } from "@/contexts/StoreContext"; import { useAuthUnified } from "@/hooks/useAuthUnified"; interface StatsPanelProps { currentDate?: Date; } export default function StatsPanel({ currentDate = new Date() }: StatsPanelProps) { const { selectedStoreId } = useStore(); const { user } = useAuthUnified(); const year = currentDate.getFullYear(); const month = currentDate.getMonth() + 1; // Construire l'URL avec les paramètres const statsUrl = `/api/stats/monthly?year=${year}&month=${month}${selectedStoreId ? `&storeId=${selectedStoreId}` : ''}`; const { data: stats, isLoading } = useQuery({ queryKey: [statsUrl, selectedStoreId], // Include selectedStoreId in key to force refetch queryFn: async () => { const response = await fetch(statsUrl, { credentials: 'include' }); if (!response.ok) { throw new Error('Failed to fetch stats'); } return response.json(); }, }); if (isLoading) { return (
{[...Array(4)].map((_, i) => (
))}
); } return ( Statistiques du mois
{Number(stats?.ordersCount) || 0}
Commandes
{Number(stats?.deliveriesCount) || 0}
Livraisons
{Number(stats?.totalPalettes) || 0}
Palettes
{Number(stats?.totalPackages) || 0}
Colis
Délai moyen: {stats?.averageDeliveryTime?.toFixed(1) || '0'} jours
{stats?.pendingOrdersCount > 0 && (
{stats.pendingOrdersCount} commandes en attente
)}
); }