@campxdev/shared 1.8.3 → 1.8.5-0.alpha-1

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.
Files changed (233) hide show
  1. package/antd.customize.less +73 -73
  2. package/exports.ts +19 -19
  3. package/package.json +86 -86
  4. package/src/assets/fonts/avenir/index.ts +13 -13
  5. package/src/assets/fonts/poppins/index.ts +18 -18
  6. package/src/assets/images/index.ts +17 -17
  7. package/src/assets/images/unauth.svg +92 -92
  8. package/src/components/ActionButton.tsx +20 -20
  9. package/src/components/ApplicationProfile/ApplicationProfile.tsx +342 -0
  10. package/src/components/ApplicationProfile/Service.ts +68 -0
  11. package/src/components/ApplicationProfile/UserProfileRelation.tsx +174 -0
  12. package/src/components/ApplicationProfile/index.tsx +1 -0
  13. package/src/components/Attachment.tsx +26 -26
  14. package/src/components/AutocompleteSearch/AutocompleteSearch.tsx +57 -57
  15. package/src/components/AutocompleteSearch/index.tsx +1 -1
  16. package/src/components/Breadcrumbs.tsx +72 -72
  17. package/src/components/Card.tsx +98 -98
  18. package/src/components/CardsGrid.tsx +28 -28
  19. package/src/components/ChangePassword.tsx +123 -123
  20. package/src/components/Chips.tsx +81 -81
  21. package/src/components/Detail.tsx +15 -15
  22. package/src/components/DetailsGrid.tsx +52 -52
  23. package/src/components/DividerHeading.tsx +41 -41
  24. package/src/components/DrawerWrapper/DialogWrapper.tsx +67 -67
  25. package/src/components/DrawerWrapper/DrawerTemplate.tsx +53 -53
  26. package/src/components/DrawerWrapper/DrawerWrapper.tsx +53 -53
  27. package/src/components/DropDownButton/AnchorElement.tsx +44 -44
  28. package/src/components/DropDownButton/DropDownButton.tsx +184 -184
  29. package/src/components/DropDownButton/DropdownMenuItem.tsx +122 -110
  30. package/src/components/DropDownButton/index.tsx +1 -1
  31. package/src/components/DropDownButton/styles.tsx +59 -59
  32. package/src/components/ErrorBoundary/ErrorBoundary.tsx +33 -28
  33. package/src/components/ErrorBoundary/ErrorFallback.tsx +241 -236
  34. package/src/components/ErrorBoundary/GlobalNetworkLoadingIndicator.tsx +13 -13
  35. package/src/components/ErrorBoundary/index.tsx +1 -1
  36. package/src/components/ErrorBox.tsx +42 -42
  37. package/src/components/ExcelToJsonInput/ExcelJsonUpload.tsx +50 -50
  38. package/src/components/ExcelToJsonInput/ExcelToJsonInput.tsx +57 -57
  39. package/src/components/ExcelToJsonInput/index.tsx +1 -1
  40. package/src/components/FilterComponents/FilterButton.tsx +56 -56
  41. package/src/components/FilterComponents/SearchBar.tsx +45 -45
  42. package/src/components/FloatingContainer.tsx +33 -33
  43. package/src/components/Form/Form.tsx +147 -147
  44. package/src/components/Form/RenderForm.tsx +189 -189
  45. package/src/components/FullScreenLoader.tsx +19 -19
  46. package/src/components/HookForm/AutoCompleteSearch.tsx +113 -113
  47. package/src/components/HookForm/DatePicker.tsx +40 -40
  48. package/src/components/HookForm/DateTimePicker.tsx +47 -47
  49. package/src/components/HookForm/FormLabel.tsx +14 -14
  50. package/src/components/HookForm/MultiCheckbox.tsx +69 -69
  51. package/src/components/HookForm/MultiSelect.tsx +61 -53
  52. package/src/components/HookForm/RadioGroup.tsx +47 -47
  53. package/src/components/HookForm/SingleCheckbox.tsx +34 -34
  54. package/src/components/HookForm/SingleSelect.tsx +46 -46
  55. package/src/components/HookForm/TextField.tsx +40 -40
  56. package/src/components/HookForm/TimePicker.tsx +40 -40
  57. package/src/components/HookForm/index.ts +23 -23
  58. package/src/components/IconButtons/IconButtons.tsx +137 -137
  59. package/src/components/IconButtons/Icons.tsx +268 -268
  60. package/src/components/IconButtons/assets/edit.svg +4 -4
  61. package/src/components/IconButtons/assets/eye.svg +6 -6
  62. package/src/components/IconButtons/assets/trash.svg +7 -7
  63. package/src/components/IconButtons/index.tsx +8 -8
  64. package/src/components/IconLabel.tsx +37 -37
  65. package/src/components/Image/Image.tsx +45 -45
  66. package/src/components/Image/index.tsx +1 -1
  67. package/src/components/ImageUpload.tsx +98 -98
  68. package/src/components/Input/AsyncSearchSelect/AsyncSearchSelect.tsx +216 -216
  69. package/src/components/Input/AsyncSearchSelect/index.tsx +1 -1
  70. package/src/components/Input/AsyncSearchSelect/styles.tsx +105 -105
  71. package/src/components/Input/AutoCompleteSearch.tsx +121 -121
  72. package/src/components/Input/DatePicker.tsx +87 -76
  73. package/src/components/Input/DateRangePicker.tsx +105 -105
  74. package/src/components/Input/DateTimePicker.tsx +84 -84
  75. package/src/components/Input/FormLabel.tsx +21 -21
  76. package/src/components/Input/MultiCheckbox.tsx +64 -64
  77. package/src/components/Input/MultiSelect.tsx +161 -150
  78. package/src/components/Input/RadioGroup.tsx +104 -104
  79. package/src/components/Input/SingleCheckbox.tsx +78 -78
  80. package/src/components/Input/SingleSelect.tsx +104 -104
  81. package/src/components/Input/TextField.tsx +46 -46
  82. package/src/components/Input/TimePicker.tsx +77 -77
  83. package/src/components/Input/index.ts +26 -26
  84. package/src/components/Input/types.ts +3 -3
  85. package/src/components/JsonPreview.tsx +7 -7
  86. package/src/components/LabelValue.tsx +21 -21
  87. package/src/components/Layout/ChangePassword.tsx +49 -49
  88. package/src/components/Layout/Header/AppHeader.tsx +113 -112
  89. package/src/components/Layout/Header/AppsMenu.tsx +133 -105
  90. package/src/components/Layout/Header/HeaderActions/CogWheelMenu.tsx +33 -33
  91. package/src/components/Layout/Header/HeaderActions/FreshChatButton.tsx +61 -0
  92. package/src/components/Layout/Header/HeaderActions/FreshDeskHelpButton.tsx +53 -24
  93. package/src/components/Layout/Header/HeaderActions/HeaderActions.tsx +20 -18
  94. package/src/components/Layout/Header/HeaderActions/UserBox.tsx +56 -56
  95. package/src/components/Layout/Header/Notification.tsx +13 -13
  96. package/src/components/Layout/Header/SchoolSwitch/SchoolSwitch.tsx +33 -0
  97. package/src/components/Layout/Header/applications.ts +102 -103
  98. package/src/components/Layout/Header/assets/campx_square_small.svg +8 -8
  99. package/src/components/Layout/Header/assets/chat_with_us.png +0 -0
  100. package/src/components/Layout/Header/assets/commutex.svg +14 -14
  101. package/src/components/Layout/Header/assets/commutexSmall.svg +11 -11
  102. package/src/components/Layout/Header/assets/enroll.svg +14 -14
  103. package/src/components/Layout/Header/assets/enrollx.svg +14 -14
  104. package/src/components/Layout/Header/assets/exams_small.svg +11 -11
  105. package/src/components/Layout/Header/assets/examsx.svg +14 -14
  106. package/src/components/Layout/Header/assets/hostel_small.svg +13 -13
  107. package/src/components/Layout/Header/assets/hostelx.svg +13 -13
  108. package/src/components/Layout/Header/assets/index.ts +37 -35
  109. package/src/components/Layout/Header/assets/libraryx.svg +12 -12
  110. package/src/components/Layout/Header/assets/pay_small.svg +15 -15
  111. package/src/components/Layout/Header/assets/payx.svg +19 -19
  112. package/src/components/Layout/Header/assets/people_small.svg +9 -9
  113. package/src/components/Layout/Header/assets/peoplex.svg +12 -12
  114. package/src/components/Layout/Header/assets/squarex.svg +12 -12
  115. package/src/components/Layout/Header/icons.tsx +57 -57
  116. package/src/components/Layout/Header/index.tsx +1 -1
  117. package/src/components/Layout/Header/styles.tsx +133 -133
  118. package/src/components/Layout/Helmet.tsx +108 -49
  119. package/src/components/Layout/LayoutWrapper.tsx +28 -28
  120. package/src/components/Layout/SideMenuHeader.tsx +29 -29
  121. package/src/components/Layout/SideNav.tsx +168 -168
  122. package/src/components/Layout/Tickets/HelpWidget/HelpWidget.tsx +273 -273
  123. package/src/components/Layout/Tickets/HelpWidget/styles.tsx +94 -94
  124. package/src/components/Layout/Tickets/MyTickets.tsx +72 -72
  125. package/src/components/Layout/Tickets/TicketDetails.tsx +65 -65
  126. package/src/components/Layout/Tickets/TimeLine.tsx +64 -64
  127. package/src/components/Layout/Tickets/index.tsx +1 -1
  128. package/src/components/Layout/Tickets/services.ts +11 -11
  129. package/src/components/Layout/Tickets/styles.tsx +136 -136
  130. package/src/components/LinearProgress.tsx +19 -19
  131. package/src/components/ListItemButton.tsx +95 -95
  132. package/src/components/LoginForm.tsx +279 -274
  133. package/src/components/MediaRow/MediaRow.tsx +67 -67
  134. package/src/components/MediaRow/index.tsx +1 -1
  135. package/src/components/ModalButtons/DialogButton.tsx +126 -126
  136. package/src/components/ModalButtons/DrawerButton.tsx +115 -115
  137. package/src/components/ModalButtons/PopoverButton.tsx +99 -0
  138. package/src/components/ModalButtons/index.tsx +4 -4
  139. package/src/components/NoDataIllustration.tsx +32 -32
  140. package/src/components/PageContent.tsx +12 -12
  141. package/src/components/PageHeader.tsx +55 -55
  142. package/src/components/PageNotFound.tsx +26 -26
  143. package/src/components/PopupConfirm/ConfirmContextProvider.tsx +40 -40
  144. package/src/components/PopupConfirm/PopupConfirm.tsx +67 -67
  145. package/src/components/PopupConfirm/index.tsx +1 -1
  146. package/src/components/PopupConfirm/useConfirm.ts +47 -47
  147. package/src/components/ResetPassword.tsx +107 -107
  148. package/src/components/Row.tsx +24 -24
  149. package/src/components/Spinner.tsx +18 -18
  150. package/src/components/StepsHeader/StepsHeader.tsx +115 -115
  151. package/src/components/StepsHeader/index.tsx +1 -1
  152. package/src/components/StyledTableContainer.tsx +33 -33
  153. package/src/components/SwitchButton.tsx +41 -41
  154. package/src/components/Table.tsx +42 -42
  155. package/src/components/Tables/BasicTable/Table.tsx +198 -198
  156. package/src/components/Tables/BasicTable/TableFooter.tsx +86 -86
  157. package/src/components/Tables/BasicTable/index.tsx +1 -1
  158. package/src/components/Tables/ReactTable/BatchActionsHeader.tsx +58 -58
  159. package/src/components/Tables/ReactTable/ReactTable.tsx +295 -295
  160. package/src/components/Tables/ReactTable/RenderTableBody.tsx +49 -49
  161. package/src/components/Tables/ReactTable/index.tsx +1 -1
  162. package/src/components/Tables/ReactTable/react-table-config.d.ts +128 -128
  163. package/src/components/Tables/common/NoRecordsFound.tsx +27 -27
  164. package/src/components/Tables/common/TableStats.tsx +22 -22
  165. package/src/components/Tables/common/icons.tsx +50 -50
  166. package/src/components/Tables/common/styles.tsx +170 -170
  167. package/src/components/Tables/common/types.ts +57 -57
  168. package/src/components/Tabs/NavigationTabs.tsx +80 -80
  169. package/src/components/Tabs/Tabs.tsx +53 -53
  170. package/src/components/Tabs/TabsContainer.tsx +53 -50
  171. package/src/components/Tabs/index.tsx +1 -1
  172. package/src/components/Tabs/styles.tsx +55 -55
  173. package/src/components/ToastContainer/ToastContainer.tsx +57 -57
  174. package/src/components/ToastContainer/index.tsx +1 -1
  175. package/src/components/UploadButton/UploadButton.tsx +126 -126
  176. package/src/components/UploadButton/index.tsx +1 -1
  177. package/src/components/UploadButton/types.ts +19 -19
  178. package/src/components/UploadDocument.tsx +108 -108
  179. package/src/components/UploadFileDialog/UploadFileDialog.tsx +238 -238
  180. package/src/components/UploadFileDialog/index.tsx +1 -1
  181. package/src/components/index.ts +110 -106
  182. package/src/config/axios.ts +79 -73
  183. package/src/config/axiosEvaluator.ts +53 -53
  184. package/src/config/axiosXTenant.ts +57 -57
  185. package/src/constants/UIConstants.ts +124 -97
  186. package/src/constants/formValidations.ts +6 -6
  187. package/src/constants/index.ts +5 -5
  188. package/src/constants/isDevelopment.ts +4 -4
  189. package/src/constants/permissions.ts +67 -67
  190. package/src/constants/validateMessages.ts +12 -12
  191. package/src/contexts/LoginFormProvider.tsx +39 -44
  192. package/src/contexts/Providers.tsx +52 -50
  193. package/src/contexts/PublicProviders.tsx +30 -30
  194. package/src/contexts/QueryClientProvider.tsx +22 -22
  195. package/src/hooks/index.ts +5 -4
  196. package/src/hooks/useAuth.ts +152 -96
  197. package/src/hooks/useExternalScript.ts +38 -0
  198. package/src/hooks/useFetch.ts +53 -53
  199. package/src/hooks/useFilters.ts +83 -80
  200. package/src/hooks/useRouter.ts +31 -31
  201. package/src/layouts/Components/DashBoardMenu.tsx +232 -232
  202. package/src/layouts/Components/icons/index.tsx +403 -403
  203. package/src/layouts/Components/styles.tsx +74 -74
  204. package/src/layouts/ComponentsLayout.tsx +3 -3
  205. package/src/permissions/PageWithPermission.tsx +18 -18
  206. package/src/permissions/PermissionDeniedPage.tsx +16 -16
  207. package/src/permissions/ValidateAccess.tsx +18 -18
  208. package/src/permissions/index.ts +2 -2
  209. package/src/react-app-env.d.ts +1 -1
  210. package/src/shared-state/AssetsStore.ts +15 -15
  211. package/src/shared-state/PermissionsStore.ts +1159 -356
  212. package/src/shared-state/UserStore.ts +13 -13
  213. package/src/shared-state/index.ts +4 -4
  214. package/src/theme/App.less +3 -3
  215. package/src/theme/MuiThemeProvider.tsx +18 -18
  216. package/src/theme/customCssBaseline.ts +135 -135
  217. package/src/theme/index.css +28 -28
  218. package/src/theme/muiTheme.ts +597 -595
  219. package/src/theme/theme.d.ts +77 -75
  220. package/src/utils/adminAxios.ts +15 -15
  221. package/src/utils/alphabet.ts +23 -23
  222. package/src/utils/arrayPadEnd.ts +3 -3
  223. package/src/utils/formatCurrency.ts +9 -9
  224. package/src/utils/getUrlParams.ts +5 -5
  225. package/src/utils/index.ts +7 -7
  226. package/src/utils/logout.ts +25 -25
  227. package/src/utils/ordinalSuffixOf.ts +14 -14
  228. package/src/utils/romanize.ts +40 -40
  229. package/src/utils/withRouteWrapper.tsx +25 -25
  230. package/src/utils/withSuspense.tsx +6 -6
  231. package/styled-components.tsx +60 -60
  232. package/tsconfig.json +21 -21
  233. package/src/components/Layout/Header/assets/commuteX.png +0 -0
