選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

IAmSmartRegister.js 8.8 KiB

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