You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

263 lines
8.9 KiB

  1. // import { Link } from 'react-router-dom';
  2. import React, { useState, useRef } from 'react';
  3. // material-ui
  4. import {
  5. Stepper,
  6. Step,
  7. StepButton,
  8. // Grid,
  9. Stack,
  10. Typography,
  11. Button, StepLabel,
  12. CircularProgress,
  13. } from '@mui/material';
  14. import { POST_USERNAME, POST_VERIFY_CAPTCHA } from "utils/ApiPathConst";
  15. import RegisterStepIcon, { registerStepStyle } from 'components/RegisterStepIcon';
  16. // project import
  17. import Loadable from 'components/Loadable';
  18. import { lazy } from 'react';
  19. import { notifyActionError } from 'utils/CommonFunction';
  20. const CustomFormWizard = Loadable(lazy(() => import('./auth-forms/BusCustomFormWizard')));
  21. const AuthWrapper = Loadable(lazy(() => import('./AuthWrapperCustom')));
  22. import axios from "axios";
  23. import {FormattedMessage, useIntl} from "react-intl";
  24. import { PRIMARY_CONTAINED_BUTTON_SX } from "themes/colorConst";
  25. // import CustomFormWizard from './auth-forms/BusCustomFormWizard';
  26. // import AuthWrapper from './AuthWrapperCustom';
  27. // ================================|| REGISTER ||================================ //
  28. const BusRegister = () => {
  29. const [activeStep, setActiveStep] = useState(0);
  30. const [completed, setCompleted] = useState([false]);
  31. const [updateValid, setUpdateValid] = useState(false);
  32. const step0GuardRef = useRef(null);
  33. const nextBusyRef = useRef(false);
  34. const [username, setUsername] = useState("")
  35. const [base64Url, setBase64Url] = useState("")
  36. const [checkCode, setCheckCode] = useState("")
  37. const [isNextBusy, setIsNextBusy] = useState(false);
  38. const [registrationComplete, setRegistrationComplete] = useState(false);
  39. const intl = useIntl();
  40. const steps = [
  41. intl.formatMessage({id: 'personalInformation'}),
  42. intl.formatMessage({id: 'preview'}),
  43. intl.formatMessage({id: 'finishSubmission'})
  44. ];
  45. const totalSteps = () => {
  46. return steps.length;
  47. };
  48. const completedSteps = () => {
  49. return Object.keys(completed).length;
  50. };
  51. const isLastStep = () => {
  52. return activeStep === totalSteps() - 1;
  53. };
  54. const allStepsCompleted = () => {
  55. return completedSteps() === totalSteps();
  56. };
  57. const handleCheckUsername = async () => {
  58. const response = await axios.post(`${POST_USERNAME}`, {
  59. u1: username,
  60. })
  61. return Number(response.data[0]) > 0
  62. }
  63. const handleCaptcha = async () => {
  64. const response = await axios.post(`${POST_VERIFY_CAPTCHA}`, {
  65. captcha: base64Url,
  66. checkCode: checkCode,
  67. })
  68. return Boolean(response.data["success"]);
  69. }
  70. const handleNext = async () => {
  71. if (nextBusyRef.current) return;
  72. nextBusyRef.current = true;
  73. setIsNextBusy(true);
  74. try {
  75. if (activeStep === 0 && step0GuardRef.current) {
  76. const step0Ok = await Promise.resolve(step0GuardRef.current());
  77. if (!step0Ok) {
  78. return;
  79. }
  80. }
  81. const captchaTest = await handleCaptcha();
  82. if (!captchaTest) {
  83. notifyActionError(intl.formatMessage({id: 'validVerify'}))
  84. return;
  85. }
  86. const test = await handleCheckUsername()
  87. if (test) {
  88. notifyActionError(intl.formatMessage({id: 'usernameTaken'}))
  89. } else {
  90. const newActiveStep =
  91. isLastStep() && !allStepsCompleted()
  92. ? // It's the last step, but not all steps have been completed,
  93. // find the first step that has been completed
  94. steps.findIndex((step, i) => !(i in completed))
  95. : activeStep + 1;
  96. setActiveStep(newActiveStep);
  97. scrollToTop();
  98. }
  99. } finally {
  100. nextBusyRef.current = false;
  101. setIsNextBusy(false);
  102. }
  103. };
  104. const handleBack = () => {
  105. scrollToTop();
  106. setActiveStep((prevActiveStep) => prevActiveStep - 1);
  107. };
  108. const scrollToTop = () => {
  109. window.scrollTo(0, 0);
  110. };
  111. const handleReset = () => {
  112. setActiveStep(0);
  113. setCompleted({});
  114. setRegistrationComplete(false);
  115. };
  116. const isStepCompleted = (index) => index < activeStep || registrationComplete;
  117. return (
  118. // <AuthWrapper>
  119. <Stack sx={{ width: '100%', fontSize: '2rem', paddingTop: '35px', bgcolor: 'backgroundColor.default' }} alignItems="center">
  120. <Stepper activeStep={activeStep} sx={registerStepStyle}>
  121. {steps.map((label, index) => (
  122. <Step key={label} completed={isStepCompleted(index)} readOnly={true}>
  123. <StepButton
  124. aria-current={activeStep === index ? "step" : undefined}
  125. >
  126. <StepLabel
  127. StepIconComponent={RegisterStepIcon}
  128. sx={{
  129. flexDirection: "column",
  130. "& .MuiStepLabel-iconContainer": { paddingRight: 0 },
  131. }}
  132. >
  133. <Typography variant="step1">{label}</Typography>
  134. </StepLabel>
  135. </StepButton>
  136. </Step>
  137. ))}
  138. </Stepper>
  139. {allStepsCompleted() ? (
  140. <React.Fragment>
  141. <Typography component="span" variant="h4" sx={{ mt: 2, mb: 1, display: 'block' }}>
  142. All steps completed - you&apos;re finished
  143. </Typography>
  144. <Stack direction="row" sx={{ pb: 2 }}>
  145. <Stack sx={{ flex: '1 1 auto' }} />
  146. <Button variant="h5" onClick={handleReset}>Reset</Button>
  147. </Stack>
  148. </React.Fragment>
  149. ) : (
  150. <React.Fragment>
  151. <AuthWrapper>
  152. <CustomFormWizard
  153. setUpdateValid={setUpdateValid}
  154. step0GuardRef={step0GuardRef}
  155. step={activeStep}
  156. setUsername={setUsername}
  157. setBase64Url={setBase64Url}
  158. setCheckCode={setCheckCode}
  159. onRegistrationComplete={() => setRegistrationComplete(true)}
  160. />
  161. {/* <CustomFormWizard step={activeStep} /> */}
  162. </AuthWrapper>
  163. <Stack direction="row" sx={{ pb: 2, mt:-4, mb:2 }}>
  164. {activeStep === totalSteps() - 1 ? (
  165. <Button
  166. color="inherit"
  167. disabled={true}
  168. onClick={handleBack}
  169. sx={{ mr: 1 }}
  170. variant="h5"
  171. >
  172. <Typography component="span" variant="h5">
  173. <FormattedMessage id="back"/>
  174. </Typography>
  175. </Button>
  176. ) : (
  177. <Button
  178. color="inherit"
  179. disabled={activeStep === 0 || isNextBusy}
  180. onClick={handleBack}
  181. sx={{ mr: 1 }}
  182. variant="h5"
  183. >
  184. <Typography component="span" variant="h5">
  185. <FormattedMessage id="back"/>
  186. </Typography>
  187. </Button>
  188. )
  189. }
  190. <Stack sx={{ flex: '1 1 auto' }} />
  191. {activeStep === totalSteps() - 2 ?
  192. (
  193. <Button
  194. variant="contained"
  195. disabled={isNextBusy}
  196. onClick={handleNext}
  197. aria-busy={isNextBusy}
  198. sx={{ mr: 1, minWidth: 120, ...PRIMARY_CONTAINED_BUTTON_SX }}
  199. >
  200. {isNextBusy ? (
  201. <CircularProgress size={22} color="inherit" aria-hidden />
  202. ) : (
  203. <Typography component="span" variant="h5" sx={{ color: 'inherit' }}>
  204. <FormattedMessage id="submit"/>
  205. </Typography>
  206. )}
  207. </Button>
  208. ) : (activeStep === totalSteps() - 1 ?
  209. (
  210. <Button variant="contained" color="inherit"
  211. disabled={true} sx={{ mr: 1, ...PRIMARY_CONTAINED_BUTTON_SX }}>
  212. <Typography component="span" variant="h5" sx={{ color: 'inherit' }}>
  213. <FormattedMessage id="submit"/>
  214. </Typography>
  215. </Button>
  216. ) :
  217. (
  218. // <Button disabled={updateValid} variant="outlined" onClick={handleNext} sx={{ mr: 1 }}>
  219. <Button disabled={!updateValid || isNextBusy} variant="contained" onClick={handleNext} sx={{ mr: 1, ...PRIMARY_CONTAINED_BUTTON_SX }}>
  220. <Typography component="span" variant="h5" sx={{ color: 'inherit' }}>
  221. <FormattedMessage id="continue"/>
  222. </Typography>
  223. </Button>
  224. )
  225. )}
  226. {/* {activeStep !== steps.length &&
  227. (completed[activeStep] ? (
  228. <Typography variant="caption" sx={{ display: 'inline-block' }}>
  229. Step {activeStep + 1} already completed
  230. </Typography>
  231. ) : (
  232. <Button onClick={handleComplete}>
  233. {completedSteps() === totalSteps() - 1
  234. ? 'Finish'
  235. : 'Complete Step'}
  236. </Button>
  237. ))} */}
  238. </Stack>
  239. </React.Fragment>
  240. )}
  241. </Stack >
  242. // </AuthWrapper>
  243. );
  244. };
  245. export default BusRegister;