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.

Register.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import React, { useState } from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. RegisterPageContainer,
  5. Footer,
  6. FooterText,
  7. LoginAltText,
  8. LoginTextContainer,
  9. ProgressContainer,
  10. RegisterDescription,
  11. RegisterTitle,
  12. ProfileImagePicker,
  13. ProfilePicture,
  14. RegisterPageContent,
  15. ErrorMessage,
  16. } from "./Register.styled";
  17. import { ReactComponent as Logo } from "../../../assets/images/svg/logo-vertical.svg";
  18. import { NavLink, useHistory } from "react-router-dom";
  19. import { Trans, useTranslation } from "react-i18next";
  20. import Link from "../../../components/Link/Link";
  21. import StepProgress from "../../../components/StepProgress/StepProgress";
  22. import { REGISTER_SUCCESSFUL_PAGE } from "../../../constants/pages";
  23. import FirstPartOfRegistration from "./FirstPart/FirstPartOfRegistration";
  24. import SecondPartOfRegistration from "./SecondPart/SecondPartOfRegistration";
  25. import ThirdPartOfRegistration from "./ThirdPart/ThirdPartOfRegistration";
  26. import { useDispatch } from "react-redux";
  27. import { fetchRegisterUser } from "../../../store/actions/register/registerActions";
  28. const Register = () => {
  29. const { t } = useTranslation();
  30. const history = useHistory();
  31. const dispatch = useDispatch();
  32. const [currentStep, setCurrentStep] = useState(1);
  33. const [informations, setInformations] = useState({}); // Values of fields typed in all steps
  34. const [mailError, setMailError] = useState(""); // Wrong mail typed
  35. const [mailErrorMessage, setMailErrorMessage] = useState(""); // Error message caused by typing wrong mail
  36. const [PIBError, setPIBError] = useState(""); // Wrong PIB typed
  37. const [PIBErrorMessage, setPIBErrorMessage] = useState(""); // Error message caused by typing wrong PIB
  38. const [imageError, setImageError] = useState(false); // Not picked image
  39. const handleResponseSuccess = () => {
  40. history.push(REGISTER_SUCCESSFUL_PAGE);
  41. };
  42. const handleResponseError = (error) => {
  43. console.log(error);
  44. const { mail, password, PIB, image } = informations;
  45. if (error.type === "mail") {
  46. setInformations({ image });
  47. setCurrentStep(1);
  48. setMailError(mail);
  49. if (
  50. error.error.response.data.toString() ===
  51. "User with email already exists"
  52. ) {
  53. setMailErrorMessage(t("register.emailTaken"));
  54. } else {
  55. setMailErrorMessage(t("register.emailFormat"));
  56. }
  57. } else {
  58. setInformations({ mail, password, image });
  59. setCurrentStep(2);
  60. setPIBError(PIB.toString());
  61. setPIBErrorMessage(t("register.PIBTaken"));
  62. }
  63. };
  64. const registerUser = (values) => {
  65. dispatch(
  66. fetchRegisterUser({ values, handleResponseSuccess, handleResponseError })
  67. );
  68. };
  69. const handleSubmit = (values) => {
  70. if (currentStep !== 3) {
  71. setCurrentStep((prevState) => prevState + 1);
  72. } else {
  73. if (!informations.image) {
  74. setImageError(true);
  75. } else {
  76. registerUser({ ...informations, ...values });
  77. }
  78. }
  79. setInformations({ ...informations, ...values });
  80. };
  81. const setImage = (image) => {
  82. setImageError(false);
  83. setInformations((prevInfo) => ({
  84. ...prevInfo,
  85. image,
  86. }));
  87. };
  88. const goStepBack = (stepNumber) => {
  89. setCurrentStep(stepNumber);
  90. const { mail, password, image } = informations;
  91. if (stepNumber === 1) {
  92. setInformations({ image });
  93. }
  94. if (stepNumber === 2) {
  95. setInformations({ mail, password, image });
  96. }
  97. };
  98. return (
  99. <RegisterPageContainer currentstep={currentStep}>
  100. <RegisterPageContent>
  101. <Logo />
  102. <RegisterTitle component="h1" variant="h5">
  103. {t("register.title")}
  104. </RegisterTitle>
  105. <RegisterDescription component="h1" variant="h6">
  106. {t("register.descriptionMain")}
  107. </RegisterDescription>
  108. <ProgressContainer>
  109. <StepProgress
  110. functions={[() => goStepBack(1), () => goStepBack(2)]}
  111. current={currentStep}
  112. numberOfSteps={3}
  113. />
  114. </ProgressContainer>
  115. <ProfileImagePicker setImage={setImage}>
  116. <ProfilePicture />
  117. </ProfileImagePicker>
  118. {currentStep === 1 && (
  119. <FirstPartOfRegistration
  120. handleSubmit={handleSubmit}
  121. error={mailError}
  122. errorMessage={mailErrorMessage}
  123. />
  124. )}
  125. {currentStep === 2 && (
  126. <SecondPartOfRegistration
  127. handleSubmit={handleSubmit}
  128. error={PIBError}
  129. errorMessage={PIBErrorMessage}
  130. />
  131. )}
  132. {currentStep === 3 && (
  133. <ThirdPartOfRegistration handleSubmit={handleSubmit} />
  134. )}
  135. {imageError && <ErrorMessage>{t("register.imageError")}</ErrorMessage>}
  136. <LoginTextContainer>
  137. <LoginAltText>{t("register.loginText")}</LoginAltText>
  138. <Link
  139. to="/login"
  140. component={NavLink}
  141. underline="hover"
  142. align="center"
  143. >
  144. {t("register.login")}
  145. </Link>
  146. </LoginTextContainer>
  147. </RegisterPageContent>
  148. <Footer>
  149. <FooterText>
  150. <Trans i18nKey="register.acceptTerms" />{" "}
  151. <NavLink
  152. to="#"
  153. style={{ color: "black", fontWeight: "500", cursor: "pointer" }}
  154. >
  155. <Trans i18nKey="register.terms" />
  156. </NavLink>
  157. </FooterText>
  158. </Footer>
  159. </RegisterPageContainer>
  160. );
  161. };
  162. Register.propTypes = {
  163. children: PropTypes.node,
  164. };
  165. export default Register;