Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ThirdPartOfRegistration.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React, { useEffect, useState } from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. CheckboxInput,
  5. CheckboxInputContainer,
  6. CheckboxInputText,
  7. FormContainer,
  8. // RegisterDescription,
  9. } from "./ThirdPartOfRegistration.styled";
  10. import { useFormik } from "formik";
  11. import { useTranslation } from "react-i18next";
  12. import { TextField } from "../../../../components/TextFields/TextField/TextField";
  13. import AutoSuggestTextField from "../../../../components/TextFields/AutoSuggestTextField/AutoSuggestTextField";
  14. import { useDispatch, useSelector } from "react-redux";
  15. import { selectLocations } from "../../../../store/selectors/locationsSelectors";
  16. import { fetchLocations } from "../../../../store/actions/locations/locationsActions";
  17. import thirdPartInitialValues from "../../../../initialValues/registerInitialValues/thirdPartInitialValues";
  18. import ErrorMessage from "./ErrorMessage/ErrorMessage";
  19. import thirdPartValidation from "../../../../validations/registerValidations/thirdPartValidation";
  20. import { PrimaryAnimatedButton } from "../../../../components/Styles/globalStyleComponents";
  21. const ThirdPartOfRegistration = (props) => {
  22. const [checkboxValue, setCheckboxValue] = useState(false);
  23. const { t } = useTranslation();
  24. const locations = useSelector(selectLocations);
  25. const dispatch = useDispatch();
  26. console.log(checkboxValue);
  27. useEffect(() => {
  28. dispatch(fetchLocations());
  29. }, []);
  30. const handleSubmit = () => {
  31. if (
  32. formik.values.website?.length !== 0 &&
  33. !formik.values.website.match(
  34. /^((ftp|http|https):\/\/)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+((\/)[\w#]+)*(\/\w+\?[a-zA-Z0-9_]+=\w+(&[a-zA-Z0-9_]+=\w+)*)?$/gm
  35. )
  36. ) {
  37. formik.setFieldError("website");
  38. } else {
  39. props.handleSubmit(formik.values);
  40. }
  41. };
  42. const formik = useFormik({
  43. initialValues: thirdPartInitialValues(props.informations),
  44. validationSchema: thirdPartValidation(locations),
  45. onSubmit: handleSubmit,
  46. enableReinitialize: true,
  47. });
  48. return (
  49. <FormContainer component="form" onSubmit={formik.handleSubmit}>
  50. {/* <RegisterDescription component="p" variant="p">
  51. {t("register.descriptionThird")}
  52. </RegisterDescription> */}
  53. <TextField
  54. name="phoneNumber"
  55. placeholder={t("common.labelPhone")}
  56. margin="normal"
  57. type="number"
  58. value={formik.values.phoneNumber}
  59. onChange={(event) => {
  60. console.log(event);
  61. formik.setFieldValue("phoneNumber", event.target.value);
  62. }}
  63. error={formik.touched.phoneNumber && Boolean(formik.errors.phoneNumber)}
  64. helperText={formik.touched.phoneNumber && formik.errors.phoneNumber}
  65. autoFocus
  66. fullWidth
  67. onInput={(e) => {
  68. e.target.value =
  69. e.target.value[0] === "0" && e.target.value.length > 1
  70. ? "0" +
  71. String(
  72. Math.max(0, parseInt(e.target.value)).toString().slice(0, 14)
  73. )
  74. : Math.max(0, parseInt(e.target.value)).toString().slice(0, 14);
  75. }}
  76. />
  77. <AutoSuggestTextField
  78. placeholder={t("common.labelLocation")}
  79. data={locations.map((item) => ({ name: item.city }))}
  80. value={formik.values.location}
  81. error={formik.touched.location && Boolean(formik.errors.location)}
  82. onChange={(event, { newValue }) =>
  83. formik.setFieldValue("location", newValue)
  84. }
  85. />
  86. <TextField
  87. name="website"
  88. placeholder={t("common.labelWebsite")}
  89. margin="normal"
  90. type="text"
  91. value={formik.values.website}
  92. onChange={formik.handleChange}
  93. error={formik.touched.website && Boolean(formik.errors.website)}
  94. helperText={formik.touched.website && formik.errors.website}
  95. fullWidth
  96. />
  97. <CheckboxInputContainer>
  98. <CheckboxInput onClick={() => setCheckboxValue(!checkboxValue)} />
  99. <CheckboxInputText>
  100. {t("register.acceptTermsCheckbox")}
  101. </CheckboxInputText>
  102. </CheckboxInputContainer>
  103. <ErrorMessage formik={formik} />
  104. <PrimaryAnimatedButton
  105. type="submit"
  106. variant="contained"
  107. height="48px"
  108. fullWidth
  109. textcolor="white"
  110. isLoading={props.isLoading}
  111. disabled={
  112. formik.values.phoneNumber.length === 0 ||
  113. formik.values.location.length === 0 ||
  114. !checkboxValue
  115. }
  116. >
  117. {t("common.continue")}
  118. </PrimaryAnimatedButton>
  119. </FormContainer>
  120. );
  121. };
  122. ThirdPartOfRegistration.propTypes = {
  123. children: PropTypes.node,
  124. handleSubmit: PropTypes.func,
  125. informations: PropTypes.any,
  126. isLoading: PropTypes.bool,
  127. };
  128. export default ThirdPartOfRegistration;