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:
@@ -14,20 +14,22 @@ export const Route = createFileRoute("/_authenticated/attendance")({
|
|||||||
});
|
});
|
||||||
|
|
||||||
function AttendancePage() {
|
function AttendancePage() {
|
||||||
const { user, roles } = useAuth();
|
const { user, roles, loading } = useAuth();
|
||||||
const isAdmin = roles.includes("admin");
|
const isAdmin = roles.includes("admin");
|
||||||
const qc = useQueryClient();
|
const qc = useQueryClient();
|
||||||
const [date, setDate] = useState(new Date().toISOString().slice(0, 10));
|
const [date, setDate] = useState(new Date().toISOString().slice(0, 10));
|
||||||
const [classId, setClassId] = useState<string>("");
|
const [classId, setClassId] = useState<string>("");
|
||||||
|
|
||||||
const { data: classes } = useQuery({
|
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 () => {
|
queryFn: async () => {
|
||||||
let q = supabase.from("classes").select("id, name, teacher_id");
|
let q = supabase.from("classes").select("id, name, teacher_id");
|
||||||
if (!isAdmin) q = q.eq("teacher_id", user!.id);
|
if (!isAdmin) q = q.eq("teacher_id", user!.id);
|
||||||
return (await q.order("name")).data ?? [];
|
return (await q.order("name")).data ?? [];
|
||||||
},
|
},
|
||||||
enabled: !!user,
|
enabled: !!user && !loading,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { data: students } = useQuery({
|
const { data: students } = useQuery({
|
||||||
|
|||||||
Reference in New Issue
Block a user