Fix empty class dropdown on Attendance for admins

The classes query filtered by teacher_id when isAdmin was momentarily false
(roles not yet loaded) and never refetched — cache key lacked isAdmin. Gate on
!loading and include isAdmin in the query key.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-19 17:11:25 -04:00
co-authored by Claude Opus 4.8
parent 8fe6ad00f4
commit 9020b214f3
+5 -3
View File
@@ -14,20 +14,22 @@ export const Route = createFileRoute("/_authenticated/attendance")({
});
function AttendancePage() {
const { user, roles } = useAuth();
const { user, roles, loading } = useAuth();
const isAdmin = roles.includes("admin");
const qc = useQueryClient();
const [date, setDate] = useState(new Date().toISOString().slice(0, 10));
const [classId, setClassId] = useState<string>("");
const { data: classes } = useQuery({
queryKey: ["my-classes", user?.id],
// isAdmin is in the key so the query refetches once roles resolve; gated on
// !loading so it never runs with a not-yet-known (false) admin status.
queryKey: ["my-classes", user?.id, isAdmin],
queryFn: async () => {
let q = supabase.from("classes").select("id, name, teacher_id");
if (!isAdmin) q = q.eq("teacher_id", user!.id);
return (await q.order("name")).data ?? [];
},
enabled: !!user,
enabled: !!user && !loading,
});
const { data: students } = useQuery({