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.
 
 

279 rivejä
9.0 KiB

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