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.

FirstPartCreateOffer.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import React, { useState } from "react";
  2. import PropTypes from "prop-types";
  3. import { useFormik } from "formik";
  4. import {
  5. CreateOfferFormContainer,
  6. DescriptionField,
  7. FieldLabel,
  8. NextButton,
  9. TitleField,
  10. } from "./FirstPartCreateOffer.styled";
  11. // import { TextField } from "../../../TextFields/TextField/TextField";
  12. // import { PrimaryButton } from "../../../Buttons/PrimaryButton/PrimaryButton";
  13. import * as Yup from "yup";
  14. import selectedTheme from "../../../../themes";
  15. import { useTranslation } from "react-i18next";
  16. import Option from "../../../Select/Option/Option";
  17. import { SelectField } from "../CreateOffer.styled";
  18. import { useSelector } from "react-redux";
  19. const FirstPartCreateOffer = (props) => {
  20. const [subcat, setSubcat] = useState([{}]);
  21. const locations = useSelector((state) => state.locations.locations);
  22. const categories = useSelector((state) => state.categories.categories);
  23. console.log(locations);
  24. const { t } = useTranslation();
  25. const handleSubmit = (values) => {
  26. console.log(values);
  27. props.handleNext(values);
  28. };
  29. const formik = useFormik({
  30. initialValues: {
  31. nameOfProduct: "",
  32. description: "",
  33. location: "",
  34. category: "",
  35. subcategory: "",
  36. },
  37. validationSchema: Yup.object().shape({
  38. nameOfProduct: Yup.string().required(t("login.nameOfProductRequired")),
  39. description: Yup.string().required(t("login.descriptionRequired")).min(8),
  40. location: Yup.string().oneOf(locations.map((l) => l.city)),
  41. category: Yup.string().oneOf(categories.map((c) => c.name)),
  42. // subcategory: Yup.string().oneOf(
  43. // subcat[0]?.subcategories ? subcat[0].subcategories : []
  44. // ),
  45. }),
  46. onSubmit: handleSubmit,
  47. validateOnBlur: true,
  48. enableReinitialize: true,
  49. });
  50. const handleSubcategories = (category) => {
  51. const filtered = categories.filter((cat) => cat.name === category);
  52. setSubcat(filtered);
  53. };
  54. return (
  55. <CreateOfferFormContainer component="form" onSubmit={formik.handleSubmit}>
  56. {/* <Backdrop position="absolute" isLoading={isLoading} /> */}
  57. <FieldLabel leftText={t("offer.title")} />
  58. <TitleField
  59. name="nameOfProduct"
  60. placeholder={t("offer.productName")}
  61. italicPlaceholder
  62. margin="normal"
  63. value={formik.values.nameOfProduct}
  64. onChange={formik.handleChange}
  65. error={formik.touched.nameOfProduct && formik.errors.nameOfProduct}
  66. helperText={formik.touched.nameOfProduct && formik.errors.nameOfProduct}
  67. autoFocus
  68. fullWidth
  69. />
  70. <FieldLabel leftText={t("offer.productDescription")} />
  71. <DescriptionField
  72. name="description"
  73. placeholder={t("offer.description")}
  74. margin="normal"
  75. italicPlaceholder
  76. value={formik.values.description}
  77. onChange={formik.handleChange}
  78. error={formik.touched.description && formik.errors.description}
  79. helperText={formik.touched.description && formik.errors.description}
  80. fullWidth
  81. multiline
  82. minRows={4}
  83. height={"100px"}
  84. />
  85. <FieldLabel leftText={t("offer.location")} />
  86. <SelectField
  87. defaultValue="default"
  88. onChange={(value) => {
  89. formik.setFieldValue("location", value.target.value.city);
  90. }}
  91. >
  92. <Option value="default">{t("offer.choseLocation")}</Option>
  93. {locations.map((loc) => {
  94. return (
  95. <Option key={loc._if} value={loc}>
  96. {loc.city}
  97. </Option>
  98. );
  99. })}
  100. </SelectField>
  101. <FieldLabel leftText={t("offer.category")} />
  102. <SelectField
  103. defaultValue="default"
  104. onChange={(value) => {
  105. formik.setFieldValue("category", value.target.value.name);
  106. }}
  107. >
  108. <Option value="default">{t("offer.choseCategory")}</Option>
  109. {categories.map((cat, i) => {
  110. return (
  111. <Option
  112. key={i}
  113. value={cat}
  114. onClick={() => handleSubcategories(cat.name)}
  115. >
  116. {cat.name}
  117. </Option>
  118. );
  119. })}
  120. </SelectField>
  121. <FieldLabel leftText={t("offer.subcategory")} />
  122. <SelectField
  123. defaultValue="default"
  124. onChange={(value) => {
  125. formik.setFieldValue("subcategory", value.target.value.name);
  126. console.log(value.target);
  127. }}
  128. >
  129. <Option value="default">{t("offer.choseSubcategory")}</Option>
  130. {subcat?.length > 0 &&
  131. subcat[0]?.subcategories &&
  132. subcat[0].subcategories.map((sub, i) => {
  133. return (
  134. <Option key={i} value={sub}>
  135. {sub.name}
  136. </Option>
  137. );
  138. })}
  139. </SelectField>
  140. <NextButton
  141. type="submit"
  142. variant="contained"
  143. height="48px"
  144. fullWidth
  145. buttoncolor={selectedTheme.primaryPurple}
  146. textcolor="white"
  147. // disabled={
  148. // formik.values.username.length === 0 ||
  149. // formik.values.password.length === 0
  150. // }
  151. >
  152. {t("offer.continue")}
  153. </NextButton>
  154. </CreateOfferFormContainer>
  155. );
  156. };
  157. FirstPartCreateOffer.propTypes = {
  158. children: PropTypes.any,
  159. handleNext: PropTypes.func,
  160. };
  161. export default FirstPartCreateOffer;