Added routes
This commit is contained in:
117
apps/web/src/features/class-attendance/AttendanceProvider.tsx
Normal file
117
apps/web/src/features/class-attendance/AttendanceProvider.tsx
Normal file
@@ -0,0 +1,117 @@
|
||||
import { createContext, useContext, useEffect, useMemo, useState } from 'react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import type { SessionStudents } from '@gruperly/shared'
|
||||
import { useToast } from '../../components/ui'
|
||||
import { getSessionStudents, markAttendance } from '../../lib/api'
|
||||
import { isAttendanceLocked } from './utils'
|
||||
import type { AttendanceChoice } from './utils'
|
||||
|
||||
type AttendanceContextValue = {
|
||||
sessionId: string
|
||||
sessionInfo: SessionStudents | undefined
|
||||
studentsPending: boolean
|
||||
studentsFailed: boolean
|
||||
retryStudents: () => void
|
||||
startTime: string | null
|
||||
presentCount: number
|
||||
choices: Record<string, AttendanceChoice>
|
||||
choiceFor: (attendeeId: string) => AttendanceChoice
|
||||
toggleStudent: (attendeeId: string) => void
|
||||
saveAttendance: () => void
|
||||
isSaving: boolean
|
||||
}
|
||||
|
||||
const AttendanceContext = createContext<AttendanceContextValue | null>(null)
|
||||
|
||||
export function AttendanceProvider({ sessionId, children }: { sessionId: string; children: ReactNode }) {
|
||||
const queryClient = useQueryClient()
|
||||
const toast = useToast()
|
||||
|
||||
const studentsQuery = useQuery({
|
||||
queryKey: ['class-students', sessionId],
|
||||
queryFn: () => getSessionStudents(sessionId),
|
||||
staleTime: 0,
|
||||
})
|
||||
|
||||
const sessionInfo = studentsQuery.data
|
||||
const [choices, setChoices] = useState<Record<string, AttendanceChoice>>({})
|
||||
|
||||
useEffect(() => {
|
||||
if (!sessionInfo) return
|
||||
const initial: Record<string, AttendanceChoice> = {}
|
||||
for (const student of sessionInfo.students) {
|
||||
if (isAttendanceLocked(student)) continue
|
||||
initial[student.attendeeId] = student.attendanceStatus === 'ABSENT' ? 'ABSENT' : 'PRESENT'
|
||||
}
|
||||
setChoices(initial)
|
||||
}, [sessionInfo])
|
||||
|
||||
const presentCount = useMemo(
|
||||
() =>
|
||||
(sessionInfo?.students ?? []).filter(
|
||||
(student) => !isAttendanceLocked(student) && choices[student.attendeeId] === 'PRESENT',
|
||||
).length,
|
||||
[sessionInfo, choices],
|
||||
)
|
||||
|
||||
const toggleStudent = (attendeeId: string) => {
|
||||
setChoices((prev) => ({
|
||||
...prev,
|
||||
[attendeeId]: prev[attendeeId] === 'ABSENT' ? 'PRESENT' : 'ABSENT',
|
||||
}))
|
||||
}
|
||||
|
||||
const saveMutation = useMutation({
|
||||
mutationFn: () => {
|
||||
const records = (sessionInfo?.students ?? [])
|
||||
.filter((student) => !isAttendanceLocked(student))
|
||||
.map((student) => ({
|
||||
attendeeId: student.attendeeId,
|
||||
status: choices[student.attendeeId] ?? 'PRESENT',
|
||||
}))
|
||||
return markAttendance(sessionId, { classSessionId: sessionId, records })
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Asistencia guardada correctamente.', 'Listo')
|
||||
void queryClient.invalidateQueries({ queryKey: ['classes-today'] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['class-students', sessionId] })
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(error.message || 'No pudimos guardar la asistencia.')
|
||||
},
|
||||
})
|
||||
|
||||
const startTime = sessionInfo
|
||||
? new Date(sessionInfo.startsAt).toLocaleTimeString('es-MX', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hour12: false,
|
||||
})
|
||||
: null
|
||||
|
||||
const value: AttendanceContextValue = {
|
||||
sessionId,
|
||||
sessionInfo,
|
||||
studentsPending: studentsQuery.isPending,
|
||||
studentsFailed: studentsQuery.isError,
|
||||
retryStudents: () => void studentsQuery.refetch(),
|
||||
startTime,
|
||||
presentCount,
|
||||
choices,
|
||||
choiceFor: (attendeeId) => choices[attendeeId] ?? 'PRESENT',
|
||||
toggleStudent,
|
||||
saveAttendance: () => saveMutation.mutate(),
|
||||
isSaving: saveMutation.isPending,
|
||||
}
|
||||
|
||||
return <AttendanceContext.Provider value={value}>{children}</AttendanceContext.Provider>
|
||||
}
|
||||
|
||||
export function useAttendance() {
|
||||
const context = useContext(AttendanceContext)
|
||||
if (!context) {
|
||||
throw new Error('useAttendance debe usarse dentro de <AttendanceProvider>')
|
||||
}
|
||||
return context
|
||||
}
|
||||
45
apps/web/src/features/class-attendance/AttendanceToggle.tsx
Normal file
45
apps/web/src/features/class-attendance/AttendanceToggle.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Check, X } from 'lucide-react'
|
||||
import { cn } from '../../lib/utils'
|
||||
import type { AttendanceChoice } from './utils'
|
||||
|
||||
export function AttendanceToggle({
|
||||
choice,
|
||||
disabled,
|
||||
onToggle,
|
||||
fullName,
|
||||
}: {
|
||||
choice: AttendanceChoice
|
||||
disabled?: boolean
|
||||
onToggle: () => void
|
||||
fullName: string
|
||||
}) {
|
||||
const isPresent = choice === 'PRESENT'
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={isPresent}
|
||||
aria-label={`${isPresent ? 'Presente' : 'Ausente'}: ${fullName}`}
|
||||
disabled={disabled}
|
||||
onClick={onToggle}
|
||||
className={cn(
|
||||
'relative inline-flex h-11 w-14 shrink-0 items-center rounded-full transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-surface disabled:cursor-not-allowed disabled:opacity-70',
|
||||
isPresent ? 'bg-success' : 'bg-danger',
|
||||
)}
|
||||
>
|
||||
<span
|
||||
className={cn(
|
||||
'inline-flex size-9 items-center justify-center rounded-full bg-white shadow-sm transition-transform duration-150',
|
||||
isPresent ? 'translate-x-3' : 'translate-x-1',
|
||||
)}
|
||||
>
|
||||
{isPresent ? (
|
||||
<Check className="size-5 text-success" aria-hidden />
|
||||
) : (
|
||||
<X className="size-5 text-danger" aria-hidden />
|
||||
)}
|
||||
</span>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import { Link } from '@tanstack/react-router'
|
||||
import { ArrowLeft, Loader2 } from 'lucide-react'
|
||||
import { Badge, Button } from '../../components/ui'
|
||||
import { useAttendance } from './AttendanceProvider'
|
||||
import { StudentRow } from './StudentRow'
|
||||
|
||||
export function ClassAttendanceView() {
|
||||
const {
|
||||
sessionInfo,
|
||||
studentsPending,
|
||||
studentsFailed,
|
||||
retryStudents,
|
||||
startTime,
|
||||
presentCount,
|
||||
saveAttendance,
|
||||
isSaving,
|
||||
} = useAttendance()
|
||||
|
||||
return (
|
||||
<section>
|
||||
<Link
|
||||
to="/"
|
||||
className="mb-3 hidden items-center gap-1 text-sm text-foreground/60 transition-colors hover:text-primary lg:inline-flex"
|
||||
>
|
||||
<ArrowLeft className="size-4" />
|
||||
<span>Volver al inicio</span>
|
||||
</Link>
|
||||
|
||||
{studentsPending ? (
|
||||
<div className="flex items-center justify-center py-16">
|
||||
<Loader2 className="size-6 animate-spin text-foreground/40" />
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{studentsFailed ? (
|
||||
<div className="mt-4 space-y-3 rounded-xl bg-danger-soft px-4 py-3 text-sm text-danger">
|
||||
<p>No pudimos cargar la lista de alumnos.</p>
|
||||
<Button variant="outline" size="sm" onClick={retryStudents}>
|
||||
Reintentar
|
||||
</Button>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{sessionInfo ? (
|
||||
<>
|
||||
<header className="sticky top-16 z-30 -mx-4 border-b border-border bg-background/95 px-4 py-3 backdrop-blur lg:mx-0 lg:rounded-xl lg:border lg:px-4">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<h1 className="truncate text-lg font-bold text-primary">{sessionInfo.groupName}</h1>
|
||||
<p className="text-xs text-foreground/60">{startTime ? `Hoy · ${startTime} hs` : 'Clase de hoy'}</p>
|
||||
</div>
|
||||
<Badge variant="success" className="shrink-0 text-sm">
|
||||
{presentCount}/{sessionInfo.students.length} presentes
|
||||
</Badge>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{sessionInfo.students.length === 0 ? (
|
||||
<div className="mt-6 rounded-xl border border-dashed border-border bg-surface px-4 py-10 text-center">
|
||||
<p className="text-sm font-medium text-primary">Este grupo no tiene alumnos</p>
|
||||
<p className="mt-1 text-sm text-foreground/60">
|
||||
Agregá miembros desde la ficha del grupo para tomar asistencia.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<ul className="mt-4 space-y-3 pb-2">
|
||||
{sessionInfo.students.map((student) => (
|
||||
<StudentRow key={student.attendeeId} student={student} />
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
{sessionInfo.students.length > 0 ? (
|
||||
<div className="sticky bottom-20 z-30 mt-6 lg:bottom-6">
|
||||
<Button
|
||||
variant="primary"
|
||||
className="h-12 w-full text-base font-semibold shadow-lg"
|
||||
disabled={isSaving}
|
||||
onClick={saveAttendance}
|
||||
>
|
||||
{isSaving ? <Loader2 className="size-4 animate-spin" /> : null}
|
||||
Guardar Asistencia
|
||||
</Button>
|
||||
</div>
|
||||
) : null}
|
||||
</>
|
||||
) : null}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
37
apps/web/src/features/class-attendance/StudentRow.tsx
Normal file
37
apps/web/src/features/class-attendance/StudentRow.tsx
Normal file
@@ -0,0 +1,37 @@
|
||||
import type { SessionStudentDto } from '@gruperly/shared'
|
||||
import { Avatar, Badge } from '../../components/ui'
|
||||
import { AttendanceToggle } from './AttendanceToggle'
|
||||
import { useAttendance } from './AttendanceProvider'
|
||||
import { isAttendanceLocked } from './utils'
|
||||
|
||||
function PaymentBadge({ status }: { status: SessionStudentDto['paymentStatus'] }) {
|
||||
return status === 'UP_TO_DATE' ? <Badge variant="success">Al día</Badge> : <Badge variant="danger">Pendiente</Badge>
|
||||
}
|
||||
|
||||
export function StudentRow({ student }: { student: SessionStudentDto }) {
|
||||
const { choiceFor, toggleStudent } = useAttendance()
|
||||
const locked = isAttendanceLocked(student)
|
||||
|
||||
return (
|
||||
<li
|
||||
className={`flex items-center gap-3 rounded-xl border bg-surface px-4 py-3 ${
|
||||
locked ? 'border-warning/30 bg-warning-soft/30' : 'border-border'
|
||||
}`}
|
||||
>
|
||||
<Avatar name={student.fullName} />
|
||||
<div className="min-w-0 flex-1">
|
||||
<p className="truncate text-sm font-semibold text-primary">{student.fullName}</p>
|
||||
<div className="mt-1 flex flex-wrap items-center gap-1.5">
|
||||
<PaymentBadge status={student.paymentStatus} />
|
||||
{locked ? <Badge variant="warning">Avisó ausencia</Badge> : null}
|
||||
</div>
|
||||
</div>
|
||||
<AttendanceToggle
|
||||
choice={choiceFor(student.attendeeId)}
|
||||
disabled={locked}
|
||||
onToggle={() => toggleStudent(student.attendeeId)}
|
||||
fullName={student.fullName}
|
||||
/>
|
||||
</li>
|
||||
)
|
||||
}
|
||||
7
apps/web/src/features/class-attendance/utils.ts
Normal file
7
apps/web/src/features/class-attendance/utils.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { SessionStudentDto } from '@gruperly/shared'
|
||||
|
||||
export type AttendanceChoice = 'PRESENT' | 'ABSENT'
|
||||
|
||||
export function isAttendanceLocked(student: SessionStudentDto): boolean {
|
||||
return student.notifiedAbsence || student.attendanceStatus === 'EXCUSED'
|
||||
}
|
||||
Reference in New Issue
Block a user