@campxdev/shared 1.11.7-1.alpha.6 → 1.11.7-1.alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/contexts/Providers.tsx +51 -5
- package/src/hooks/useAuth.ts +6 -10
package/package.json
CHANGED
|
@@ -4,20 +4,66 @@ import MuiThemeProvider from '../theme/MuiThemeProvider'
|
|
|
4
4
|
import QueryClientProvider from './QueryClientProvider'
|
|
5
5
|
|
|
6
6
|
import Cookies from 'js-cookie'
|
|
7
|
-
import { ReactNode } from 'react'
|
|
7
|
+
import { ReactNode, useEffect } from 'react'
|
|
8
8
|
import { ToastContainer } from '../components'
|
|
9
9
|
import DialogProvider from '../components/DrawerWrapper/DrawerWrapper'
|
|
10
10
|
import GlobalNetworkLoadingIndicator from '../components/ErrorBoundary/GlobalNetworkLoadingIndicator'
|
|
11
11
|
import ErrorModalProvider from '../components/ErrorModalWrapper/ErrorModalWrapper'
|
|
12
12
|
import RootModal from './RootModal'
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export const
|
|
14
|
+
const isDevelopment = process.env.NODE_ENV == 'development'
|
|
15
|
+
|
|
16
|
+
export const campxTenantKey = isDevelopment
|
|
17
|
+
? window.location.pathname.split('/')[1]
|
|
18
|
+
: window.location.hostname.split('.')[0] ?? Cookies.get('campx_tenant')
|
|
19
|
+
export const urlTenantKey = isDevelopment
|
|
20
|
+
? window.location.pathname.split('/')[1]
|
|
21
|
+
: window.location.hostname.split('.')[0] ?? Cookies.get('campx_tenant')
|
|
22
|
+
export const instituitionKey = isDevelopment
|
|
23
|
+
? window.location.pathname.split('/')[2]
|
|
24
|
+
: window.location.pathname.split('/')[1]
|
|
17
25
|
|
|
18
26
|
export default function Providers({ children }: { children: ReactNode }) {
|
|
27
|
+
const sessionKey = Cookies.get('campx_session_key')
|
|
28
|
+
var baseName = '/'
|
|
29
|
+
var tenantCode
|
|
30
|
+
var institutionCode
|
|
31
|
+
|
|
32
|
+
if (isDevelopment) {
|
|
33
|
+
tenantCode = window.location.pathname.split('/')[1]
|
|
34
|
+
institutionCode = window.location.pathname.split('/')[2]
|
|
35
|
+
if (
|
|
36
|
+
tenantCode &&
|
|
37
|
+
institutionCode &&
|
|
38
|
+
window.location.pathname !== '/auth/login'
|
|
39
|
+
) {
|
|
40
|
+
baseName = `/${tenantCode}/${institutionCode}`
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
tenantCode = window.location.hostname.split('.')[0]
|
|
44
|
+
institutionCode = window.location.pathname.split('/')[1]
|
|
45
|
+
|
|
46
|
+
if (institutionCode && window.location.pathname !== '/auth/login') {
|
|
47
|
+
baseName = `/${institutionCode}`
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
if (
|
|
53
|
+
isDevelopment &&
|
|
54
|
+
location.pathname === '/' &&
|
|
55
|
+
institutionCode &&
|
|
56
|
+
tenantCode
|
|
57
|
+
) {
|
|
58
|
+
window.location.replace(
|
|
59
|
+
window.location.origin + `/${tenantCode}/${institutionCode}`,
|
|
60
|
+
)
|
|
61
|
+
} else if (location.pathname === '/' && institutionCode) {
|
|
62
|
+
window.location.replace(window.location.origin + `/${institutionCode}`)
|
|
63
|
+
}
|
|
64
|
+
}, [])
|
|
19
65
|
return (
|
|
20
|
-
<BrowserRouter>
|
|
66
|
+
<BrowserRouter basename={baseName}>
|
|
21
67
|
<QueryClientProvider>
|
|
22
68
|
<MuiThemeProvider>
|
|
23
69
|
<ConfirmContextProvider>
|
package/src/hooks/useAuth.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import Cookies from 'js-cookie'
|
|
2
1
|
import { useEffect, useState } from 'react'
|
|
3
2
|
import { useNavigate } from 'react-router-dom'
|
|
4
3
|
import { toast } from 'react-toastify'
|
|
@@ -98,18 +97,18 @@ const useAuth = ({
|
|
|
98
97
|
permissionsEndpoint,
|
|
99
98
|
loginUrl,
|
|
100
99
|
}: AuthParams): AuthResponse => {
|
|
101
|
-
const [loading, setLoading] = useState<boolean>(
|
|
100
|
+
const [loading, setLoading] = useState<boolean>(true)
|
|
102
101
|
const [data, setData] = useState<any>(null)
|
|
103
102
|
const { current } = InstitutionsStore.useState()
|
|
104
103
|
const navigate = useNavigate()
|
|
105
104
|
|
|
106
105
|
const appInit = async () => {
|
|
107
|
-
setLoading(true)
|
|
108
106
|
try {
|
|
109
107
|
const res = await axios.get(permissionsEndpoint)
|
|
110
108
|
const isAdmin = checkIsAdmin(res.data.user)
|
|
111
|
-
|
|
109
|
+
setLoading(false)
|
|
112
110
|
setData(res.data)
|
|
111
|
+
|
|
113
112
|
UserStore.update((s) => {
|
|
114
113
|
s.username = res.data?.user?.username
|
|
115
114
|
s.user = res.data?.user
|
|
@@ -143,17 +142,13 @@ const useAuth = ({
|
|
|
143
142
|
|
|
144
143
|
InstitutionsStore.update((s) => {
|
|
145
144
|
s.institutions = res.data?.institutions
|
|
146
|
-
s.current = res.data?.institutions
|
|
147
|
-
(item) => item.code === Cookies.get('campx_institution'),
|
|
148
|
-
)
|
|
145
|
+
s.current = res.data?.institutions[0]
|
|
149
146
|
})
|
|
150
147
|
|
|
151
148
|
AssetsStore.update((s) => {
|
|
152
149
|
s.logo = res.data?.assets.logo
|
|
153
150
|
s.logo_square = res.data?.assets.logo_square
|
|
154
151
|
})
|
|
155
|
-
|
|
156
|
-
setLoading(false)
|
|
157
152
|
} catch (err: any) {
|
|
158
153
|
setLoading(false)
|
|
159
154
|
const origin = window.location.origin
|
|
@@ -176,10 +171,11 @@ const useAuth = ({
|
|
|
176
171
|
}
|
|
177
172
|
}
|
|
178
173
|
}
|
|
179
|
-
|
|
174
|
+
console.log(loading, !data?.permissions, !current, 'abc1')
|
|
180
175
|
useEffect(() => {
|
|
181
176
|
appInit()
|
|
182
177
|
}, [])
|
|
178
|
+
console.log(loading, !data?.permissions, !current, 'abc2')
|
|
183
179
|
|
|
184
180
|
return {
|
|
185
181
|
loading: loading || !data?.permissions || !current,
|