Greg Claude Sonnet 4.6 commited on
Commit
655da53
·
1 Parent(s): 2c6caad

feat(ta-01): admin attendance page with /admin-attendance route + sidebar item

Browse files

Adds AdminAttendancePage (standalone, no extraction — state shapes differ too
much between teacher list view and admin roster view). Features:
- Section dropdown + date picker (default today, supports back-dating)
- Roster table with effective-status logic, pending toggles, amber highlight
- Gate chip: neutral gray, informational tooltip, never blocking
- Save calls batchToggle then refreshes roster
- Toast auto-dismiss, error banner, loading spinner
Wired at /dashboard/admin-attendance; sidebar visible to admin role only.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

frontend/src/App.tsx CHANGED
@@ -9,6 +9,7 @@ import EnrollmentsPage from './pages/EnrollmentsPage'
9
  import AttendanceReportsPage from './pages/AttendanceReportsPage'
10
  import SettingsPage from './pages/SettingsPage'
11
  import TeacherAttendancePage from './pages/TeacherAttendancePage'
 
12
  import TeacherSubjectsPage from './pages/TeacherSubjectsPage'
13
  import TeacherSectionsPage from './pages/TeacherSectionsPage'
14
  import PublicCheckInPage from './pages/PublicCheckInPage'
@@ -48,6 +49,7 @@ export default function App() {
48
  <Route path="attendance" element={<AttendanceReportsPage />} />
49
  <Route path="settings" element={<SettingsPage />} />
50
  <Route path="teacher-attendance" element={<TeacherAttendancePage />} />
 
51
  <Route path="public-checkin" element={<PublicCheckInPage />} />
52
  <Route path="developer" element={<DeveloperPage />} />
53
  <Route path="card-templates" element={<CardTemplatesPage />} />
 
9
  import AttendanceReportsPage from './pages/AttendanceReportsPage'
10
  import SettingsPage from './pages/SettingsPage'
11
  import TeacherAttendancePage from './pages/TeacherAttendancePage'
12
+ import AdminAttendancePage from './pages/AdminAttendancePage'
13
  import TeacherSubjectsPage from './pages/TeacherSubjectsPage'
14
  import TeacherSectionsPage from './pages/TeacherSectionsPage'
15
  import PublicCheckInPage from './pages/PublicCheckInPage'
 
49
  <Route path="attendance" element={<AttendanceReportsPage />} />
50
  <Route path="settings" element={<SettingsPage />} />
51
  <Route path="teacher-attendance" element={<TeacherAttendancePage />} />
52
+ <Route path="admin-attendance" element={<AdminAttendancePage />} />
53
  <Route path="public-checkin" element={<PublicCheckInPage />} />
54
  <Route path="developer" element={<DeveloperPage />} />
55
  <Route path="card-templates" element={<CardTemplatesPage />} />
frontend/src/components/Sidebar.tsx CHANGED
@@ -14,6 +14,7 @@ const navItems = [
14
  { to: '/dashboard/mis-hijos', label: 'Mis Hijos', icon: Heart, roles: ['representative'] },
15
  { to: '/dashboard/public-checkin', label: 'Mi Asistencia', icon: Clock, roles: ['admin', 'teacher', 'representative', 'student', 'administrativo', 'obrero'] },
16
  { to: '/dashboard/teacher-attendance', label: 'Asistencia', icon: CreditCard, roles: ['teacher'] },
 
17
  { to: '/dashboard/attendance', label: 'Reportes', icon: BarChart3, roles: ['admin', 'administrativo'] },
18
  { to: '/dashboard/student-report', label: 'Rep. Estudiantes', icon: FileText, roles: ['admin', 'administrativo'] },
19
  { to: '/dashboard/late-arrivals', label: 'Tardanzas', icon: Clock, roles: ['admin', 'administrativo', 'teacher'] },
 
14
  { to: '/dashboard/mis-hijos', label: 'Mis Hijos', icon: Heart, roles: ['representative'] },
15
  { to: '/dashboard/public-checkin', label: 'Mi Asistencia', icon: Clock, roles: ['admin', 'teacher', 'representative', 'student', 'administrativo', 'obrero'] },
16
  { to: '/dashboard/teacher-attendance', label: 'Asistencia', icon: CreditCard, roles: ['teacher'] },
17
+ { to: '/dashboard/admin-attendance', label: 'Asistencia (contingencia)', icon: ClipboardCheck, roles: ['admin'] },
18
  { to: '/dashboard/attendance', label: 'Reportes', icon: BarChart3, roles: ['admin', 'administrativo'] },
19
  { to: '/dashboard/student-report', label: 'Rep. Estudiantes', icon: FileText, roles: ['admin', 'administrativo'] },
20
  { to: '/dashboard/late-arrivals', label: 'Tardanzas', icon: Clock, roles: ['admin', 'administrativo', 'teacher'] },
frontend/src/pages/AdminAttendancePage.tsx ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useEffect, useState } from 'react';
2
+ import { Loader2, Save } from 'lucide-react';
3
+ import {
4
+ adminAttendanceService,
5
+ type AdminSection,
6
+ type AdminRosterItem,
7
+ type AdminAttendanceRecord,
8
+ } from '../services/adminAttendanceService';
9
+ import DateInput from '../components/DateInput';
10
+
11
+ export default function AdminAttendancePage() {
12
+ const [sections, setSections] = useState<AdminSection[]>([]);
13
+ const [sectionId, setSectionId] = useState<number | ''>('');
14
+ const [date, setDate] = useState<string>(() => new Date().toISOString().slice(0, 10));
15
+ const [roster, setRoster] = useState<AdminRosterItem[]>([]);
16
+ const [rosterLoading, setRosterLoading] = useState(false);
17
+ // pending: student_id → desired new present value
18
+ const [pendingToggles, setPendingToggles] = useState<Map<number, boolean>>(new Map());
19
+ const [saving, setSaving] = useState(false);
20
+ const [error, setError] = useState<string | null>(null);
21
+ const [toast, setToast] = useState<{ text: string; type: 'error' | 'success' } | null>(null);
22
+
23
+ // Auto-dismiss toast
24
+ useEffect(() => {
25
+ if (!toast) return;
26
+ const t = setTimeout(() => setToast(null), 4000);
27
+ return () => clearTimeout(t);
28
+ }, [toast]);
29
+
30
+ // Load sections on mount
31
+ useEffect(() => {
32
+ adminAttendanceService
33
+ .listSections()
34
+ .then(setSections)
35
+ .catch((e) => setError(String(e?.message ?? e)));
36
+ }, []);
37
+
38
+ // Load roster when section or date changes
39
+ useEffect(() => {
40
+ if (sectionId === '') return;
41
+ setRosterLoading(true);
42
+ setError(null);
43
+ setPendingToggles(new Map());
44
+ adminAttendanceService
45
+ .getRoster(Number(sectionId), date)
46
+ .then((r) => { setRoster(r); setRosterLoading(false); })
47
+ .catch((e) => { setError(String(e?.message ?? e)); setRosterLoading(false); });
48
+ }, [sectionId, date]);
49
+
50
+ const getEffective = (r: AdminRosterItem): { present: boolean; pending: boolean } => {
51
+ if (pendingToggles.has(r.id)) {
52
+ return { present: pendingToggles.get(r.id)!, pending: true };
53
+ }
54
+ return { present: r.already_checked_in, pending: false };
55
+ };
56
+
57
+ const toggleStudent = (r: AdminRosterItem) => {
58
+ const currentEffective = pendingToggles.has(r.id)
59
+ ? pendingToggles.get(r.id)!
60
+ : r.already_checked_in;
61
+ setPendingToggles((prev) => {
62
+ const next = new Map(prev);
63
+ next.set(r.id, !currentEffective);
64
+ return next;
65
+ });
66
+ };
67
+
68
+ const save = async () => {
69
+ if (sectionId === '' || pendingToggles.size === 0) return;
70
+ setSaving(true);
71
+ setError(null);
72
+ try {
73
+ const records: AdminAttendanceRecord[] = Array.from(pendingToggles.entries()).map(
74
+ ([student_id, present]) => ({ student_id, present }),
75
+ );
76
+ const res = await adminAttendanceService.batchToggle(Number(sectionId), records, date);
77
+ setPendingToggles(new Map());
78
+ const skippedCount = res.skipped?.length ?? 0;
79
+ if (skippedCount > 0) {
80
+ setToast({ text: `${res.created} marcados. ${skippedCount} no se pudieron marcar (sin check-in en gate).`, type: 'error' });
81
+ } else {
82
+ setToast({ text: `Asistencia guardada correctamente (${res.created} registros).`, type: 'success' });
83
+ }
84
+ // Refresh roster to show updated state
85
+ const fresh = await adminAttendanceService.getRoster(Number(sectionId), date);
86
+ setRoster(fresh);
87
+ } catch (e: any) {
88
+ setError(String(e?.response?.data?.detail ?? e?.message ?? e));
89
+ } finally {
90
+ setSaving(false);
91
+ }
92
+ };
93
+
94
+ const sortedRoster = [...roster].sort((a, b) => {
95
+ const aEff = getEffective(a);
96
+ const bEff = getEffective(b);
97
+ if (aEff.present === bEff.present) return 0;
98
+ return aEff.present ? 1 : -1; // absent first
99
+ });
100
+
101
+ const presentCount = roster.filter((r) => getEffective(r).present).length;
102
+ const absentCount = roster.length - presentCount;
103
+
104
+ return (
105
+ <div className="max-w-4xl mx-auto space-y-6">
106
+ {toast && (
107
+ <div
108
+ className={`fixed top-6 left-1/2 -translate-x-1/2 z-[100] px-6 py-3 rounded-xl shadow-lg text-sm font-medium transition-all duration-300 animate-fade-in ${toast.type === 'error' ? 'bg-red-600 text-white' : 'bg-emerald-600 text-white'}`}
109
+ onClick={() => setToast(null)}
110
+ style={{ cursor: 'pointer' }}
111
+ >
112
+ {toast.text}
113
+ </div>
114
+ )}
115
+
116
+ <div>
117
+ <h1>Asistencia por contingencia</h1>
118
+ <p className="text-sm text-gray-500 mt-1">
119
+ Usá esta vista cuando un docente esté ausente y necesités pasar lista en su aula.
120
+ </p>
121
+ </div>
122
+
123
+ {error && (
124
+ <div className="rounded-lg bg-red-50 border border-red-200 text-red-700 px-4 py-2 text-sm">
125
+ {error}
126
+ </div>
127
+ )}
128
+
129
+ <div className="flex gap-3 items-end flex-wrap">
130
+ <div>
131
+ <label className="block text-xs text-gray-500 mb-1">Sección</label>
132
+ <select
133
+ value={sectionId}
134
+ onChange={(e) => setSectionId(e.target.value ? Number(e.target.value) : '')}
135
+ className="border border-gray-300 rounded-lg px-3 py-2"
136
+ style={{ minWidth: 220 }}
137
+ >
138
+ <option value="">Elegí una sección…</option>
139
+ {sections.map((s) => (
140
+ <option key={s.id} value={s.id}>{s.name}</option>
141
+ ))}
142
+ </select>
143
+ </div>
144
+ <div>
145
+ <label className="block text-xs text-gray-500 mb-1">Fecha</label>
146
+ <DateInput
147
+ value={date}
148
+ onChange={(v) => setDate(v)}
149
+ className="border border-gray-300 rounded-lg px-3 py-2"
150
+ />
151
+ </div>
152
+ </div>
153
+
154
+ {sectionId !== '' && (
155
+ <>
156
+ {rosterLoading ? (
157
+ <div className="flex items-center justify-center py-12">
158
+ <Loader2 size={24} className="animate-spin text-gray-400" />
159
+ </div>
160
+ ) : (
161
+ <>
162
+ {roster.length > 0 && (
163
+ <div className="rounded-lg bg-emerald-50 border border-emerald-200 px-4 py-2 text-sm font-medium text-emerald-900 inline-flex gap-3">
164
+ <span>✓ Presentes: {presentCount}</span>
165
+ <span>Ausentes: {absentCount}</span>
166
+ </div>
167
+ )}
168
+
169
+ <div className="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
170
+ <div className="overflow-x-auto">
171
+ <table className="w-full text-sm min-w-[560px] lg:min-w-0">
172
+ <thead>
173
+ <tr className="border-b border-gray-200 text-left text-gray-500">
174
+ <th className="pb-2 font-medium pl-3 pr-2 pt-2">Estudiante</th>
175
+ <th className="pb-2 font-medium pr-2 pt-2">Cédula</th>
176
+ <th className="pb-2 font-medium pr-2 pt-2">Estado</th>
177
+ <th className="pb-2 font-medium pr-2 pt-2">Gate</th>
178
+ <th className="pb-2 font-medium pt-2 pr-3 text-right">Acción</th>
179
+ </tr>
180
+ </thead>
181
+ <tbody>
182
+ {sortedRoster.length === 0 ? (
183
+ <tr>
184
+ <td colSpan={5} className="text-gray-400 text-sm text-center py-8">
185
+ Sin estudiantes en esta sección
186
+ </td>
187
+ </tr>
188
+ ) : (
189
+ sortedRoster.map((r) => {
190
+ const eff = getEffective(r);
191
+ return (
192
+ <tr
193
+ key={r.id}
194
+ className={`border-b border-gray-100 ${eff.pending ? 'bg-amber-50/50' : ''}`}
195
+ >
196
+ <td className="py-2 font-medium pl-3 pr-2">
197
+ {r.lastname}, {r.name}
198
+ </td>
199
+ <td className="py-2 text-gray-500 text-xs font-mono pr-2">
200
+ {r.card_id || '-'}
201
+ </td>
202
+ <td className="py-2 pr-2">
203
+ <span
204
+ className={`inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium ${
205
+ eff.present
206
+ ? 'bg-green-100 text-green-700'
207
+ : 'bg-red-100 text-red-700'
208
+ }`}
209
+ >
210
+ {eff.present ? 'Presente' : 'Ausente'}
211
+ </span>
212
+ </td>
213
+ <td className="py-2 pr-2">
214
+ <span
215
+ className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-500"
216
+ title={
217
+ r.entrance_logged
218
+ ? 'Pasó por el gate hoy.'
219
+ : 'Sin check-in en gate. Podés marcar igualmente.'
220
+ }
221
+ >
222
+ {r.entrance_logged ? 'Gate ✓' : 'Gate –'}
223
+ </span>
224
+ </td>
225
+ <td className="py-2 pr-3 text-right">
226
+ <button
227
+ type="button"
228
+ onClick={() => toggleStudent(r)}
229
+ className={`px-3 py-1 rounded-lg text-xs transition-colors ${
230
+ eff.pending
231
+ ? 'bg-amber-100 text-amber-700 border border-amber-300 hover:bg-amber-200'
232
+ : 'bg-gray-50 border border-gray-200 text-gray-600 hover:bg-blue-50 hover:border-blue-300'
233
+ }`}
234
+ >
235
+ {eff.pending
236
+ ? eff.present ? 'Marcar presente ↩' : 'Marcar ausente ↩'
237
+ : eff.present ? 'Marcar ausente' : 'Marcar presente'}
238
+ </button>
239
+ </td>
240
+ </tr>
241
+ );
242
+ })
243
+ )}
244
+ </tbody>
245
+ </table>
246
+ </div>
247
+
248
+ {pendingToggles.size > 0 && (
249
+ <div className="border-t border-gray-200 p-4 flex items-center justify-between bg-amber-50/30">
250
+ <span className="text-sm text-amber-700 font-medium">
251
+ {pendingToggles.size} cambio{pendingToggles.size !== 1 ? 's' : ''} pendiente{pendingToggles.size !== 1 ? 's' : ''}
252
+ </span>
253
+ <button
254
+ type="button"
255
+ onClick={save}
256
+ disabled={saving}
257
+ className="inline-flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition-colors disabled:opacity-50 text-sm"
258
+ >
259
+ {saving ? <Loader2 size={16} className="animate-spin" /> : <Save size={16} />}
260
+ {saving ? 'Guardando…' : 'Guardar Cambios'}
261
+ </button>
262
+ </div>
263
+ )}
264
+ </div>
265
+ </>
266
+ )}
267
+ </>
268
+ )}
269
+ </div>
270
+ );
271
+ }