Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

274 righe
8.9 KiB

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