@campxdev/shared 1.11.7-0.alpha.51 → 1.11.7-0.alpha.53

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.11.7-0.alpha.51",
3
+ "version": "1.11.7-0.alpha.53",
4
4
  "main": "./exports.ts",
5
5
  "scripts": {
6
6
  "start": "react-scripts start",
@@ -1,5 +1,7 @@
1
1
  import { AxiosError } from 'axios'
2
+ import Cookies from 'js-cookie'
2
3
  import { useEffect, useState } from 'react'
4
+ import { useNavigate } from 'react-router-dom'
3
5
  import { toast } from 'react-toastify'
4
6
  import axios from '../config/axios'
5
7
  import { isDevelopment } from '../constants'
@@ -93,45 +95,6 @@ function handleInstitutions(institutions) {
93
95
  }
94
96
  }
95
97
 
96
- const loginErrorHandler = ({
97
- loginUrl,
98
- setLoading,
99
- err,
100
- }: {
101
- loginUrl: string
102
- setLoading?: any
103
- err: AxiosError
104
- }) => {
105
- setLoading && setLoading(false)
106
- const origin = window.location.origin
107
- const isStaging = origin.split('campx')[1] === '.dev'
108
- const isSetup = window.location.hostname.split('.').includes('setup')
109
-
110
- if (isDevelopment || isStaging || isSetup) {
111
- openRootModal({
112
- key: 'login',
113
- contentData: {
114
- loginUrl,
115
- },
116
- dialogProps: {
117
- disableEscapeKeyDown: true,
118
- onClose: () => {},
119
- },
120
- })
121
- return
122
- } else {
123
- window.location.replace(`https://www.id.campx.in/?redirect_to=${url}`)
124
- }
125
-
126
- if (err.response.status !== 401) {
127
- if (err.response.status > 400 && err.response.status < 500) {
128
- window.location.replace(`https://www.id.campx.in/?redirect_to=${url}`)
129
- } else {
130
- toast.error('Server Error')
131
- }
132
- }
133
- }
134
-
135
98
  function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
136
99
  const [loading, setLoading] = useState<boolean>(false)
137
100
  const [data, setData] = useState(null)
@@ -142,30 +105,7 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
142
105
  axios
143
106
  .get(permissionsEndpoint)
144
107
  .then((res) => {
145
- const origin = window.location.origin
146
- const originSubdomain =
147
- window.location.host.split('.')?.slice(-3)[0] ?? 'ums'
148
- const isStaging = origin.split('campx')[1] === '.dev'
149
108
  const isAdmin = checkIsAdmin(res.data.user)
150
- const isSetup =
151
- window.location.hostname.split('.').includes('setup') && isAdmin
152
- const isMasterSlave = res.data?.institutionType === 'MASTER_CHILD'
153
-
154
- // eslint-disable-next-line no-console
155
-
156
- if (isDevelopment == false && isStaging == false && !isSetup) {
157
- if (
158
- !res.data.applications.includes(ApplicationObj[originSubdomain])
159
- ) {
160
- window.location.replace(
161
- `https://www.id.campx.in/apps?redirect_to=${url}`,
162
- )
163
- }
164
- }
165
-
166
- if (res.data?.institutions) {
167
- handleInstitutions(res.data?.institutions)
168
- }
169
109
 
170
110
  setData(res.data)
171
111
  UserStore.update((s) => {
@@ -198,6 +138,14 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
198
138
  s.isMasterInstitution = res.data?.isMasterInstitution
199
139
  s.isHomePageEnabled = res.data?.isHomePageEnabled
200
140
  })
141
+
142
+ InstitutionsStore.update((s) => {
143
+ s.institutions = res.data?.institutions
144
+ s.current = res.data?.institutions.find(
145
+ (item) => item.code === Cookies.get('campx_institution'),
146
+ )
147
+ })
148
+
201
149
  AssetsStore.update((s) => {
202
150
  s.logo = res.data?.assets.logo
203
151
  s.logo_square = res.data?.assets.logo_square
@@ -206,7 +154,26 @@ function useAuth({ permissionsEndpoint, loginUrl }: AuthParams): AuthResponse {
206
154
  setLoading(false)
207
155
  })
208
156
  .catch((err: AxiosError) => {
209
- loginErrorHandler({ loginUrl, setLoading, err })
157
+ const navigate = useNavigate()
158
+ setLoading && setLoading(false)
159
+ const origin = window.location.origin
160
+ const isStaging = origin.split('campx')[1] === '.dev'
161
+
162
+ if (isDevelopment || isStaging) {
163
+ openRootModal({
164
+ key: 'login',
165
+ contentData: {
166
+ loginUrl,
167
+ },
168
+ dialogProps: {
169
+ disableEscapeKeyDown: true,
170
+ onClose: () => {},
171
+ },
172
+ })
173
+ return
174
+ } else {
175
+ navigate('/auth/login')
176
+ }
210
177
  })
211
178
  }
212
179