@@ -1,274 +1,279 @@
1
- import { Visibility, VisibilityOff } from '@mui/icons-material'
2
- import {
3
- Alert,
4
- Box,
5
- Button,
6
- IconButton,
7
- InputAdornment,
8
- Stack,
9
- Typography,
10
- styled,
11
- } from '@mui/material'
12
- import axiosBase from 'axios'
13
- import Cookies from 'js-cookie'
14
- import { useEffect, useState } from 'react'
15
- import { useForm } from 'react-hook-form'
16
- import ActionButton from './ActionButton'
17
- import { FormTextField } from './HookForm'
18
- import axios from '../config/axios'
19
- import ResetPassword from './ResetPassword'
20
- import { Link } from 'react-router-dom'
21
- import { DialogButton } from './ModalButtons'
22
- import adminAxios from '../utils/adminAxios'
23
-
24
- export function LoginForm({
25
- loginUrl,
26
- showSuperAdminForm,
27
- }: {
28
- loginUrl?: string
29
- showSuperAdminForm?: boolean
30
- }) {
31
- const [showPassword, setShowPassword] = useState(false)
32
- const [forgotMail, setForgotMail] = useState(null)
33
- const [forgotPassword, setForgotPassword] = useState(true)
34
- const [resetPassword, setResetPassword] = useState(false)
35
- const { handleSubmit, control } = useForm()
36
- const [error, setError] = useState('')
37
-
38
- const onSubmit = async (values) => {
39
- try {
40
- const res = await axiosBase({
41
- method: 'POST',
42
- baseURL: process.env.REACT_APP_API_HOST,
43
- url: loginUrl ? loginUrl : `/auth-server/auth/login`,
44
- data: values,
45
- })
46
- Cookies.set('campx_tenant', res?.data?.subDomain)
47
- Cookies.set('campx_session_key', res.data?.token)
48
- window.location.href = window.location.origin + `/${res?.data?.subDomain}`
49
- } catch (err) {
50
- // eslint-disable-next-line no-console
51
- console.log(err)
52
- setError(err?.response?.data?.message ?? 'Server Error')
53
- }
54
- }
55
-
56
- const [mailSent, setMailSent] = useState(false)
57
-
58
- async function handleSendMail() {
59
- try {
60
- await axios
61
- .get(
62
- `https://api.campx.dev/auth-server/auth/forgot-password?email=${forgotMail}`,
63
- )
64
- .then((res) => {
65
- setForgotMail(null)
66
- setMailSent(true)
67
- setError(null)
68
- })
69
- } catch (err) {
70
- setError(err.response.data.message ?? 'Server Error')
71
- setMailSent(false)
72
- }
73
- }
74
-
75
- useEffect(() => {
76
- const restLink = window.location.pathname.split('/')[1]
77
- if (restLink == 'reset-password') {
78
- setResetPassword(true)
79
- }
80
- }, [])
81
-
82
- return (
83
- <Box sx={{ position: 'relative', height: '100%' }}>
84
- {resetPassword ? (
85
- <>
86
- <ResetPassword />
87
- </>
88
- ) : forgotPassword ? (
89
- <form onSubmit={handleSubmit(onSubmit)}>
90
- <Stack gap={'30px'} sx={{ display: resetPassword ? 'none' : 'flex' }}>
91
- <FormTextField
92
- control={control}
93
- name="username"
94
- label="User ID"
95
- required
96
- />
97
- <FormTextField
98
- control={control}
99
- name="password"
100
- label="Password"
101
- type={showPassword ? 'text' : 'password'}
102
- required
103
- InputProps={{
104
- endAdornment: (
105
- <InputAdornment position="end">
106
- <IconButton
107
- size="small"
108
- aria-label="toggle password visibility"
109
- onClick={() => setShowPassword((prev) => !prev)}
110
- edge="end"
111
- >
112
- {showPassword ? <VisibilityOff /> : <Visibility />}
113
- </IconButton>
114
- </InputAdornment>
115
- ),
116
- }}
117
- />
118
- <ActionButton type="submit">Login</ActionButton>
119
- <Typography
120
- variant="h6"
121
- align="right"
122
- onClick={() => setForgotPassword(false)}
123
- >
124
- <StyledLink to="/">Forgot Password</StyledLink>
125
- </Typography>
126
- </Stack>
127
- </form>
128
- ) : (
129
- <>
130
- <Stack gap={'30px'}>
131
- <FormTextField
132
- control={control}
133
- name="email"
134
- label="Email"
135
- required
136
- onChange={(e) => setForgotMail(e.target.value)}
137
- />
138
- <Button onClick={() => handleSendMail()}>Send Email</Button>
139
-
140
- <Typography
141
- variant="subtitle2"
142
- onClick={() => {
143
- setForgotPassword(true)
144
- setError('')
145
- setMailSent(false)
146
- }}
147
- align="right"
148
- >
149
- <StyledLink to="/">Login here</StyledLink>
150
- </Typography>
151
-
152
- {mailSent && (
153
- <Alert severity="success">
154
- Reset password link has been sent to your email.
155
- </Alert>
156
- )}
157
- </Stack>
158
- </>
159
- )}
160
-
161
- {error && (
162
- <Alert severity="error" sx={{ marginTop: '20px' }}>
163
- {error}
164
- </Alert>
165
- )}
166
-
167
- {showSuperAdminForm && (
168
- <Box
169
- sx={{
170
- position: 'absolute',
171
- bottom: '20px',
172
- right: '20px',
173
- }}
174
- >
175
- <DialogButton
176
- title="Super Admin Login"
177
- anchor={({ open }) => (
178
- <Button onClick={open} size="small" variant="text">
179
- Super Admin Login
180
- </Button>
181
- )}
182
- content={({ close }) => <SuperAdminLoginForm close={close} />}
183
- />
184
- </Box>
185
- )}
186
- </Box>
187
- )
188
- }
189
-
190
- export const StyledLink = styled(Link)({
191
- textDecoration: 'none',
192
- color: 'black',
193
- })
194
-
195
- export default LoginForm
196
-
197
- const SuperAdminLoginForm = ({ close }) => {
198
- const [showPassword, setShowPassword] = useState(false)
199
- const [loading, setLoading] = useState(false)
200
- const { handleSubmit, control } = useForm({
201
- defaultValues: null,
202
- })
203
- const [error, setError] = useState('')
204
-
205
- const onSubmit = async (values) => {
206
- setLoading(true)
207
- try {
208
- const res = await adminAxios.post(`/auth/login`, values)
209
- Cookies.set('campx_admin_session_key', res?.data?.cookie)
210
-
211
- setLoading(false)
212
- window.location.href = '/campx'
213
- } catch (err) {
214
- setLoading(false)
215
- setError(err.response.data.message ?? 'Server Error')
216
- }
217
- }
218
-
219
- const onError = (e) => {
220
- console.log(e)
221
- }
222
-
223
- return (
224
- <Box width={'100%'} marginTop="20px">
225
- <form onSubmit={handleSubmit(onSubmit, onError)}>
226
- <Stack gap={'40px'}>
227
- <Typography variant="h6" textAlign={'center'}>
228
- Sign in to Super Admin Account
229
- </Typography>
230
- <FormTextField
231
- control={control}
232
- name="username"
233
- label="Username/Email"
234
- required
235
- />
236
- <FormTextField
237
- control={control}
238
- name="password"
239
- label="Password"
240
- type={showPassword ? 'text' : 'password'}
241
- required
242
- InputProps={{
243
- endAdornment: (
244
- <InputAdornment position="end">
245
- <IconButton
246
- size="small"
247
- aria-label="toggle password visibility"
248
- onClick={() => setShowPassword((prev) => !prev)}
249
- edge="end"
250
- >
251
- {showPassword ? <VisibilityOff /> : <Visibility />}
252
- </IconButton>
253
- </InputAdornment>
254
- ),
255
- }}
256
- />
257
- <ActionButton
258
- type="submit"
259
- fullWidth
260
- disabled={loading}
261
- loading={loading}
262
- >
263
- Login
264
- </ActionButton>
265
- </Stack>
266
- </form>
267
- {error && (
268
- <Alert severity="error" sx={{ marginTop: '20px' }}>
269
- {error}
270
- </Alert>
271
- )}
272
- </Box>
273
- )
274
- }
1
+ import { Visibility, VisibilityOff } from '@mui/icons-material'
2
+ import {
3
+ Alert,
4
+ Box,
5
+ Button,
6
+ IconButton,
7
+ InputAdornment,
8
+ Stack,
9
+ Typography,
10
+ styled,
11
+ } from '@mui/material'
12
+ import axiosBase from 'axios'
13
+ import Cookies from 'js-cookie'
14
+ import { useEffect, useState } from 'react'
15
+ import { useForm } from 'react-hook-form'
16
+ import ActionButton from './ActionButton'
17
+ import { FormTextField } from './HookForm'
18
+ import axios from '../config/axios'
19
+ import ResetPassword from './ResetPassword'
20
+ import { Link } from 'react-router-dom'
21
+ import { DialogButton } from './ModalButtons'
22
+ import adminAxios from '../utils/adminAxios'
23
+
24
+ export function LoginForm({
25
+ loginUrl,
26
+ showSuperAdminForm,
27
+ }: {
28
+ loginUrl?: string
29
+ showSuperAdminForm?: boolean
30
+ }) {
31
+ const [showPassword, setShowPassword] = useState(false)
32
+ const [forgotMail, setForgotMail] = useState(null)
33
+ const [forgotPassword, setForgotPassword] = useState(true)
34
+ const [resetPassword, setResetPassword] = useState(false)
35
+ const { handleSubmit, control } = useForm()
36
+ const [error, setError] = useState('')
37
+
38
+ const onSubmit = async (values) => {
39
+ try {
40
+ const res = await axiosBase({
41
+ method: 'POST',
42
+ baseURL: process.env.REACT_APP_API_HOST,
43
+ url: loginUrl ? loginUrl : `/auth-server/auth/login`,
44
+ data: values,
45
+ })
46
+ Cookies.set('campx_tenant', res?.data?.subDomain)
47
+ Cookies.set('campx_session_key', res.data?.token)
48
+ window.location.href = window.location.origin + `/${res?.data?.subDomain}`
49
+ } catch (err) {
50
+ // eslint-disable-next-line no-console
51
+ console.log(err)
52
+ setError(err?.response?.data?.message ?? 'Server Error')
53
+ }
54
+ }
55
+
56
+ const [mailSent, setMailSent] = useState(false)
57
+
58
+ async function handleSendMail() {
59
+ try {
60
+ await axios
61
+ .get(
62
+ `https://api.campx.dev/auth-server/auth/forgot-password?email=${forgotMail}`,
63
+ )
64
+ .then((res) => {
65
+ setForgotMail(null)
66
+ setMailSent(true)
67
+ setError(null)
68
+ })
69
+ } catch (err) {
70
+ if (!forgotMail) {
71
+ setError('Email should not be empty')
72
+ setMailSent(false)
73
+ } else {
74
+ setError(err.response.data.message ?? 'Server Error')
75
+ setMailSent(false)
76
+ }
77
+ }
78
+ }
79
+
80
+ useEffect(() => {
81
+ const restLink = window.location.pathname.split('/')[1]
82
+ if (restLink == 'reset-password') {
83
+ setResetPassword(true)
84
+ }
85
+ }, [])
86
+
87
+ return (
88
+ <Box sx={{ position: 'relative', height: '100%' }}>
89
+ {resetPassword ? (
90
+ <>
91
+ <ResetPassword />
92
+ </>
93
+ ) : forgotPassword ? (
94
+ <form onSubmit={handleSubmit(onSubmit)}>
95
+ <Stack gap={'30px'} sx={{ display: resetPassword ? 'none' : 'flex' }}>
96
+ <FormTextField
97
+ control={control}
98
+ name="username"
99
+ label="User ID"
100
+ required
101
+ />
102
+ <FormTextField
103
+ control={control}
104
+ name="password"
105
+ label="Password"
106
+ type={showPassword ? 'text' : 'password'}
107
+ required
108
+ InputProps={{
109
+ endAdornment: (
110
+ <InputAdornment position="end">
111
+ <IconButton
112
+ size="small"
113
+ aria-label="toggle password visibility"
114
+ onClick={() => setShowPassword((prev) => !prev)}
115
+ edge="end"
116
+ >
117
+ {showPassword ? <VisibilityOff /> : <Visibility />}
118
+ </IconButton>
119
+ </InputAdornment>
120
+ ),
121
+ }}
122
+ />
123
+ <ActionButton type="submit">Login</ActionButton>
124
+ <Typography
125
+ variant="h6"
126
+ align="right"
127
+ onClick={() => setForgotPassword(false)}
128
+ >
129
+ <StyledLink to="/">Forgot Password</StyledLink>
130
+ </Typography>
131
+ </Stack>
132
+ </form>
133
+ ) : (
134
+ <>
135
+ <Stack gap={'30px'}>
136
+ <FormTextField
137
+ control={control}
138
+ name="email"
139
+ label="Email"
140
+ required
141
+ onChange={(e) => setForgotMail(e.target.value)}
142
+ />
143
+ <Button onClick={() => handleSendMail()}>Send Email</Button>
144
+
145
+ <Typography
146
+ variant="subtitle2"
147
+ onClick={() => {
148
+ setForgotPassword(true)
149
+ setError('')
150
+ setMailSent(false)
151
+ }}
152
+ align="right"
153
+ >
154
+ <StyledLink to="/">Login here</StyledLink>
155
+ </Typography>
156
+
157
+ {mailSent && (
158
+ <Alert severity="success">
159
+ Reset password link has been sent to your email.
160
+ </Alert>
161
+ )}
162
+ </Stack>
163
+ </>
164
+ )}
165
+
166
+ {error && (
167
+ <Alert severity="error" sx={{ marginTop: '20px' }}>
168
+ {error}
169
+ </Alert>
170
+ )}
171
+
172
+ {showSuperAdminForm && (
173
+ <Box
174
+ sx={{
175
+ position: 'absolute',
176
+ bottom: '20px',
177
+ right: '20px',
178
+ }}
179
+ >
180
+ <DialogButton
181
+ title="Super Admin Login"
182
+ anchor={({ open }) => (
183
+ <Button onClick={open} size="small" variant="text">
184
+ Super Admin Login
185
+ </Button>
186
+ )}
187
+ content={({ close }) => <SuperAdminLoginForm close={close} />}
188
+ />
189
+ </Box>
190
+ )}
191
+ </Box>
192
+ )
193
+ }
194
+
195
+ export const StyledLink = styled(Link)({
196
+ textDecoration: 'none',
197
+ color: 'black',
198
+ })
199
+
200
+ export default LoginForm
201
+
202
+ const SuperAdminLoginForm = ({ close }) => {
203
+ const [showPassword, setShowPassword] = useState(false)
204
+ const [loading, setLoading] = useState(false)
205
+ const { handleSubmit, control } = useForm({
206
+ defaultValues: null,
207
+ })
208
+ const [error, setError] = useState('')
209
+
210
+ const onSubmit = async (values) => {
211
+ setLoading(true)
212
+ try {
213
+ const res = await adminAxios.post(`/auth/login`, values)
214
+ Cookies.set('campx_admin_session_key', res?.data?.cookie)
215
+
216
+ setLoading(false)
217
+ window.location.href = '/campx'
218
+ } catch (err) {
219
+ setLoading(false)
220
+ setError(err.response.data.message ?? 'Server Error')
221
+ }
222
+ }
223
+
224
+ const onError = (e) => {
225
+ console.log(e)
226
+ }
227
+
228
+ return (
229
+ <Box width={'100%'} marginTop="20px">
230
+ <form onSubmit={handleSubmit(onSubmit, onError)}>
231
+ <Stack gap={'40px'}>
232
+ <Typography variant="h6" textAlign={'center'}>
233
+ Sign in to Super Admin Account
234
+ </Typography>
235
+ <FormTextField
236
+ control={control}
237
+ name="username"
238
+ label="Username/Email"
239
+ required
240
+ />
241
+ <FormTextField
242
+ control={control}
243
+ name="password"
244
+ label="Password"
245
+ type={showPassword ? 'text' : 'password'}
246
+ required
247
+ InputProps={{
248
+ endAdornment: (
249
+ <InputAdornment position="end">
250
+ <IconButton
251
+ size="small"
252
+ aria-label="toggle password visibility"
253
+ onClick={() => setShowPassword((prev) => !prev)}
254
+ edge="end"
255
+ >
256
+ {showPassword ? <VisibilityOff /> : <Visibility />}
257
+ </IconButton>
258
+ </InputAdornment>
259
+ ),
260
+ }}
261
+ />
262
+ <ActionButton
263
+ type="submit"
264
+ fullWidth
265
+ disabled={loading}
266
+ loading={loading}
267
+ >
268
+ Login
269
+ </ActionButton>
270
+ </Stack>
271
+ </form>
272
+ {error && (
273
+ <Alert severity="error" sx={{ marginTop: '20px' }}>
274
+ {error}
275
+ </Alert>
276
+ )}
277
+ </Box>
278
+ )
279
+ }