import { useState } from 'react' import { useQuery } from '@tanstack/react-query' import { instructorListOptions, scheduleSlotListOptions } from '@/api/lessons' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Badge } from '@/components/ui/badge' import type { ScheduleSlot, Instructor } from '@/types/lesson' const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] const HOURS = Array.from({ length: 14 }, (_, i) => i + 7) // 7 AM to 8 PM export function LessonsScheduleView() { const [selectedInstructorId, setSelectedInstructorId] = useState('all') const { data: instructorsData } = useQuery({ ...instructorListOptions({ page: 1, limit: 100, q: undefined, sort: 'name', order: 'asc' }), }) const instructors = (instructorsData?.data ?? []).filter((i: Instructor) => i.isActive !== false) const { data: slotsData } = useQuery({ ...scheduleSlotListOptions( { page: 1, limit: 500, q: undefined, sort: undefined, order: 'asc' }, selectedInstructorId !== 'all' ? { instructorId: selectedInstructorId } : undefined, ), }) const slots = slotsData?.data ?? [] // Group slots by day function getSlotsForDay(dayOfWeek: number) { return slots.filter((s: ScheduleSlot) => s.dayOfWeek === dayOfWeek) } function formatTime(time: string) { const [h, m] = time.split(':').map(Number) const ampm = h >= 12 ? 'PM' : 'AM' const hour = h > 12 ? h - 12 : h === 0 ? 12 : h return `${hour}:${m.toString().padStart(2, '0')} ${ampm}` } return (
{/* Controls */}
Weekly Schedule {slots.length} slots
{/* Weekly grid */}
{/* Header row */}
{DAYS.map((day) => (
{day.slice(0, 3)}
))} {/* Time rows */} {HOURS.map((hour) => ( <>
{hour > 12 ? hour - 12 : hour}{hour >= 12 ? 'p' : 'a'}
{DAYS.map((_, dayIdx) => { const daySlots = getSlotsForDay(dayIdx).filter((s: ScheduleSlot) => { const slotHour = parseInt(s.startTime.split(':')[0]) return slotHour === hour }) return (
{daySlots.map((slot: ScheduleSlot) => { const instructor = instructors.find((i: Instructor) => i.id === slot.instructorId) return (
{formatTime(slot.startTime)}
{instructor &&
{instructor.displayName}
} Open
) })}
) })} ))}
) }