feat(dashboard): add dashboard with statistics
- Add dashboard stats API endpoint with key metrics - Add stat-card component for displaying metrics - Add occupancy-chart component for court occupancy visualization - Add recent-bookings component for today's bookings list - Add quick-actions component for common admin actions - Update dashboard page with full implementation Stats include: today's bookings, revenue, occupancy rate, active members, pending bookings, and upcoming tournaments. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,306 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useSession } from "next-auth/react";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { formatCurrency, formatDate } from "@/lib/utils";
|
||||||
|
import { StatCard, StatCardSkeleton } from "@/components/dashboard/stat-card";
|
||||||
|
import { OccupancyChart, OccupancyChartSkeleton } from "@/components/dashboard/occupancy-chart";
|
||||||
|
import { RecentBookings, RecentBookingsSkeleton } from "@/components/dashboard/recent-bookings";
|
||||||
|
import { QuickActions } from "@/components/dashboard/quick-actions";
|
||||||
|
|
||||||
|
interface DashboardStats {
|
||||||
|
todayBookings: number;
|
||||||
|
todayRevenue: number;
|
||||||
|
occupancyRate: number;
|
||||||
|
activeMembers: number;
|
||||||
|
pendingBookings: number;
|
||||||
|
upcomingTournaments: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CourtOccupancy {
|
||||||
|
courtId: string;
|
||||||
|
courtName: string;
|
||||||
|
availableHours: number;
|
||||||
|
bookedHours: number;
|
||||||
|
occupancyPercent: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RecentBooking {
|
||||||
|
id: string;
|
||||||
|
startTime: string;
|
||||||
|
endTime: string;
|
||||||
|
status: string;
|
||||||
|
court: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
client: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
} | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DashboardData {
|
||||||
|
stats: DashboardStats;
|
||||||
|
courtOccupancy: CourtOccupancy[];
|
||||||
|
recentBookings: RecentBooking[];
|
||||||
|
date: string;
|
||||||
|
}
|
||||||
|
|
||||||
export default function DashboardPage() {
|
export default function DashboardPage() {
|
||||||
|
const { data: session } = useSession();
|
||||||
|
const [dashboardData, setDashboardData] = useState<DashboardData | null>(null);
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
async function fetchDashboardData() {
|
||||||
|
try {
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
|
||||||
|
const response = await fetch("/api/dashboard/stats");
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Error al cargar los datos del dashboard");
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
setDashboardData(data);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Dashboard fetch error:", err);
|
||||||
|
setError(err instanceof Error ? err.message : "Error desconocido");
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchDashboardData();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const userName = session?.user?.name?.split(" ")[0] || "Usuario";
|
||||||
|
const today = new Date();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div className="space-y-6">
|
||||||
|
{/* Welcome Section */}
|
||||||
|
<div className="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
|
||||||
<div>
|
<div>
|
||||||
<h1 className="text-2xl font-bold text-primary-800">Dashboard</h1>
|
<h1 className="text-2xl font-bold text-primary-800">
|
||||||
<p className="mt-2 text-primary-600">
|
Bienvenido, {userName}
|
||||||
Bienvenido al panel de administración de Padel Pro.
|
</h1>
|
||||||
|
<p className="text-primary-500 mt-1">
|
||||||
|
{formatDate(today)} - Panel de administracion
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
{session?.user?.siteName && (
|
||||||
|
<div className="flex items-center gap-2 px-4 py-2 bg-primary-50 rounded-lg">
|
||||||
|
<svg
|
||||||
|
className="w-5 h-5 text-primary-500"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"
|
||||||
|
/>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
<span className="text-sm font-medium text-primary-700">
|
||||||
|
{session.user.siteName}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Error State */}
|
||||||
|
{error && (
|
||||||
|
<div className="p-4 bg-red-50 border border-red-200 rounded-lg text-red-700">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<svg
|
||||||
|
className="w-5 h-5"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
<span>{error}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Stats Cards Row */}
|
||||||
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
|
||||||
|
{isLoading ? (
|
||||||
|
<>
|
||||||
|
<StatCardSkeleton />
|
||||||
|
<StatCardSkeleton />
|
||||||
|
<StatCardSkeleton />
|
||||||
|
<StatCardSkeleton />
|
||||||
|
</>
|
||||||
|
) : dashboardData ? (
|
||||||
|
<>
|
||||||
|
<StatCard
|
||||||
|
title="Reservas Hoy"
|
||||||
|
value={dashboardData.stats.todayBookings}
|
||||||
|
color="blue"
|
||||||
|
icon={
|
||||||
|
<svg
|
||||||
|
className="w-6 h-6"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
title="Ingresos Hoy"
|
||||||
|
value={formatCurrency(dashboardData.stats.todayRevenue)}
|
||||||
|
color="green"
|
||||||
|
icon={
|
||||||
|
<svg
|
||||||
|
className="w-6 h-6"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
title="Ocupacion"
|
||||||
|
value={`${dashboardData.stats.occupancyRate}%`}
|
||||||
|
color="purple"
|
||||||
|
icon={
|
||||||
|
<svg
|
||||||
|
className="w-6 h-6"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
title="Miembros Activos"
|
||||||
|
value={dashboardData.stats.activeMembers}
|
||||||
|
color="accent"
|
||||||
|
icon={
|
||||||
|
<svg
|
||||||
|
className="w-6 h-6"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Secondary Stats Row */}
|
||||||
|
{!isLoading && dashboardData && (
|
||||||
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||||
|
<StatCard
|
||||||
|
title="Reservas Pendientes"
|
||||||
|
value={dashboardData.stats.pendingBookings}
|
||||||
|
color="orange"
|
||||||
|
icon={
|
||||||
|
<svg
|
||||||
|
className="w-6 h-6"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<StatCard
|
||||||
|
title="Torneos Proximos"
|
||||||
|
value={dashboardData.stats.upcomingTournaments}
|
||||||
|
color="primary"
|
||||||
|
icon={
|
||||||
|
<svg
|
||||||
|
className="w-6 h-6"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M5 3v4M3 5h4M6 17v4m-2-2h4m5-16l2.286 6.857L21 12l-5.714 2.143L13 21l-2.286-6.857L5 12l5.714-2.143L13 3z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Two Column Layout: Occupancy + Recent Bookings */}
|
||||||
|
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
||||||
|
{isLoading ? (
|
||||||
|
<>
|
||||||
|
<OccupancyChartSkeleton />
|
||||||
|
<RecentBookingsSkeleton />
|
||||||
|
</>
|
||||||
|
) : dashboardData ? (
|
||||||
|
<>
|
||||||
|
<OccupancyChart data={dashboardData.courtOccupancy} />
|
||||||
|
<RecentBookings bookings={dashboardData.recentBookings} />
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Quick Actions */}
|
||||||
|
<QuickActions />
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
322
apps/web/app/api/dashboard/stats/route.ts
Normal file
322
apps/web/app/api/dashboard/stats/route.ts
Normal file
@@ -0,0 +1,322 @@
|
|||||||
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
import { getServerSession } from 'next-auth';
|
||||||
|
import { authOptions } from '@/lib/auth';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
|
||||||
|
// GET /api/dashboard/stats - Get dashboard statistics
|
||||||
|
export async function GET(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
const session = await getServerSession(authOptions);
|
||||||
|
|
||||||
|
if (!session?.user) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'No autorizado' },
|
||||||
|
{ status: 401 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const siteId = searchParams.get('siteId') || session.user.siteId;
|
||||||
|
const dateParam = searchParams.get('date');
|
||||||
|
|
||||||
|
// Default to today
|
||||||
|
const targetDate = dateParam ? new Date(dateParam) : new Date();
|
||||||
|
const startOfDay = new Date(targetDate);
|
||||||
|
startOfDay.setHours(0, 0, 0, 0);
|
||||||
|
const endOfDay = new Date(targetDate);
|
||||||
|
endOfDay.setHours(23, 59, 59, 999);
|
||||||
|
|
||||||
|
// Build where clause for site filtering
|
||||||
|
interface SiteFilter {
|
||||||
|
organizationId: string;
|
||||||
|
id?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const siteFilter: SiteFilter = {
|
||||||
|
organizationId: session.user.organizationId,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (siteId) {
|
||||||
|
siteFilter.id = siteId;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get sites matching the filter
|
||||||
|
const sites = await db.site.findMany({
|
||||||
|
where: siteFilter,
|
||||||
|
select: { id: true, openTime: true, closeTime: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
const siteIds = sites.map(s => s.id);
|
||||||
|
|
||||||
|
// 1. Today's bookings count
|
||||||
|
const todayBookings = await db.booking.count({
|
||||||
|
where: {
|
||||||
|
siteId: { in: siteIds },
|
||||||
|
startTime: {
|
||||||
|
gte: startOfDay,
|
||||||
|
lte: endOfDay,
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
in: ['PENDING', 'CONFIRMED', 'COMPLETED'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// 2. Today's revenue (from sales + booking payments)
|
||||||
|
// Get sales from today
|
||||||
|
const todaySales = await db.sale.aggregate({
|
||||||
|
where: {
|
||||||
|
createdBy: {
|
||||||
|
organizationId: session.user.organizationId,
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
gte: startOfDay,
|
||||||
|
lte: endOfDay,
|
||||||
|
},
|
||||||
|
...(siteId
|
||||||
|
? {
|
||||||
|
cashRegister: {
|
||||||
|
siteId,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
},
|
||||||
|
_sum: {
|
||||||
|
total: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get booking payments from today
|
||||||
|
const todayBookingPayments = await db.booking.aggregate({
|
||||||
|
where: {
|
||||||
|
siteId: { in: siteIds },
|
||||||
|
startTime: {
|
||||||
|
gte: startOfDay,
|
||||||
|
lte: endOfDay,
|
||||||
|
},
|
||||||
|
status: 'COMPLETED',
|
||||||
|
},
|
||||||
|
_sum: {
|
||||||
|
paidAmount: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const salesTotal = Number(todaySales._sum.total || 0);
|
||||||
|
const bookingPaymentsTotal = Number(todayBookingPayments._sum.paidAmount || 0);
|
||||||
|
const todayRevenue = salesTotal + bookingPaymentsTotal;
|
||||||
|
|
||||||
|
// 3. Calculate occupancy rate
|
||||||
|
// Get all courts for the sites
|
||||||
|
const courts = await db.court.findMany({
|
||||||
|
where: {
|
||||||
|
siteId: { in: siteIds },
|
||||||
|
isActive: true,
|
||||||
|
status: 'AVAILABLE',
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
site: {
|
||||||
|
select: {
|
||||||
|
openTime: true,
|
||||||
|
closeTime: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Calculate total available hours per court for the day
|
||||||
|
let totalAvailableSlots = 0;
|
||||||
|
let bookedSlots = 0;
|
||||||
|
|
||||||
|
for (const court of courts) {
|
||||||
|
// Parse open/close times (format: "HH:MM")
|
||||||
|
const openTime = court.site.openTime || '08:00';
|
||||||
|
const closeTime = court.site.closeTime || '22:00';
|
||||||
|
|
||||||
|
const [openHour] = openTime.split(':').map(Number);
|
||||||
|
const [closeHour] = closeTime.split(':').map(Number);
|
||||||
|
|
||||||
|
// Each slot is 1 hour
|
||||||
|
const availableHours = closeHour - openHour;
|
||||||
|
totalAvailableSlots += availableHours;
|
||||||
|
|
||||||
|
// Count booked hours for this court today
|
||||||
|
const courtBookings = await db.booking.findMany({
|
||||||
|
where: {
|
||||||
|
courtId: court.id,
|
||||||
|
startTime: {
|
||||||
|
gte: startOfDay,
|
||||||
|
lte: endOfDay,
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
in: ['PENDING', 'CONFIRMED', 'COMPLETED'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
startTime: true,
|
||||||
|
endTime: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Calculate booked hours
|
||||||
|
for (const booking of courtBookings) {
|
||||||
|
const durationMs = booking.endTime.getTime() - booking.startTime.getTime();
|
||||||
|
const durationHours = durationMs / (1000 * 60 * 60);
|
||||||
|
bookedSlots += durationHours;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const occupancyRate = totalAvailableSlots > 0
|
||||||
|
? Math.round((bookedSlots / totalAvailableSlots) * 100)
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
// 4. Active memberships count
|
||||||
|
const now = new Date();
|
||||||
|
const activeMembers = await db.membership.count({
|
||||||
|
where: {
|
||||||
|
status: 'ACTIVE',
|
||||||
|
endDate: {
|
||||||
|
gte: now,
|
||||||
|
},
|
||||||
|
plan: {
|
||||||
|
organizationId: session.user.organizationId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// 5. Pending bookings (awaiting payment/confirmation)
|
||||||
|
const pendingBookings = await db.booking.count({
|
||||||
|
where: {
|
||||||
|
siteId: { in: siteIds },
|
||||||
|
status: 'PENDING',
|
||||||
|
startTime: {
|
||||||
|
gte: startOfDay,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// 6. Upcoming tournaments (next 30 days)
|
||||||
|
const thirtyDaysFromNow = new Date();
|
||||||
|
thirtyDaysFromNow.setDate(thirtyDaysFromNow.getDate() + 30);
|
||||||
|
|
||||||
|
const upcomingTournaments = await db.tournament.count({
|
||||||
|
where: {
|
||||||
|
organizationId: session.user.organizationId,
|
||||||
|
...(siteId ? { siteId } : {}),
|
||||||
|
startDate: {
|
||||||
|
gte: now,
|
||||||
|
lte: thirtyDaysFromNow,
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
in: ['DRAFT', 'REGISTRATION_OPEN', 'REGISTRATION_CLOSED', 'IN_PROGRESS'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get court occupancy details for chart
|
||||||
|
const courtOccupancy = await Promise.all(
|
||||||
|
courts.map(async (court) => {
|
||||||
|
const openTime = court.site.openTime || '08:00';
|
||||||
|
const closeTime = court.site.closeTime || '22:00';
|
||||||
|
|
||||||
|
const [openHour] = openTime.split(':').map(Number);
|
||||||
|
const [closeHour] = closeTime.split(':').map(Number);
|
||||||
|
const availableHours = closeHour - openHour;
|
||||||
|
|
||||||
|
const courtBookings = await db.booking.findMany({
|
||||||
|
where: {
|
||||||
|
courtId: court.id,
|
||||||
|
startTime: {
|
||||||
|
gte: startOfDay,
|
||||||
|
lte: endOfDay,
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
in: ['PENDING', 'CONFIRMED', 'COMPLETED'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
startTime: true,
|
||||||
|
endTime: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
let bookedHours = 0;
|
||||||
|
for (const booking of courtBookings) {
|
||||||
|
const durationMs = booking.endTime.getTime() - booking.startTime.getTime();
|
||||||
|
bookedHours += durationMs / (1000 * 60 * 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
courtId: court.id,
|
||||||
|
courtName: court.name,
|
||||||
|
availableHours,
|
||||||
|
bookedHours: Math.round(bookedHours * 10) / 10,
|
||||||
|
occupancyPercent: availableHours > 0
|
||||||
|
? Math.round((bookedHours / availableHours) * 100)
|
||||||
|
: 0,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get recent bookings for the day
|
||||||
|
const recentBookings = await db.booking.findMany({
|
||||||
|
where: {
|
||||||
|
siteId: { in: siteIds },
|
||||||
|
startTime: {
|
||||||
|
gte: startOfDay,
|
||||||
|
lte: endOfDay,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
court: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
client: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
firstName: true,
|
||||||
|
lastName: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
startTime: 'asc',
|
||||||
|
},
|
||||||
|
take: 10,
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
stats: {
|
||||||
|
todayBookings,
|
||||||
|
todayRevenue,
|
||||||
|
occupancyRate,
|
||||||
|
activeMembers,
|
||||||
|
pendingBookings,
|
||||||
|
upcomingTournaments,
|
||||||
|
},
|
||||||
|
courtOccupancy,
|
||||||
|
recentBookings: recentBookings.map((booking) => ({
|
||||||
|
id: booking.id,
|
||||||
|
startTime: booking.startTime,
|
||||||
|
endTime: booking.endTime,
|
||||||
|
status: booking.status,
|
||||||
|
court: booking.court,
|
||||||
|
client: booking.client
|
||||||
|
? {
|
||||||
|
id: booking.client.id,
|
||||||
|
name: `${booking.client.firstName} ${booking.client.lastName}`,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
})),
|
||||||
|
date: targetDate.toISOString().split('T')[0],
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching dashboard stats:', error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'Error al obtener estadísticas del dashboard' },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
4
apps/web/components/dashboard/index.ts
Normal file
4
apps/web/components/dashboard/index.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
export { StatCard, StatCardSkeleton } from './stat-card';
|
||||||
|
export { OccupancyChart, OccupancyChartSkeleton } from './occupancy-chart';
|
||||||
|
export { RecentBookings, RecentBookingsSkeleton } from './recent-bookings';
|
||||||
|
export { QuickActions } from './quick-actions';
|
||||||
201
apps/web/components/dashboard/occupancy-chart.tsx
Normal file
201
apps/web/components/dashboard/occupancy-chart.tsx
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
|
interface CourtOccupancy {
|
||||||
|
courtId: string;
|
||||||
|
courtName: string;
|
||||||
|
availableHours: number;
|
||||||
|
bookedHours: number;
|
||||||
|
occupancyPercent: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OccupancyChartProps {
|
||||||
|
data: CourtOccupancy[];
|
||||||
|
isLoading?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OccupancyChart({ data, isLoading = false }: OccupancyChartProps) {
|
||||||
|
if (isLoading) {
|
||||||
|
return <OccupancyChartSkeleton />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.length === 0) {
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader className="pb-2">
|
||||||
|
<CardTitle className="text-lg flex items-center gap-2">
|
||||||
|
<svg
|
||||||
|
className="w-5 h-5 text-primary-500"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
Ocupacion de Canchas
|
||||||
|
</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<div className="flex flex-col items-center justify-center py-8 text-primary-500">
|
||||||
|
<svg
|
||||||
|
className="w-12 h-12 mb-2 opacity-50"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M20 12H4M12 20V4"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
<p className="text-sm">No hay canchas configuradas</p>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate overall occupancy
|
||||||
|
const totalAvailable = data.reduce((sum, c) => sum + c.availableHours, 0);
|
||||||
|
const totalBooked = data.reduce((sum, c) => sum + c.bookedHours, 0);
|
||||||
|
const overallOccupancy = totalAvailable > 0
|
||||||
|
? Math.round((totalBooked / totalAvailable) * 100)
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader className="pb-2">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<CardTitle className="text-lg flex items-center gap-2">
|
||||||
|
<svg
|
||||||
|
className="w-5 h-5 text-primary-500"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
Ocupacion de Canchas
|
||||||
|
</CardTitle>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
"text-2xl font-bold",
|
||||||
|
overallOccupancy >= 80
|
||||||
|
? "text-green-600"
|
||||||
|
: overallOccupancy >= 50
|
||||||
|
? "text-blue-600"
|
||||||
|
: "text-primary-600"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{overallOccupancy}%
|
||||||
|
</span>
|
||||||
|
<span className="text-sm text-primary-500">total</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<div className="space-y-4">
|
||||||
|
{data.map((court) => (
|
||||||
|
<div key={court.courtId}>
|
||||||
|
<div className="flex items-center justify-between mb-1">
|
||||||
|
<span className="text-sm font-medium text-primary-700">
|
||||||
|
{court.courtName}
|
||||||
|
</span>
|
||||||
|
<span className="text-sm text-primary-500">
|
||||||
|
{court.bookedHours}h / {court.availableHours}h
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="relative h-4 bg-gray-100 rounded-full overflow-hidden">
|
||||||
|
{/* Available bar (background) */}
|
||||||
|
<div className="absolute inset-0 bg-green-100"></div>
|
||||||
|
{/* Booked bar */}
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
"absolute inset-y-0 left-0 rounded-full transition-all duration-500",
|
||||||
|
court.occupancyPercent >= 80
|
||||||
|
? "bg-blue-500"
|
||||||
|
: court.occupancyPercent >= 50
|
||||||
|
? "bg-blue-400"
|
||||||
|
: "bg-blue-300"
|
||||||
|
)}
|
||||||
|
style={{ width: `${court.occupancyPercent}%` }}
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center justify-between mt-1">
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
"text-xs font-medium",
|
||||||
|
court.occupancyPercent >= 80
|
||||||
|
? "text-blue-600"
|
||||||
|
: court.occupancyPercent >= 50
|
||||||
|
? "text-blue-500"
|
||||||
|
: "text-primary-500"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{court.occupancyPercent}% ocupado
|
||||||
|
</span>
|
||||||
|
<span className="text-xs text-green-600">
|
||||||
|
{court.availableHours - court.bookedHours}h disponible
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Legend */}
|
||||||
|
<div className="flex items-center justify-center gap-6 mt-6 pt-4 border-t border-primary-100">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<div className="w-3 h-3 rounded-full bg-blue-400"></div>
|
||||||
|
<span className="text-xs text-primary-500">Ocupado</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<div className="w-3 h-3 rounded-full bg-green-100"></div>
|
||||||
|
<span className="text-xs text-primary-500">Disponible</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loading skeleton
|
||||||
|
export function OccupancyChartSkeleton() {
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader className="pb-2">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div className="h-6 bg-primary-100 rounded w-40 animate-pulse"></div>
|
||||||
|
<div className="h-8 bg-primary-100 rounded w-16 animate-pulse"></div>
|
||||||
|
</div>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<div className="space-y-4">
|
||||||
|
{[1, 2, 3].map((i) => (
|
||||||
|
<div key={i} className="animate-pulse">
|
||||||
|
<div className="flex items-center justify-between mb-1">
|
||||||
|
<div className="h-4 bg-primary-100 rounded w-24"></div>
|
||||||
|
<div className="h-4 bg-primary-100 rounded w-16"></div>
|
||||||
|
</div>
|
||||||
|
<div className="h-4 bg-primary-100 rounded-full"></div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
151
apps/web/components/dashboard/quick-actions.tsx
Normal file
151
apps/web/components/dashboard/quick-actions.tsx
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import Link from "next/link";
|
||||||
|
|
||||||
|
interface QuickAction {
|
||||||
|
label: string;
|
||||||
|
href: string;
|
||||||
|
icon: React.ReactNode;
|
||||||
|
color: string;
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const quickActions: QuickAction[] = [
|
||||||
|
{
|
||||||
|
label: "Nueva Reserva",
|
||||||
|
href: "/bookings",
|
||||||
|
icon: (
|
||||||
|
<svg
|
||||||
|
className="w-6 h-6"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
),
|
||||||
|
color: "bg-blue-500 hover:bg-blue-600",
|
||||||
|
description: "Crear una nueva reserva de cancha",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: "Abrir Caja",
|
||||||
|
href: "/pos",
|
||||||
|
icon: (
|
||||||
|
<svg
|
||||||
|
className="w-6 h-6"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M17 9V7a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2m2 4h10a2 2 0 002-2v-6a2 2 0 00-2-2H9a2 2 0 00-2 2v6a2 2 0 002 2zm7-5a2 2 0 11-4 0 2 2 0 014 0z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
),
|
||||||
|
color: "bg-green-500 hover:bg-green-600",
|
||||||
|
description: "Iniciar turno de caja registradora",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: "Nueva Venta",
|
||||||
|
href: "/pos",
|
||||||
|
icon: (
|
||||||
|
<svg
|
||||||
|
className="w-6 h-6"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M16 11V7a4 4 0 00-8 0v4M5 9h14l1 12H4L5 9z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
),
|
||||||
|
color: "bg-purple-500 hover:bg-purple-600",
|
||||||
|
description: "Registrar venta en el punto de venta",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: "Registrar Cliente",
|
||||||
|
href: "/clients",
|
||||||
|
icon: (
|
||||||
|
<svg
|
||||||
|
className="w-6 h-6"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M18 9v3m0 0v3m0-3h3m-3 0h-3m-2-5a4 4 0 11-8 0 4 4 0 018 0zM3 20a6 6 0 0112 0v1H3v-1z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
),
|
||||||
|
color: "bg-orange-500 hover:bg-orange-600",
|
||||||
|
description: "Agregar un nuevo cliente al sistema",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export function QuickActions() {
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader className="pb-2">
|
||||||
|
<CardTitle className="text-lg flex items-center gap-2">
|
||||||
|
<svg
|
||||||
|
className="w-5 h-5 text-primary-500"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M13 10V3L4 14h7v7l9-11h-7z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
Acciones Rapidas
|
||||||
|
</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
|
||||||
|
{quickActions.map((action) => (
|
||||||
|
<Link key={action.label} href={action.href}>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
className="w-full h-auto flex flex-col items-center gap-3 p-4 hover:bg-primary-50 border border-primary-100 rounded-lg transition-all hover:shadow-md group"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className={`w-12 h-12 rounded-full flex items-center justify-center text-white transition-transform group-hover:scale-110 ${action.color}`}
|
||||||
|
>
|
||||||
|
{action.icon}
|
||||||
|
</div>
|
||||||
|
<div className="text-center">
|
||||||
|
<p className="font-medium text-primary-800 text-sm">
|
||||||
|
{action.label}
|
||||||
|
</p>
|
||||||
|
<p className="text-xs text-primary-500 mt-1 line-clamp-2">
|
||||||
|
{action.description}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</Button>
|
||||||
|
</Link>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
201
apps/web/components/dashboard/recent-bookings.tsx
Normal file
201
apps/web/components/dashboard/recent-bookings.tsx
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { cn, formatTime } from "@/lib/utils";
|
||||||
|
import Link from "next/link";
|
||||||
|
|
||||||
|
interface Booking {
|
||||||
|
id: string;
|
||||||
|
startTime: string;
|
||||||
|
endTime: string;
|
||||||
|
status: string;
|
||||||
|
court: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
client: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
} | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RecentBookingsProps {
|
||||||
|
bookings: Booking[];
|
||||||
|
isLoading?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const statusConfig: Record<string, { label: string; className: string }> = {
|
||||||
|
PENDING: {
|
||||||
|
label: "Pendiente",
|
||||||
|
className: "bg-yellow-100 text-yellow-700",
|
||||||
|
},
|
||||||
|
CONFIRMED: {
|
||||||
|
label: "Confirmada",
|
||||||
|
className: "bg-blue-100 text-blue-700",
|
||||||
|
},
|
||||||
|
COMPLETED: {
|
||||||
|
label: "Completada",
|
||||||
|
className: "bg-green-100 text-green-700",
|
||||||
|
},
|
||||||
|
CANCELLED: {
|
||||||
|
label: "Cancelada",
|
||||||
|
className: "bg-red-100 text-red-700",
|
||||||
|
},
|
||||||
|
NO_SHOW: {
|
||||||
|
label: "No asistio",
|
||||||
|
className: "bg-gray-100 text-gray-700",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export function RecentBookings({ bookings, isLoading = false }: RecentBookingsProps) {
|
||||||
|
if (isLoading) {
|
||||||
|
return <RecentBookingsSkeleton />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader className="pb-2">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<CardTitle className="text-lg flex items-center gap-2">
|
||||||
|
<svg
|
||||||
|
className="w-5 h-5 text-primary-500"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
Reservas de Hoy
|
||||||
|
</CardTitle>
|
||||||
|
<Link href="/bookings">
|
||||||
|
<Button variant="ghost" size="sm" className="text-sm">
|
||||||
|
Ver todas
|
||||||
|
<svg
|
||||||
|
className="w-4 h-4 ml-1"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M9 5l7 7-7 7"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
</Button>
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
{bookings.length === 0 ? (
|
||||||
|
<div className="flex flex-col items-center justify-center py-8 text-primary-500">
|
||||||
|
<svg
|
||||||
|
className="w-12 h-12 mb-2 opacity-50"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
<p className="text-sm">No hay reservas para hoy</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-3">
|
||||||
|
{bookings.map((booking) => {
|
||||||
|
const status = statusConfig[booking.status] || statusConfig.PENDING;
|
||||||
|
const startTime = new Date(booking.startTime);
|
||||||
|
const endTime = new Date(booking.endTime);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={booking.id}
|
||||||
|
className="flex items-center gap-4 p-3 rounded-lg bg-primary-50 hover:bg-primary-100 transition-colors"
|
||||||
|
>
|
||||||
|
{/* Time */}
|
||||||
|
<div className="flex-shrink-0 text-center min-w-[70px]">
|
||||||
|
<p className="text-sm font-semibold text-primary-800">
|
||||||
|
{formatTime(startTime)}
|
||||||
|
</p>
|
||||||
|
<p className="text-xs text-primary-500">
|
||||||
|
- {formatTime(endTime)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Divider */}
|
||||||
|
<div className="w-px h-10 bg-primary-200"></div>
|
||||||
|
|
||||||
|
{/* Details */}
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<p className="text-sm font-medium text-primary-800 truncate">
|
||||||
|
{booking.client?.name || "Sin cliente"}
|
||||||
|
</p>
|
||||||
|
<p className="text-xs text-primary-500 truncate">
|
||||||
|
{booking.court.name}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Status badge */}
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
"flex-shrink-0 px-2 py-1 text-xs font-medium rounded-full",
|
||||||
|
status.className
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{status.label}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loading skeleton
|
||||||
|
export function RecentBookingsSkeleton() {
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader className="pb-2">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div className="h-6 bg-primary-100 rounded w-36 animate-pulse"></div>
|
||||||
|
<div className="h-8 bg-primary-100 rounded w-20 animate-pulse"></div>
|
||||||
|
</div>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<div className="space-y-3">
|
||||||
|
{[1, 2, 3, 4, 5].map((i) => (
|
||||||
|
<div
|
||||||
|
key={i}
|
||||||
|
className="flex items-center gap-4 p-3 rounded-lg bg-primary-50 animate-pulse"
|
||||||
|
>
|
||||||
|
<div className="flex-shrink-0 text-center min-w-[70px]">
|
||||||
|
<div className="h-4 bg-primary-100 rounded w-16 mb-1"></div>
|
||||||
|
<div className="h-3 bg-primary-100 rounded w-12 mx-auto"></div>
|
||||||
|
</div>
|
||||||
|
<div className="w-px h-10 bg-primary-200"></div>
|
||||||
|
<div className="flex-1">
|
||||||
|
<div className="h-4 bg-primary-100 rounded w-32 mb-1"></div>
|
||||||
|
<div className="h-3 bg-primary-100 rounded w-20"></div>
|
||||||
|
</div>
|
||||||
|
<div className="h-6 bg-primary-100 rounded-full w-20"></div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
134
apps/web/components/dashboard/stat-card.tsx
Normal file
134
apps/web/components/dashboard/stat-card.tsx
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Card, CardContent } from "@/components/ui/card";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
import { ReactNode } from "react";
|
||||||
|
|
||||||
|
interface StatCardProps {
|
||||||
|
title: string;
|
||||||
|
value: string | number;
|
||||||
|
icon: ReactNode;
|
||||||
|
trend?: {
|
||||||
|
value: number;
|
||||||
|
isPositive: boolean;
|
||||||
|
};
|
||||||
|
color?: "primary" | "accent" | "green" | "blue" | "purple" | "orange";
|
||||||
|
}
|
||||||
|
|
||||||
|
const colorVariants = {
|
||||||
|
primary: {
|
||||||
|
bg: "bg-primary-50",
|
||||||
|
icon: "bg-primary-100 text-primary-600",
|
||||||
|
text: "text-primary-700",
|
||||||
|
},
|
||||||
|
accent: {
|
||||||
|
bg: "bg-accent-50",
|
||||||
|
icon: "bg-accent-100 text-accent-600",
|
||||||
|
text: "text-accent-700",
|
||||||
|
},
|
||||||
|
green: {
|
||||||
|
bg: "bg-green-50",
|
||||||
|
icon: "bg-green-100 text-green-600",
|
||||||
|
text: "text-green-700",
|
||||||
|
},
|
||||||
|
blue: {
|
||||||
|
bg: "bg-blue-50",
|
||||||
|
icon: "bg-blue-100 text-blue-600",
|
||||||
|
text: "text-blue-700",
|
||||||
|
},
|
||||||
|
purple: {
|
||||||
|
bg: "bg-purple-50",
|
||||||
|
icon: "bg-purple-100 text-purple-600",
|
||||||
|
text: "text-purple-700",
|
||||||
|
},
|
||||||
|
orange: {
|
||||||
|
bg: "bg-orange-50",
|
||||||
|
icon: "bg-orange-100 text-orange-600",
|
||||||
|
text: "text-orange-700",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export function StatCard({ title, value, icon, trend, color = "primary" }: StatCardProps) {
|
||||||
|
const colors = colorVariants[color];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card className="hover:shadow-md transition-shadow">
|
||||||
|
<CardContent className="p-6">
|
||||||
|
<div className="flex items-start justify-between">
|
||||||
|
<div className="flex-1">
|
||||||
|
<p className="text-sm font-medium text-primary-500 mb-1">{title}</p>
|
||||||
|
<p className="text-3xl font-bold text-primary-800">{value}</p>
|
||||||
|
{trend && (
|
||||||
|
<div className="flex items-center mt-2">
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
"text-sm font-medium flex items-center gap-1",
|
||||||
|
trend.isPositive ? "text-green-600" : "text-red-600"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{trend.isPositive ? (
|
||||||
|
<svg
|
||||||
|
className="w-4 h-4"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M5 10l7-7m0 0l7 7m-7-7v18"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
) : (
|
||||||
|
<svg
|
||||||
|
className="w-4 h-4"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M19 14l-7 7m0 0l-7-7m7 7V3"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
)}
|
||||||
|
{trend.isPositive ? "+" : ""}
|
||||||
|
{trend.value}%
|
||||||
|
</span>
|
||||||
|
<span className="text-xs text-primary-400 ml-1">vs ayer</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
"flex-shrink-0 w-12 h-12 rounded-lg flex items-center justify-center",
|
||||||
|
colors.icon
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{icon}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loading skeleton for stat card
|
||||||
|
export function StatCardSkeleton() {
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardContent className="p-6">
|
||||||
|
<div className="flex items-start justify-between animate-pulse">
|
||||||
|
<div className="flex-1">
|
||||||
|
<div className="h-4 bg-primary-100 rounded w-24 mb-2"></div>
|
||||||
|
<div className="h-8 bg-primary-100 rounded w-16"></div>
|
||||||
|
</div>
|
||||||
|
<div className="w-12 h-12 bg-primary-100 rounded-lg"></div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user