@@ -19,11 +19,13 @@ interface GroupsMap {
19
19
interface UserContextType {
20
20
token : string | null ;
21
21
userId : string | null ;
22
+ fetchFailed : boolean ;
22
23
isAuthenticated : boolean ;
23
24
isTeacher : boolean ;
24
25
myGroups : GroupsMap ;
25
26
userData : UserData | null ;
26
27
setUserData : ( userData : UserData ) => void ;
28
+ setFetchFailed : ( fetchFailed : boolean ) => void ;
27
29
setIsTeacher : ( isTeacher : boolean ) => void ;
28
30
setMyGroups : ( groups : GroupsMap ) => void ;
29
31
login : ( token : string , userId : string , isTeacher : boolean ) => void ;
@@ -65,6 +67,7 @@ const defaultSidebarItems: SidebarItem[] = [...baseSidebarItems];
65
67
const UserContext = createContext < UserContextType > ( {
66
68
token : null ,
67
69
userId : null ,
70
+ fetchFailed : false ,
68
71
isAuthenticated : false ,
69
72
isTeacher : false ,
70
73
myGroups : { } as GroupsMap ,
@@ -73,6 +76,7 @@ const UserContext = createContext<UserContextType>({
73
76
login : ( ) => { } ,
74
77
logout : ( ) => { } ,
75
78
setUserData : ( ) => { } ,
79
+ setFetchFailed : ( ) => { } ,
76
80
sidebarItems : defaultSidebarItems ,
77
81
setSidebarItems : ( ) => { } ,
78
82
setMyGroups : ( ) => { } ,
@@ -85,6 +89,7 @@ export const UserProvider = ({ children }: { children: React.ReactNode }) => {
85
89
const [ userId , setUserId ] = useState < string | null > ( null ) ;
86
90
const [ isTeacher , setIsTeacher ] = useState < boolean > ( false ) ;
87
91
const [ userData , setUserData ] = useState < UserData | null > ( null ) ;
92
+ const [ fetchFailed , setFetchFailed ] = useState < boolean > ( false ) ;
88
93
const [ sidebarItems , setSidebarItems ] = useState < SidebarItem [ ] > ( defaultSidebarItems ) ;
89
94
const [ myGroups , setMyGroups ] = useState < GroupsMap > ( { } as GroupsMap ) ;
90
95
@@ -107,7 +112,9 @@ export const UserProvider = ({ children }: { children: React.ReactNode }) => {
107
112
} ) ;
108
113
109
114
if ( ! response . ok ) {
110
- throw new Error ( 'Failed to fetch user data' ) ;
115
+ setFetchFailed ( true ) ;
116
+ console . log ( "Failed to fetch user data" , response ) ;
117
+ return ;
111
118
}
112
119
113
120
const data = await response . json ( ) ;
@@ -132,6 +139,7 @@ export const UserProvider = ({ children }: { children: React.ReactNode }) => {
132
139
}
133
140
setMyGroups ( groups as GroupsMap ) ;
134
141
} catch ( error ) {
142
+ setFetchFailed ( true ) ;
135
143
console . error ( 'Error fetching user data:' , error ) ;
136
144
toast . error ( 'Failed to load user information' ) ;
137
145
}
@@ -185,6 +193,7 @@ export const UserProvider = ({ children }: { children: React.ReactNode }) => {
185
193
localStorage . setItem ( 'sidebarItems' , JSON . stringify ( [ ...defaultSidebarItems , ...studentSidebarItems ] ) ) ;
186
194
console . log ( "studentSidebarItems" , [ ...defaultSidebarItems , ...studentSidebarItems ] ) ;
187
195
}
196
+ setFetchFailed ( false ) ;
188
197
} ;
189
198
190
199
const logout = ( ) => {
@@ -197,6 +206,7 @@ export const UserProvider = ({ children }: { children: React.ReactNode }) => {
197
206
// Reset sidebar items to default when logging out
198
207
setSidebarItems ( defaultSidebarItems ) ;
199
208
localStorage . setItem ( 'sidebarItems' , JSON . stringify ( defaultSidebarItems ) ) ;
209
+ setFetchFailed ( false ) ;
200
210
} ;
201
211
202
212
const isAuthenticated = IS_MOCK_AUTH || ! ! token ;
@@ -212,6 +222,8 @@ export const UserProvider = ({ children }: { children: React.ReactNode }) => {
212
222
setIsTeacher,
213
223
login,
214
224
logout,
225
+ fetchFailed,
226
+ setFetchFailed,
215
227
userData,
216
228
setMyGroups,
217
229
setUserData,
0 commit comments