Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

271 linhas
8.5 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. "& .Mui-active": {
  46. "&.MuiStepIcon-root": {
  47. color: "warning.main",
  48. fontSize: "2rem",
  49. },
  50. "& .MuiStepConnector-line": {
  51. borderColor: "warning.main"
  52. }
  53. },
  54. "& .Mui-completed": {
  55. "&.MuiStepIcon-root": {
  56. color: "secondary.main",
  57. fontSize: "2rem",
  58. },
  59. "& .MuiStepConnector-line": {
  60. borderColor: "secondary.main"
  61. }
  62. }
  63. }
  64. const totalSteps = () => {
  65. return steps.length;
  66. };
  67. const completedSteps = () => {
  68. return Object.keys(completed).length;
  69. };
  70. const isLastStep = () => {
  71. return activeStep === totalSteps() - 1;
  72. };
  73. const allStepsCompleted = () => {
  74. return completedSteps() === totalSteps();
  75. };
  76. const handleCheckUsername = async () => {
  77. const response = await axios.post(`${POST_USERNAME}`, {
  78. u1: username,
  79. })
  80. return Number(response.data[0]) === 1
  81. }
  82. const handleCaptcha = async () => {
  83. const response = await axios.post(`${POST_VERIFY_CAPTCHA}`, {
  84. captcha: base64Url,
  85. checkCode: checkCode,
  86. })
  87. return Boolean(response.data["success"]);
  88. }
  89. const handleNext = async () => {
  90. const captchaTest = await handleCaptcha();
  91. if (!captchaTest) {
  92. notifyActionError(intl.formatMessage({id: 'validVerify'}))
  93. return;
  94. }
  95. const test = await handleCheckUsername()
  96. if (test) {
  97. notifyActionError(intl.formatMessage({id: 'usernameTaken'}))
  98. } else {
  99. const newActiveStep =
  100. isLastStep() && !allStepsCompleted()
  101. ? // It's the last step, but not all steps have been completed,
  102. // find the first step that has been completed
  103. steps.findIndex((step, i) => !(i in completed))
  104. : activeStep + 1;
  105. setActiveStep(newActiveStep);
  106. scrollToTop();
  107. }
  108. };
  109. const handleBack = () => {
  110. scrollToTop();
  111. setActiveStep((prevActiveStep) => prevActiveStep - 1);
  112. };
  113. const scrollToTop = () => {
  114. window.scrollTo(0, 0);
  115. };
  116. const handleReset = () => {
  117. setActiveStep(0);
  118. setCompleted({});
  119. };
  120. return (
  121. // <AuthWrapper>
  122. <Stack sx={{ width: '100%', fontSize: '2rem', paddingTop: '35px', bgcolor: 'backgroundColor.default' }} alignItems="center">
  123. <Stepper activeStep={activeStep} sx={stepStyle}>
  124. {steps.map((label, index) => (
  125. <Step key={label} completed={completed[index]} readOnly={true}>
  126. {
  127. index < 2 ?
  128. (<StepButton
  129. // onClick={handleStep(index)}
  130. >
  131. <StepLabel sx={{
  132. flexDirection: 'column',
  133. "& .MuiStepLabel-iconContainer": {
  134. paddingRight: 0
  135. }}}>
  136. <Typography variant="step1">{label}</Typography>
  137. </StepLabel>
  138. </StepButton>) :
  139. (<StepButton
  140. sx={activeStep === 2 ? { "& .MuiSvgIcon-root": { color: "warning.main", fontSize: "2rem" } } : allStepsCompleted() ? { "& .MuiSvgIcon-root": { color: "secondary.main", fontSize: "2rem" } } : { color: "rgba(0, 0, 0, 0.38)" }}
  141. icon={<VisibilityIcon />}
  142. // onClick={handleStep(index)}
  143. >
  144. <StepLabel sx={{
  145. flexDirection: 'column',
  146. "& .MuiStepLabel-iconContainer": {
  147. paddingRight: 0
  148. }}}>
  149. <Typography variant="step1">{label}</Typography>
  150. </StepLabel>
  151. </StepButton>)
  152. }
  153. </Step>
  154. ))}
  155. </Stepper>
  156. {allStepsCompleted() ? (
  157. <React.Fragment>
  158. <Typography variant="h4" sx={{ mt: 2, mb: 1 }}>
  159. All steps completed - you&apos;re finished
  160. </Typography>
  161. <Stack direction="row" sx={{ pt: 2 }}>
  162. <Stack sx={{ flex: '1 1 auto' }} />
  163. <Button onClick={handleReset}><Typography variant="h5">Reset</Typography></Button>
  164. </Stack>
  165. </React.Fragment>
  166. ) : (
  167. <React.Fragment>
  168. <AuthWrapper>
  169. <CustomFormWizard
  170. setUpdateValid={setUpdateValid}
  171. step={activeStep}
  172. setUsername={setUsername}
  173. setBase64Url={setBase64Url}
  174. setCheckCode={setCheckCode}
  175. />
  176. {/* <CustomFormWizard step={activeStep} /> */}
  177. </AuthWrapper>
  178. <Stack direction="row" sx={{ pb: 2, mt:-4, mb:2 }}>
  179. {activeStep === 2 || activeStep === 0 ? (
  180. <Button
  181. color="inherit"
  182. disabled={true}
  183. onClick={handleBack}
  184. sx={{ mr: 1 }}
  185. variant="h5"
  186. >
  187. <Typography variant="h5">
  188. <FormattedMessage id="back"/>
  189. </Typography>
  190. </Button>
  191. ) : (
  192. <Button
  193. color="inherit"
  194. disabled={activeStep === 0}
  195. onClick={handleBack}
  196. sx={{ mr: 1 }}
  197. variant="h5"
  198. >
  199. <Typography variant="h5">
  200. <FormattedMessage id="back"/>
  201. </Typography>
  202. </Button>
  203. )
  204. }
  205. <Stack sx={{ flex: '1 1 auto' }} />
  206. {activeStep === totalSteps() - 2 ?
  207. (
  208. <Button variant="outlined" onClick={handleNext} sx={{ mr: 1 }}>
  209. <Typography variant="h5">
  210. <FormattedMessage id="submit"/>
  211. </Typography>
  212. </Button>
  213. ) : (activeStep === totalSteps() - 1 ?
  214. (
  215. <Button variant="outlined" color="inherit"
  216. disabled={true} sx={{ mr: 1 }}>
  217. <Typography variant="h5">
  218. <FormattedMessage id="submit"/>
  219. </Typography>
  220. </Button>
  221. ) :
  222. (
  223. // <Button disabled={updateValid} variant="outlined" onClick={handleNext} sx={{ mr: 1 }}>
  224. <Button disabled={!updateValid} variant="outlined" onClick={handleNext} sx={{ mr: 1 }}>
  225. <Typography variant="h5">
  226. <FormattedMessage id="continue"/>
  227. </Typography>
  228. </Button>
  229. )
  230. )}
  231. {/* {activeStep !== steps.length &&
  232. (completed[activeStep] ? (
  233. <Typography variant="caption" sx={{ display: 'inline-block' }}>
  234. Step {activeStep + 1} already completed
  235. </Typography>
  236. ) : (
  237. <Button onClick={handleComplete}>
  238. {completedSteps() === totalSteps() - 1
  239. ? 'Finish'
  240. : 'Complete Step'}
  241. </Button>
  242. ))} */}
  243. </Stack>
  244. </React.Fragment>
  245. )}
  246. </Stack >
  247. // </AuthWrapper>
  248. );
  249. };
  250. export default Register;