Skip to content

Commit cb57102

Browse files
committed
fix(concurrentPerms): check status only if Student user
1 parent fdbf76e commit cb57102

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

‎frontend/src/contexts/permanences.tsx‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { createContext, useContext, useEffect, useState } from 'react';
22

33
import type { ConcurrentPermanences } from '../interfaces/permanence.interface';
4-
import { getToken } from '../services/requests/auth.service';
54
import { getConcurrentPermanencesStatus } from '../services/requests/permanence.service';
5+
import { useUser } from './user';
66

77
type PermanencesContextType = {
88
concurrentPermanences: boolean;
@@ -22,13 +22,12 @@ export const usePermanences = (): PermanencesContextType => {
2222
};
2323

2424
export const PermanencesProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
25-
const tokenPresent = Boolean(getToken());
25+
const { user, loading: userLoading } = useUser();
2626
const [permanences, setPermanences] = useState<ConcurrentPermanences | null>(cachedPermanences);
27-
const [loading, setLoading] = useState<boolean>(() => cachedPermanences === null && tokenPresent);
27+
const [loading, setLoading] = useState<boolean>(() => cachedPermanences === null && userLoading);
2828

2929
const fetchStatus = async () => {
30-
const token = getToken();
31-
if (!token) {
30+
if (!user || user.permission !== 'Student') {
3231
setPermanences(null);
3332
setLoading(false);
3433
return;
@@ -47,8 +46,15 @@ export const PermanencesProvider: React.FC<{ children: React.ReactNode }> = ({ c
4746
};
4847

4948
useEffect(() => {
50-
if (!tokenPresent && !cachedPermanences) {
51-
// nothing to do when not authenticated
49+
if (userLoading) {
50+
setLoading(true);
51+
return;
52+
}
53+
54+
if (!user || user.permission !== 'Student') {
55+
cachedPermanences = null;
56+
setPermanences(null);
57+
setLoading(false);
5258
return;
5359
}
5460

@@ -60,9 +66,15 @@ export const PermanencesProvider: React.FC<{ children: React.ReactNode }> = ({ c
6066

6167
window.addEventListener('user-onboarding-updated', handler);
6268
return () => window.removeEventListener('user-onboarding-updated', handler);
63-
}, []);
69+
}, [user, userLoading]);
6470

6571
const refreshPermanences = async () => {
72+
if (!user || user.permission !== 'Student') {
73+
cachedPermanences = null;
74+
setPermanences(null);
75+
return;
76+
}
77+
6678
cachedPermanences = null;
6779
await fetchStatus();
6880
};

0 commit comments

Comments
 (0)