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.
 
 

241 lines
7.3 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. const CustomFormWizard = Loadable(lazy(() => import('./auth-forms/IAmSmartFormWizard')));
  24. const AuthWrapper = Loadable(lazy(() => import('./AuthWrapperCustom')));
  25. // ================================|| REGISTER ||================================ //
  26. const stepStyle = {
  27. width: "40%",
  28. boxShadow: 1,
  29. backgroundColor: "#FFFFFF",
  30. padding: 2,
  31. "& .Mui-active": {
  32. "&.MuiStepIcon-root": {
  33. color: "warning.main",
  34. fontSize: "2rem",
  35. },
  36. "& .MuiStepConnector-line": {
  37. borderColor: "warning.main"
  38. }
  39. },
  40. "& .Mui-completed": {
  41. "&.MuiStepIcon-root": {
  42. color: "secondary.main",
  43. fontSize: "2rem",
  44. },
  45. "& .MuiStepConnector-line": {
  46. borderColor: "secondary.main"
  47. }
  48. }
  49. }
  50. const steps = ['個人資料', '預覽', '完成提交'];
  51. const Register = () => {
  52. const [activeStep, setActiveStep] = useState(0);
  53. const [completed, setCompleted] = useState([false]);
  54. const [updateValid, setUpdateValid] = useState(false);
  55. const [idNo, setIdNo] = useState("");
  56. const totalSteps = () => {
  57. return steps.length;
  58. };
  59. const completedSteps = () => {
  60. return Object.keys(completed).length;
  61. };
  62. const isLastStep = () => {
  63. return activeStep === totalSteps() - 1;
  64. };
  65. const allStepsCompleted = () => {
  66. return completedSteps() === totalSteps();
  67. };
  68. const handleCheckID = async () => {
  69. const response = await axios.get(`${GET_ID}`, {
  70. params: {
  71. idNo: idNo,
  72. }
  73. })
  74. return Number(response.data[0]) === 1
  75. }
  76. const handleCaptcha = async () => {
  77. const response = await axios.post(`${POST_VERIFY_CAPTCHA}`, {
  78. captcha: base64Url,
  79. checkCode: checkCode,
  80. })
  81. return Boolean(response.data["success"]);
  82. }
  83. const handleNext = async () => {
  84. if (!idNo) {
  85. notifyActionError("資料,請返回注冊頁面。")
  86. return;
  87. }
  88. const captchaTest = await handleCaptcha();
  89. if (!captchaTest) {
  90. notifyActionError("請輸入有效驗證")
  91. return;
  92. }
  93. const test = await handleCheckID()
  94. if (test) {
  95. notifyActionError("此用戶已注冊,請返回登入頁面並進行登入流程。")
  96. } else {
  97. const newActiveStep =
  98. isLastStep() && !allStepsCompleted()
  99. ? // It's the last step, but not all steps have been completed,
  100. // find the first step that has been completed
  101. steps.findIndex((step, i) => !(i in completed))
  102. : activeStep + 1;
  103. setActiveStep(newActiveStep);
  104. scrollToTop();
  105. }
  106. };
  107. const handleBack = () => {
  108. scrollToTop();
  109. setActiveStep((prevActiveStep) => prevActiveStep - 1);
  110. };
  111. const scrollToTop = () => {
  112. window.scrollTo(0, 0);
  113. };
  114. const handleReset = () => {
  115. setActiveStep(0);
  116. setCompleted({});
  117. };
  118. return (
  119. // <AuthWrapper>
  120. <Stack sx={{ width: '100%', fontSize: '2rem', paddingTop: '65px', bgcolor: 'backgroundColor.default' }} alignItems="center">
  121. <Stepper activeStep={activeStep} sx={stepStyle}>
  122. {steps.map((label, index) => (
  123. <Step key={label} completed={completed[index]} readOnly={true}>
  124. {
  125. index < 2 ?
  126. (<StepButton
  127. // onClick={handleStep(index)}
  128. >
  129. <Typography variant="step1">{label}</Typography>
  130. </StepButton>) :
  131. (<StepButton
  132. 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)" }}
  133. icon={<VisibilityIcon />}
  134. // onClick={handleStep(index)}
  135. >
  136. <Typography variant="step1">{label}</Typography>
  137. </StepButton>)
  138. }
  139. </Step>
  140. ))}
  141. </Stepper>
  142. {allStepsCompleted() ? (
  143. <React.Fragment>
  144. <Typography variant="h4" sx={{ mt: 2, mb: 1 }}>
  145. All steps completed - you&apos;re finished
  146. </Typography>
  147. <Stack direction="row" sx={{ pt: 2 }}>
  148. <Stack sx={{ flex: '1 1 auto' }} />
  149. <Button onClick={handleReset}><Typography variant="h5">Reset</Typography></Button>
  150. </Stack>
  151. </React.Fragment>
  152. ) : (
  153. <React.Fragment>
  154. <AuthWrapper>
  155. <CustomFormWizard
  156. setUpdateValid={setUpdateValid}
  157. step={activeStep}
  158. setIdNo={setIdNo}
  159. setBase64Url={setBase64Url}
  160. setCheckCode={setCheckCode}
  161. />
  162. {/* <CustomFormWizard step={activeStep} /> */}
  163. </AuthWrapper>
  164. <Stack direction="row" sx={{ pb: 2 }}>
  165. {activeStep === 2 || activeStep === 0 ? (
  166. <Button
  167. color="inherit"
  168. disabled={true}
  169. onClick={handleBack}
  170. sx={{ mr: 1 }}
  171. >
  172. <Typography variant="h5">返回</Typography>
  173. </Button>
  174. ) : (
  175. <Button
  176. color="inherit"
  177. disabled={activeStep === 0}
  178. onClick={handleBack}
  179. sx={{ mr: 1 }}
  180. >
  181. <Typography variant="h5">返回</Typography>
  182. </Button>
  183. )
  184. }
  185. <Stack sx={{ flex: '1 1 auto' }} />
  186. {activeStep === totalSteps() - 2 ?
  187. (
  188. <Button variant="outlined" onClick={handleNext} sx={{ mr: 1 }}>
  189. <Typography variant="h5">提交</Typography>
  190. </Button>
  191. ) : (activeStep === totalSteps() - 1 ?
  192. (
  193. <Button variant="outlined" color="inherit"
  194. disabled={true} sx={{ mr: 1 }}>
  195. <Typography variant="h5">提交</Typography>
  196. </Button>
  197. ) :
  198. (
  199. // <Button disabled={updateValid} variant="outlined" onClick={handleNext} sx={{ mr: 1 }}>
  200. <Button disabled={!updateValid} variant="outlined" onClick={handleNext} sx={{ mr: 1 }}>
  201. <Typography variant="h5">繼續</Typography>
  202. </Button>
  203. )
  204. )}
  205. {/* {activeStep !== steps.length &&
  206. (completed[activeStep] ? (
  207. <Typography variant="caption" sx={{ display: 'inline-block' }}>
  208. Step {activeStep + 1} already completed
  209. </Typography>
  210. ) : (
  211. <Button onClick={handleComplete}>
  212. {completedSteps() === totalSteps() - 1
  213. ? 'Finish'
  214. : 'Complete Step'}
  215. </Button>
  216. ))} */}
  217. </Stack>
  218. </React.Fragment>
  219. )}
  220. </Stack >
  221. // </AuthWrapper>
  222. );
  223. };
  224. export default Register;