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.

265 lines
8.6 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,
  15. } from '@mui/material';
  16. import VisibilityIcon from '@mui/icons-material/Visibility';
  17. import { GET_ID, 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/IAmSmartFormWizard')));
  25. const AuthWrapper = Loadable(lazy(() => import('./AuthWrapperCustom')));
  26. // ================================|| REGISTER ||================================ //
  27. const stepStyle = {
  28. width: { lg: "40%", md: "70%", xs: "100%" },
  29. boxShadow: 1,
  30. backgroundColor: "#FFFFFF",
  31. padding: 2,
  32. /* Inactive (default) */
  33. "& .MuiStepIcon-root": { color: "#757575", fontSize: "2rem" }, // darker grey
  34. "& .MuiStepLabel-label": { color: "#424242" }, // label darker
  35. "& .MuiStepConnector-line": { borderColor: "#9E9E9E" }, // connector darker
  36. /* Active */
  37. "& .Mui-active": {
  38. "&.MuiStepIcon-root": { color: "warning.main", fontSize: "2rem" },
  39. "& .MuiStepConnector-line": { borderColor: "warning.main" }
  40. },
  41. /* Completed */
  42. "& .Mui-completed": {
  43. "&.MuiStepIcon-root": { color: "#0C489E", fontSize: "2rem" }, // use your strong blue
  44. "& .MuiStepConnector-line": { borderColor: "#0C489E" }
  45. }
  46. };
  47. const Register = () => {
  48. const [activeStep, setActiveStep] = useState(0);
  49. const [completed, setCompleted] = useState([false]);
  50. const [updateValid, setUpdateValid] = useState(false);
  51. const [base64Url, setBase64Url] = useState("")
  52. const [checkCode, setCheckCode] = useState("")
  53. const [idNo, setIdNo] = useState("");
  54. const intl = useIntl();
  55. const steps = [
  56. intl.formatMessage({id: 'personalInformation'}),
  57. intl.formatMessage({id: 'preview'}),
  58. intl.formatMessage({id: 'finishSubmission'})
  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 handleCheckID = async () => {
  73. const response = await axios.get(`${GET_ID}`, {
  74. params: {
  75. idNo: idNo,
  76. }
  77. })
  78. return Number(response?.data?.idVaild) === true
  79. }
  80. const handleCaptcha = async () => {
  81. const response = await axios.post(`${POST_VERIFY_CAPTCHA}`, {
  82. captcha: base64Url,
  83. checkCode: checkCode,
  84. })
  85. return Boolean(response.data["success"]);
  86. }
  87. const handleNext = async () => {
  88. if (!idNo) {
  89. notifyActionError(intl.formatMessage({id: 'iAmSmartNoIdNoMsg'}))
  90. return;
  91. }
  92. const captchaTest = await handleCaptcha();
  93. if (!captchaTest) {
  94. notifyActionError(intl.formatMessage({id: 'validVerify'}))
  95. return;
  96. }
  97. const test = await handleCheckID()
  98. if (test) {
  99. notifyActionError(intl.formatMessage({id: 'userRegistered'}))
  100. } else {
  101. const newActiveStep =
  102. isLastStep() && !allStepsCompleted()
  103. ? // It's the last step, but not all steps have been completed,
  104. // find the first step that has been completed
  105. steps.findIndex((step, i) => !(i in completed))
  106. : activeStep + 1;
  107. setActiveStep(newActiveStep);
  108. scrollToTop();
  109. }
  110. };
  111. const handleBack = () => {
  112. scrollToTop();
  113. setActiveStep((prevActiveStep) => prevActiveStep - 1);
  114. };
  115. const scrollToTop = () => {
  116. window.scrollTo(0, 0);
  117. };
  118. const handleReset = () => {
  119. setActiveStep(0);
  120. setCompleted({});
  121. };
  122. return (
  123. // <AuthWrapper>
  124. <Stack sx={{ width: '100%', fontSize: '2rem', paddingTop: '65px', bgcolor: 'backgroundColor.default' }} alignItems="center">
  125. <Stepper activeStep={activeStep} sx={stepStyle}>
  126. {steps.map((label, index) => (
  127. <Step key={label} completed={completed[index]} readOnly={true}>
  128. {
  129. index < 2 ?
  130. (<StepButton
  131. // onClick={handleStep(index)}
  132. >
  133. <Typography variant="step1">{label}</Typography>
  134. </StepButton>) :
  135. (<StepButton
  136. sx={
  137. activeStep === 2
  138. ? { "& .MuiSvgIcon-root": { color: "warning.main", fontSize: "2rem" } }
  139. : allStepsCompleted()
  140. ? { "& .MuiSvgIcon-root": { color: "#0C489E", fontSize: "2rem" } }
  141. : { color: "rgba(0,0,0,0.6)" } // was 0.38
  142. }
  143. icon={<VisibilityIcon />}
  144. // onClick={handleStep(index)}
  145. >
  146. <Typography variant="step1">{label}</Typography>
  147. </StepButton>)
  148. }
  149. </Step>
  150. ))}
  151. </Stepper>
  152. {allStepsCompleted() ? (
  153. <React.Fragment>
  154. <Typography variant="h4" sx={{ mt: 2, mb: 1 }}>
  155. All steps completed - you&apos;re finished
  156. </Typography>
  157. <Stack direction="row" sx={{ pt: 2 }}>
  158. <Stack sx={{ flex: '1 1 auto' }} />
  159. <Button onClick={handleReset}><Typography variant="h5"> <FormattedMessage id="reset"/></Typography></Button>
  160. </Stack>
  161. </React.Fragment>
  162. ) : (
  163. <React.Fragment id={"fragment"} sx={{width: {lg:"1000px", md:"600px", xs:"95%"}}}>
  164. <AuthWrapper id={"authWrapper"}>
  165. <CustomFormWizard
  166. id={"CustomFormWizard"}
  167. setUpdateValid={setUpdateValid}
  168. step={activeStep}
  169. setIdNo={setIdNo}
  170. setBase64Url={setBase64Url}
  171. setCheckCode={setCheckCode}
  172. />
  173. {/* <CustomFormWizard step={activeStep} /> */}
  174. </AuthWrapper>
  175. <Stack direction="row" sx={{ pb: 2 }}>
  176. {activeStep === 2 || activeStep === 0 ? (
  177. <Button
  178. color="inherit"
  179. disabled={true}
  180. onClick={handleBack}
  181. sx={{ mr: 1 }}
  182. >
  183. <Typography variant="h5">
  184. <FormattedMessage id="back"/>
  185. </Typography>
  186. </Button>
  187. ) : (
  188. <Button
  189. color="inherit"
  190. disabled={activeStep === 0}
  191. onClick={handleBack}
  192. sx={{ mr: 1 }}
  193. >
  194. <Typography variant="h5">
  195. <FormattedMessage id="back"/>
  196. </Typography>
  197. </Button>
  198. )
  199. }
  200. <Stack sx={{ flex: '1 1 auto' }} />
  201. {activeStep === totalSteps() - 2 ?
  202. (
  203. <Button variant="outlined" onClick={handleNext} sx={{ mr: 1 }}>
  204. <Typography variant="h5">
  205. <FormattedMessage id="submit"/>
  206. </Typography>
  207. </Button>
  208. ) : (activeStep === totalSteps() - 1 ?
  209. (
  210. <Button variant="outlined" color="inherit"
  211. disabled={true} sx={{ mr: 1 }}>
  212. <Typography variant="h5">
  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} variant="outlined" onClick={handleNext} sx={{ mr: 1 }}>
  220. <Typography variant="h5">
  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 Register;