@campxdev/shared 1.8.49 → 1.9.0
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/components/ApplicationProfile/ApplicationProfile.tsx +29 -27
- package/src/components/ApplicationProfile/UserProfileRelation.tsx +9 -53
- package/src/components/ErrorBoundary/ErrorFallback.tsx +5 -6
- package/src/components/Institutions/InsititutionsDialog.tsx +70 -0
- package/src/components/Institutions/InsititutionsNotAssignedDialog .tsx +12 -0
- package/src/components/Institutions/InstitutionsDropdown.tsx +33 -0
- package/src/components/Institutions/index.tsx +1 -0
- package/src/components/Institutions/services.ts +12 -0
- package/src/components/Layout/Header/AppHeader.tsx +3 -7
- package/src/components/Layout/Header/AppsMenu.tsx +6 -7
- package/src/components/Layout/Header/HeaderActions/HeaderActions.tsx +2 -2
- package/src/components/LoginForm.tsx +21 -26
- package/src/components/ModalButtons/DialogButton.tsx +28 -24
- package/src/components/NoDataIllustration.tsx +8 -5
- package/src/components/Tables/common/NoRecordsFound.tsx +5 -1
- package/src/components/Tables/common/no-data-illu.svg +24 -1
- package/src/components/UploadButton/UploadButton.tsx +3 -3
- package/src/config/axios.ts +4 -3
- package/src/constants/UIConstants.ts +1 -1
- package/src/contexts/LoginFormProvider.tsx +3 -5
- package/src/contexts/Providers.tsx +40 -18
- package/src/contexts/RootModal.tsx +76 -0
- package/src/hooks/useAuth.ts +123 -27
- package/src/shared-state/InstitutionsStore.ts +8 -0
- package/src/components/Layout/Header/SchoolSwitch/SchoolSwitch.tsx +0 -33
- /package/src/components/ApplicationProfile/{Service.ts → services.ts} +0 -0
package/src/hooks/useAuth.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { AxiosError } from 'axios'
|
|
2
2
|
import { useEffect, useState } from 'react'
|
|
3
3
|
import { toast } from 'react-toastify'
|
|
4
|
+
import { institutions } from '../components/Institutions/services'
|
|
4
5
|
import axios from '../config/axios'
|
|
5
6
|
import { isDevelopment } from '../constants'
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
7
|
+
import { urlTenantKey } from '../contexts/Providers'
|
|
8
|
+
import { openRootModal } from '../contexts/RootModal'
|
|
9
|
+
import { AssetsStore, PermissionsStore, UserStore } from '../shared-state'
|
|
10
|
+
import { InsititutionsStore } from '../shared-state/InstitutionsStore'
|
|
8
11
|
|
|
9
12
|
const url = window.location.origin
|
|
10
13
|
|
|
@@ -32,8 +35,8 @@ const ApplicationObj = {
|
|
|
32
35
|
ums: 'square',
|
|
33
36
|
payments: 'payments',
|
|
34
37
|
exams: 'exams',
|
|
38
|
+
hostel: 'hostels',
|
|
35
39
|
}
|
|
36
|
-
|
|
37
40
|
const checkIsAdmin = (user) => {
|
|
38
41
|
let subDomain = window.location.host.split('.')?.slice(-3)[0]
|
|
39
42
|
const localSubDomain = process.env.REACT_APP_SUBDOMAIN
|
|
@@ -61,11 +64,119 @@ const checkIsAdmin = (user) => {
|
|
|
61
64
|
)
|
|
62
65
|
return profile ? (profile.isAdmin == true ? 1 : 0) : 0
|
|
63
66
|
}
|
|
67
|
+
const getInstitutionKey = () => {
|
|
68
|
+
const instituitionKey = window.location.pathname.split('/')[2]
|
|
69
|
+
if (!instituitionKey) {
|
|
70
|
+
const localInstituitionKey = localStorage.getItem('institution_key')
|
|
71
|
+
if (localInstituitionKey) {
|
|
72
|
+
return localInstituitionKey
|
|
73
|
+
} else {
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
return instituitionKey
|
|
78
|
+
}
|
|
79
|
+
return instituitionKey
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function handleInstitutions(institutions) {
|
|
83
|
+
const insititutionKey = getInstitutionKey()
|
|
84
|
+
if (institutions?.length === 0) {
|
|
85
|
+
openRootModal({
|
|
86
|
+
key: 'institutions-not-assigned',
|
|
87
|
+
dialogProps: {
|
|
88
|
+
disableEscapeKeyDown: true,
|
|
89
|
+
onClose: () => {},
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
if (institutions?.length === 1) {
|
|
94
|
+
if (!insititutionKey) {
|
|
95
|
+
window.location.replace(
|
|
96
|
+
`${window.location.origin}/${urlTenantKey}/${institutions[0]?.code}`,
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
if (insititutionKey !== institutions[0]?.code) {
|
|
100
|
+
window.location.replace(
|
|
101
|
+
`${window.location.origin}/${urlTenantKey}/${institutions[0]?.code}`,
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
InsititutionsStore.update((s) => {
|
|
105
|
+
s.current = institutions[0]
|
|
106
|
+
s.institutions = institutions
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
if (institutions?.length > 1) {
|
|
110
|
+
if (!insititutionKey) {
|
|
111
|
+
openRootModal({
|
|
112
|
+
key: 'institutions',
|
|
113
|
+
dialogProps: {
|
|
114
|
+
disableEscapeKeyDown: true,
|
|
115
|
+
onClose: () => {},
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
if (
|
|
120
|
+
insititutionKey &&
|
|
121
|
+
!institutions?.find((item) => item.code === insititutionKey)
|
|
122
|
+
) {
|
|
123
|
+
openRootModal({
|
|
124
|
+
key: 'institutions',
|
|
125
|
+
dialogProps: {
|
|
126
|
+
disableEscapeKeyDown: true,
|
|
127
|
+
onClose: () => {},
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
InsititutionsStore.update((s) => {
|
|
132
|
+
s.institutions = institutions
|
|
133
|
+
s.current = institutions.find((item) => item.code === insititutionKey)
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const loginErrorHandler = ({
|
|
139
|
+
loginUrl,
|
|
140
|
+
setLoading,
|
|
141
|
+
err,
|
|
142
|
+
}: {
|
|
143
|
+
loginUrl: string
|
|
144
|
+
setLoading?: any
|
|
145
|
+
err: AxiosError
|
|
146
|
+
}) => {
|
|
147
|
+
setLoading && setLoading(false)
|
|
148
|
+
const origin = window.location.origin
|
|
149
|
+
const isStaging = origin.split('campx')[1] === '.dev'
|
|
150
|
+
|
|
151
|
+
if (isDevelopment || isStaging) {
|
|
152
|
+
openRootModal({
|
|
153
|
+
key: 'login',
|
|
154
|
+
contentData: {
|
|
155
|
+
loginUrl,
|
|
156
|
+
},
|
|
157
|
+
dialogProps: {
|
|
158
|
+
disableEscapeKeyDown: true,
|
|
159
|
+
onClose: () => {},
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
return
|
|
163
|
+
} else {
|
|
164
|
+
window.location.replace(`https://www.id.campx.in/?redirect_to=${url}`)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (err.response.status !== 401) {
|
|
168
|
+
if (err.response.status > 400 && err.response.status < 500) {
|
|
169
|
+
window.location.replace(`https://www.id.campx.in/?redirect_to=${url}`)
|
|
170
|
+
} else {
|
|
171
|
+
toast.error('Server Error')
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
64
175
|
|
|
65
176
|
function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
|
|
66
|
-
const { openLoginForm } = useLoginForm()
|
|
67
177
|
const [loading, setLoading] = useState<boolean>(false)
|
|
68
178
|
const [data, setData] = useState(null)
|
|
179
|
+
const { current } = InsititutionsStore.useState()
|
|
69
180
|
|
|
70
181
|
const appInit = async () => {
|
|
71
182
|
setLoading(true)
|
|
@@ -75,7 +186,6 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
|
|
|
75
186
|
const origin = window.location.origin
|
|
76
187
|
const originSubdomain =
|
|
77
188
|
window.location.host.split('.')?.slice(-3)[0] ?? 'ums'
|
|
78
|
-
|
|
79
189
|
const isStaging = origin.split('campx')[1] === '.dev'
|
|
80
190
|
|
|
81
191
|
if (isDevelopment == false && isStaging == false) {
|
|
@@ -88,7 +198,10 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
|
|
|
88
198
|
}
|
|
89
199
|
}
|
|
90
200
|
|
|
91
|
-
|
|
201
|
+
if (res.data?.institutions) {
|
|
202
|
+
handleInstitutions(res.data?.institutions)
|
|
203
|
+
}
|
|
204
|
+
|
|
92
205
|
setData(res.data)
|
|
93
206
|
UserStore.update((s) => {
|
|
94
207
|
s.username = res.data?.user?.username
|
|
@@ -114,28 +227,11 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
|
|
|
114
227
|
s.logo = res.data?.assets.logo
|
|
115
228
|
s.logo_square = res.data?.assets.logo_square
|
|
116
229
|
})
|
|
230
|
+
|
|
231
|
+
setLoading(false)
|
|
117
232
|
})
|
|
118
233
|
.catch((err: AxiosError) => {
|
|
119
|
-
setLoading
|
|
120
|
-
const origin = window.location.origin
|
|
121
|
-
const isStaging = origin.split('campx')[1] === '.dev'
|
|
122
|
-
|
|
123
|
-
if (isDevelopment || isStaging) {
|
|
124
|
-
openLoginForm(loginUrl)
|
|
125
|
-
return
|
|
126
|
-
} else {
|
|
127
|
-
window.location.replace(`https://www.id.campx.in/?redirect_to=${url}`)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (err.response.status !== 401) {
|
|
131
|
-
if (err.response.status > 400 && err.response.status < 500) {
|
|
132
|
-
window.location.replace(
|
|
133
|
-
`https://www.id.campx.in/?redirect_to=${url}`,
|
|
134
|
-
)
|
|
135
|
-
} else {
|
|
136
|
-
toast.error('Server Error')
|
|
137
|
-
}
|
|
138
|
-
}
|
|
234
|
+
loginErrorHandler({ loginUrl, setLoading, err })
|
|
139
235
|
})
|
|
140
236
|
}
|
|
141
237
|
|
|
@@ -144,7 +240,7 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
|
|
|
144
240
|
}, [])
|
|
145
241
|
|
|
146
242
|
return {
|
|
147
|
-
loading: loading || !data?.permissions,
|
|
243
|
+
loading: loading || !data?.permissions || !current,
|
|
148
244
|
data,
|
|
149
245
|
}
|
|
150
246
|
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { useState } from 'react'
|
|
2
|
-
import { FormSingleSelect } from '../../../HookForm'
|
|
3
|
-
import { SingleSelect } from '../../../Input'
|
|
4
|
-
|
|
5
|
-
export default function SchoolSwitch() {
|
|
6
|
-
const [state, setStatr] = useState()
|
|
7
|
-
|
|
8
|
-
return (
|
|
9
|
-
<SingleSelect
|
|
10
|
-
containerProps={{
|
|
11
|
-
sx: {
|
|
12
|
-
width: '200px',
|
|
13
|
-
},
|
|
14
|
-
}}
|
|
15
|
-
options={[
|
|
16
|
-
{
|
|
17
|
-
label: 'School 1',
|
|
18
|
-
value: 'school1',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
label: 'School 2',
|
|
22
|
-
value: 'school2',
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
label: 'School 3',
|
|
26
|
-
value: 'school3',
|
|
27
|
-
},
|
|
28
|
-
]}
|
|
29
|
-
value={state}
|
|
30
|
-
onChange={(e) => setStatr(e.target.value)}
|
|
31
|
-
/>
|
|
32
|
-
)
|
|
33
|
-
}
|
|
File without changes
|