@campxdev/shared 1.8.49-alpha.3 → 1.8.49-alpha.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campxdev/shared",
3
- "version": "1.8.49-alpha.3",
3
+ "version": "1.8.49-alpha.4",
4
4
  "main": "./exports.ts",
5
5
  "scripts": {
6
6
  "start": "react-scripts start",
@@ -0,0 +1,12 @@
1
+ import { Box, Typography } from '@mui/material'
2
+
3
+ export default function InsititutionsNotAssignedDialog() {
4
+ return (
5
+ <Box padding="40px 10px">
6
+ <Typography variant="body2" textAlign="center">
7
+ You have not been assigned to any institutions. Please contact your
8
+ administrator.
9
+ </Typography>
10
+ </Box>
11
+ )
12
+ }
@@ -8,6 +8,7 @@ export default function SchoolSwitch() {
8
8
  const { current, institutions } = InsititutionsStore.useState((s) => s)
9
9
 
10
10
  const handleChange = (e) => {
11
+ localStorage.setItem('institution_key', e.target.value)
11
12
  window.location.replace(
12
13
  `${window.location.origin}/${urlTenantKey}/${e.target.value}`,
13
14
  )
@@ -1,7 +1,8 @@
1
1
  import { Store } from 'pullstate'
2
2
  import { CustomDialog, LoginForm } from '../components'
3
- import InsititutionsDialog from '../components/Institutions'
4
3
  import { DialogProps } from '@mui/material'
4
+ import InsititutionsDialog from '../components/Institutions'
5
+ import InsititutionsNotAssignedDialog from '../components/Institutions/InsititutionsNotAssignedDialog '
5
6
 
6
7
  const getRootDialogContent = (
7
8
  key: string,
@@ -13,6 +14,8 @@ const getRootDialogContent = (
13
14
  )
14
15
  case 'institutions':
15
16
  return ({ close }) => <InsititutionsDialog close={close} />
17
+ case 'institutions-not-assigned':
18
+ return ({ close }) => <InsititutionsNotAssignedDialog />
16
19
  default:
17
20
  return () => <></>
18
21
  }
@@ -8,7 +8,6 @@ import { urlTenantKey } from '../contexts/Providers'
8
8
  import { openRootModal } from '../contexts/RootModal'
9
9
  import { AssetsStore, PermissionsStore, UserStore } from '../shared-state'
10
10
  import { InsititutionsStore } from '../shared-state/InstitutionsStore'
11
- import useInstitution from './useInstitution'
12
11
 
13
12
  const url = window.location.origin
14
13
 
@@ -65,6 +64,59 @@ const checkIsAdmin = (user) => {
65
64
  )
66
65
  return profile ? (profile.isAdmin == true ? 1 : 0) : 0
67
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
+ InsititutionsStore.update((s) => {
100
+ s.current = institutions[0]
101
+ s.institutions = institutions
102
+ })
103
+ }
104
+ if (institutions?.length > 1) {
105
+ if (!insititutionKey) {
106
+ openRootModal({
107
+ key: 'institutions',
108
+ dialogProps: {
109
+ disableEscapeKeyDown: true,
110
+ onClose: () => {},
111
+ },
112
+ })
113
+ }
114
+ InsititutionsStore.update((s) => {
115
+ s.institutions = institutions
116
+ s.current = institutions.find((item) => item.code === insititutionKey)
117
+ })
118
+ }
119
+ }
68
120
 
69
121
  const loginErrorHandler = ({
70
122
  loginUrl,
@@ -75,7 +127,7 @@ const loginErrorHandler = ({
75
127
  setLoading?: any
76
128
  err: AxiosError
77
129
  }) => {
78
- // setLoading && setLoading(false)
130
+ setLoading && setLoading(false)
79
131
  const origin = window.location.origin
80
132
  const isStaging = origin.split('campx')[1] === '.dev'
81
133
 
@@ -107,9 +159,7 @@ const loginErrorHandler = ({
107
159
  function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
108
160
  const [loading, setLoading] = useState<boolean>(false)
109
161
  const [data, setData] = useState(null)
110
- const { current } = InsititutionsStore.useState((s) => s)
111
-
112
- const { loading: loadingInstituition } = useInstitution()
162
+ const { current } = InsititutionsStore.useState()
113
163
 
114
164
  const appInit = async () => {
115
165
  setLoading(true)
@@ -119,7 +169,6 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
119
169
  const origin = window.location.origin
120
170
  const originSubdomain =
121
171
  window.location.host.split('.')?.slice(-3)[0] ?? 'ums'
122
-
123
172
  const isStaging = origin.split('campx')[1] === '.dev'
124
173
 
125
174
  if (isDevelopment == false && isStaging == false) {
@@ -132,7 +181,10 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
132
181
  }
133
182
  }
134
183
 
135
- setLoading(false)
184
+ if (res.data?.user?.institutions) {
185
+ handleInstitutions(res.data?.user?.institutions)
186
+ }
187
+
136
188
  setData(res.data)
137
189
  UserStore.update((s) => {
138
190
  s.username = res.data?.user?.username
@@ -158,6 +210,8 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
158
210
  s.logo = res.data?.assets.logo
159
211
  s.logo_square = res.data?.assets.logo_square
160
212
  })
213
+
214
+ setLoading(false)
161
215
  })
162
216
  .catch((err: AxiosError) => {
163
217
  loginErrorHandler({ loginUrl, setLoading, err })
@@ -169,7 +223,7 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
169
223
  }, [])
170
224
 
171
225
  return {
172
- loading: loading || !data?.permissions || !current || loadingInstituition,
226
+ loading: loading || !data?.permissions || !current,
173
227
  data,
174
228
  }
175
229
  }
@@ -1,94 +0,0 @@
1
- import { useEffect, useState } from 'react'
2
- import { institutions } from '../components/Institutions/services'
3
- import { urlTenantKey } from '../contexts/Providers'
4
- import { openRootModal } from '../contexts/RootModal'
5
- import { InsititutionsStore } from '../shared-state/InstitutionsStore'
6
-
7
- const getInstitutionKey = () => {
8
- const instituitionKey = window.location.pathname.split('/')[2]
9
- // if (!instituitionKey) {
10
- // const localInstituitionKey = localStorage.getItem('institution_key')
11
- // if (localInstituitionKey) {
12
- // return localInstituitionKey
13
- // } else {
14
- // return null
15
- // }
16
- // } else {
17
- // return instituitionKey
18
- // }
19
- return instituitionKey
20
- }
21
-
22
- const getInstitutions = async (startLoading, stopLoading) => {
23
- const insititutionKey = getInstitutionKey()
24
- try {
25
- startLoading()
26
- const data = await institutions.fetchInsititutions()
27
- InsititutionsStore.update((s) => {
28
- s.institutions = data?.institutions
29
- })
30
- if (data?.institutions?.length === 1 && !insititutionKey) {
31
- window.location.replace(
32
- `${window.location.origin}/${urlTenantKey}/${data?.institutions[0]?.code}`,
33
- )
34
- }
35
- if (data?.institutions?.length > 1 && !insititutionKey) {
36
- openRootModal({
37
- key: 'institutions',
38
- dialogProps: {
39
- disableEscapeKeyDown: true,
40
- onClose: () => {},
41
- },
42
- })
43
- }
44
- stopLoading()
45
- } catch (error) {
46
- stopLoading()
47
- }
48
- }
49
-
50
- const getInstitutionsByCode = async (code, startLoading, stopLoading) => {
51
- try {
52
- startLoading()
53
- const data = await institutions.fetchInsititutionByCode(code)
54
- InsititutionsStore.update((s) => {
55
- s.current = data
56
- })
57
- stopLoading()
58
- } catch (error) {
59
- stopLoading()
60
- openRootModal({
61
- key: 'institutions',
62
- dialogProps: {
63
- disableEscapeKeyDown: true,
64
- onClose: () => {},
65
- },
66
- })
67
- }
68
- }
69
-
70
- export default function useInstitution() {
71
- const insititutionKey = getInstitutionKey()
72
- const [loading, setLoading] = useState(false)
73
-
74
- const startLoading = () => {
75
- setLoading(true)
76
- }
77
- const stopLoading = () => {
78
- setLoading(false)
79
- }
80
-
81
- useEffect(() => {
82
- getInstitutions(startLoading, stopLoading)
83
- }, [insititutionKey])
84
-
85
- useEffect(() => {
86
- if (insititutionKey) {
87
- getInstitutionsByCode(insititutionKey, startLoading, stopLoading)
88
- }
89
- }, [insititutionKey])
90
-
91
- return {
92
- loading: loading,
93
- }
94
- }