@campxdev/shared 2.0.0 → 2.0.2

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": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "main": "./exports.ts",
5
5
  "scripts": {
6
6
  "start": "react-scripts start",
@@ -1,6 +1,5 @@
1
1
  import { Alert, Box, Button, styled, Typography } from '@mui/material'
2
- import Cookies from 'js-cookie'
3
- import { useEffect, useState } from 'react'
2
+ import { useNavigate } from 'react-router-dom'
4
3
  import {
5
4
  internalserver,
6
5
  nointernet,
@@ -8,10 +7,7 @@ import {
8
7
  permissiondenied,
9
8
  unauth,
10
9
  } from '../../assets/images'
11
- import axios from '../../config/axios'
12
10
  import { openRootModal } from '../../contexts/RootModal'
13
- import { PermissionsStore } from '../../shared-state'
14
- import { useModal } from '../DrawerWrapper/DrawerWrapper'
15
11
 
16
12
  const StyledAlert = styled(Alert)(({ theme }) => ({
17
13
  height: '60px',
@@ -26,13 +22,12 @@ const StyledAlert = styled(Alert)(({ theme }) => ({
26
22
  },
27
23
  position: 'relative',
28
24
  '& .retryBtn': {
29
- color: '#661b2a',
25
+ color: '#661B2A',
30
26
  position: 'absolute',
31
27
  right: 8,
32
28
  top: 8,
33
29
  },
34
30
  }))
35
-
36
31
  const StyledBox = styled(Box)(({ theme }) => ({
37
32
  width: '100%',
38
33
  display: 'flex',
@@ -40,32 +35,26 @@ const StyledBox = styled(Box)(({ theme }) => ({
40
35
  alignItems: 'center',
41
36
  justifyContent: 'center',
42
37
  }))
43
-
44
38
  export default function ErrorFallback({ error, resetErrorBoundary }) {
45
39
  if (error?.response?.status) {
46
40
  switch (error?.response?.status) {
47
41
  case 401:
48
42
  return <UnAuth resetBoundary={resetErrorBoundary} />
49
-
50
43
  case 500:
51
44
  return (
52
45
  <InternalServer resetBoundary={resetErrorBoundary} error={error} />
53
46
  )
54
-
55
47
  case 403:
56
48
  return <PemissionDenied resetBoundary={resetErrorBoundary} />
57
-
58
49
  default:
59
50
  return (
60
51
  <InternalServer resetBoundary={resetErrorBoundary} error={error} />
61
52
  )
62
53
  }
63
54
  }
64
-
65
55
  if (error?.message === 'Network Error') {
66
56
  return <NoInternet resetBoundary={resetErrorBoundary} />
67
57
  }
68
-
69
58
  return (
70
59
  <Box sx={{ marginTop: '16px', padding: '20px' }}>
71
60
  <StyledAlert severity="error">
@@ -83,7 +72,6 @@ export default function ErrorFallback({ error, resetErrorBoundary }) {
83
72
  </Box>
84
73
  )
85
74
  }
86
-
87
75
  export function PageNotFound({ resetBoundary }) {
88
76
  return (
89
77
  <>
@@ -102,7 +90,6 @@ export function PageNotFound({ resetBoundary }) {
102
90
  </>
103
91
  )
104
92
  }
105
-
106
93
  export function NoInternet({ resetBoundary }) {
107
94
  return (
108
95
  <>
@@ -116,7 +103,6 @@ export function NoInternet({ resetBoundary }) {
116
103
  </>
117
104
  )
118
105
  }
119
-
120
106
  export function PemissionDenied({ resetBoundary }) {
121
107
  return (
122
108
  <>
@@ -130,7 +116,6 @@ export function PemissionDenied({ resetBoundary }) {
130
116
  </>
131
117
  )
132
118
  }
133
-
134
119
  export function InternalServer({ resetBoundary, error }) {
135
120
  return (
136
121
  <>
@@ -158,66 +143,20 @@ export function InternalServer({ resetBoundary, error }) {
158
143
  </>
159
144
  )
160
145
  }
161
-
162
146
  export function UnAuth({ resetBoundary }) {
163
- const modal = useModal()
164
- const [username, setUsername] = useState('')
165
- const [loading, setLoading] = useState(false)
147
+ const navigate = useNavigate()
166
148
 
167
- const url = window.location.origin
168
- const origin = window?.location?.origin?.split('.')
169
- const isDevelopment =
170
- process.env.NODE_ENV === 'development' ||
171
- origin?.slice(-2).join('.') === 'campx.dev'
172
- const isSetup = window.location.hostname.split('.').includes('setup')
173
- const sessionCookie = Cookies.get('campx_session_key')
149
+ const isDevelopment = process.env.NODE_ENV === 'development'
174
150
 
175
- const appinit = async () => {
176
- setLoading(true)
177
- await axios
178
- .get('/auth/my-permissions')
179
- .then((res) => {
180
- setUsername(res.data.user.fullName)
181
- PermissionsStore.update((s) => {
182
- s.permissions = {
183
- ...res.data?.permissions,
184
- can_settings_view: 1,
185
- can_dashboard_view: 1,
186
- can_individual_pages_view: 1,
187
- } as any
188
- })
189
- setLoading(false)
190
- })
191
- .catch((e) => {
192
- openRootModal({
193
- key: 'login',
194
- })
195
- })
196
- }
197
- function LoginPage() {
151
+ function handleLoginClick() {
198
152
  if (isDevelopment) {
199
- if (!sessionCookie) {
200
- openRootModal({
201
- key: 'login',
202
- })
203
- } else {
204
- appinit()
205
- }
153
+ openRootModal({
154
+ key: 'login',
155
+ })
206
156
  } else {
207
- if (!sessionCookie) {
208
- window.location.replace(`https://www.id.campx.in/?redirect_to=${url}`)
209
- } else {
210
- appinit()
211
- }
157
+ navigate('/auth/login')
212
158
  }
213
159
  }
214
-
215
- useEffect(() => {
216
- if (!isDevelopment && !isSetup) {
217
- window.location.replace(`https://www.id.campx.in/?redirect_to=${url}`)
218
- }
219
- }, [isDevelopment])
220
-
221
160
  return (
222
161
  <>
223
162
  <StyledBox>
@@ -238,8 +177,7 @@ export function UnAuth({ resetBoundary }) {
238
177
  >
239
178
  Try Again
240
179
  </Button>
241
-
242
- <Button onClick={() => LoginPage()}>Click here to Login</Button>
180
+ <Button onClick={handleLoginClick}>Click here to Login</Button>
243
181
  </Box>
244
182
  </StyledBox>
245
183
  </>
@@ -23,6 +23,7 @@ import ActionButton from './ActionButton'
23
23
  import { FormTextField } from './HookForm'
24
24
  import Image from './Image/Image'
25
25
  import ResetPassword from './ResetPassword'
26
+ const isDevelopment = process.env.NODE_ENV == 'development'
26
27
 
27
28
  export function LoginForm({
28
29
  loginUrl,
@@ -61,12 +62,14 @@ export function LoginForm({
61
62
  ...deviceState.deviceInformation,
62
63
  },
63
64
  })
64
- Cookies.remove('campx_session_key')
65
- Cookies.remove('campx_tenant')
66
- Cookies.remove('campx_institution')
67
- Cookies.set('campx_session_key', res.data?.token)
68
- Cookies.set('campx_tenant', res.data?.subDomain)
69
- Cookies.set('campx_institution', res.data?.institutionCode)
65
+ if (isDevelopment) {
66
+ Cookies.remove('campx_session_key')
67
+ Cookies.remove('campx_tenant')
68
+ Cookies.remove('campx_institution')
69
+ Cookies.set('campx_session_key', res.data?.token)
70
+ Cookies.set('campx_tenant', res.data?.subDomain)
71
+ Cookies.set('campx_institution', res.data?.institutionCode)
72
+ }
70
73
  window.location.href = `/${res.data.institutionCode}`
71
74
  //window.location.reload()
72
75
  } catch (err) {
@@ -5,15 +5,11 @@ import { NetworkStore } from '../components/ErrorBoundary/GlobalNetworkLoadingIn
5
5
 
6
6
  const isDevelopment = process.env.NODE_ENV == 'development'
7
7
 
8
- const sessionKey = Cookies.get('campx_session_key')
9
8
  const tenantCode = isDevelopment
10
9
  ? Cookies.get('campx_tenant')
11
10
  : window.location.hostname.split('.')[0]
12
11
  const institutionCode = window.location.pathname.split('/')[1]
13
12
 
14
- const openPaymentsKey = Cookies.get('campx_open_payments_key')
15
- console.log(openPaymentsKey)
16
-
17
13
  export const formatParams = (params) => {
18
14
  return Object.fromEntries(
19
15
  Object.entries(params ?? {})?.map((i) => [
@@ -29,10 +25,6 @@ let axios = Axios.create({
29
25
  headers: {
30
26
  'x-tenant-id': tenantCode,
31
27
  'x-institution-code': institutionCode,
32
- campx_session_key: sessionKey,
33
- ...(openPaymentsKey && {
34
- campx_open_payments_key: openPaymentsKey,
35
- }),
36
28
  },
37
29
  })
38
30
 
@@ -42,7 +34,21 @@ axios.interceptors.request.use(
42
34
  NetworkStore.update((s) => {
43
35
  s.loading = true
44
36
  })
45
- return { ...config, params }
37
+ const sessionKey = Cookies.get('campx_session_key')
38
+ const openPaymentsKey = Cookies.get('campx_open_payments_key')
39
+
40
+ if (isDevelopment && sessionKey) {
41
+ config.headers.set('campx_session_key', sessionKey)
42
+ }
43
+
44
+ if (openPaymentsKey) {
45
+ config.headers.set('campx_open_payments_key', openPaymentsKey)
46
+ }
47
+
48
+ return {
49
+ ...config,
50
+ params,
51
+ }
46
52
  },
47
53
  function (error) {
48
54
  NetworkStore.update((s) => {
@@ -171,11 +171,10 @@ const useAuth = ({
171
171
  }
172
172
  }
173
173
  }
174
- console.log(loading, !data?.permissions, !current, 'abc1')
174
+
175
175
  useEffect(() => {
176
176
  appInit()
177
177
  }, [])
178
- console.log(loading, !data?.permissions, !current, 'abc2')
179
178
 
180
179
  return {
181
180
  loading: loading || !data?.permissions || !current,