1
- import { Box , Button , CssBaseline , Snackbar } from '@mui/material'
1
+ import { Box , Button , CircularProgress , CssBaseline , Snackbar } from '@mui/material'
2
2
import { ThemeProvider } from '@mui/material/styles'
3
3
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3'
4
4
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'
5
5
import { fi } from 'date-fns/locale'
6
6
import { SnackbarProvider } from 'notistack'
7
7
import React , { useEffect , useRef } from 'react'
8
- import { Outlet , useLocation , useParams } from 'react-router-dom'
8
+ import { Navigate , Outlet , useLocation , useParams } from 'react-router-dom'
9
9
import { initShibbolethPinger } from 'unfuck-spa-shibboleth-session'
10
10
import { PUBLIC_URL } from '../config'
11
11
import { Feedback } from './components/Feedback'
12
- import Footer from './components/Footer'
13
12
import NavBar from './components/NavBar'
14
13
import { AppContext } from './contexts/AppContext'
15
14
import { EmbeddedProvider , useIsEmbedded } from './contexts/EmbeddedContext'
@@ -75,23 +74,10 @@ const AdminLoggedInAsBanner = () => {
75
74
const App = ( ) => {
76
75
useUpdateUrlLang ( )
77
76
const theme = useTheme ( )
78
- const { courseId } = useParams ( )
79
- const location = useLocation ( )
80
- const { user, isLoading } = useCurrentUser ( )
81
77
82
78
useEffect ( ( ) => {
83
79
initShibbolethPinger ( )
84
80
} , [ ] )
85
- const onNoAccessPage = location . pathname . includes ( '/noaccess' )
86
-
87
- if ( isLoading && ! onNoAccessPage ) return null
88
-
89
- if ( ! onNoAccessPage && ! hasAccess ( user , courseId ) ) {
90
- window . location . href = PUBLIC_URL + getRedirect ( user )
91
- return null
92
- }
93
-
94
- if ( ! user && ! onNoAccessPage ) return null
95
81
96
82
return (
97
83
< ThemeProvider theme = { theme } >
@@ -127,7 +113,7 @@ const Layout = () => {
127
113
>
128
114
{ ! isEmbedded && < NavBar /> }
129
115
< Box sx = { { flex : 1 } } >
130
- < Outlet />
116
+ < Content />
131
117
</ Box >
132
118
< Feedback />
133
119
</ Box >
@@ -136,4 +122,26 @@ const Layout = () => {
136
122
)
137
123
}
138
124
125
+ const Content = ( ) => {
126
+ const { courseId } = useParams ( )
127
+ const location = useLocation ( )
128
+ const { user, isLoading } = useCurrentUser ( )
129
+
130
+ const onNoAccessPage = location . pathname . includes ( '/noaccess' )
131
+
132
+ if ( isLoading && ! onNoAccessPage ) return < CircularProgress sx = { { margin : 'auto' } } />
133
+
134
+ if ( ! onNoAccessPage && ! hasAccess ( user , courseId ) ) {
135
+ return < Navigate to = { PUBLIC_URL + getRedirect ( user ) } />
136
+ }
137
+
138
+ if ( ! user && ! onNoAccessPage ) return null
139
+
140
+ return (
141
+ < Box sx = { { flex : 1 } } >
142
+ < Outlet />
143
+ </ Box >
144
+ )
145
+ }
146
+
139
147
export default App
0 commit comments