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.

CreateOffer.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import React, { useState } from "react";
  2. import PropTypes from "prop-types";
  3. import { useDispatch, useSelector } from "react-redux";
  4. import { useHistory } from "react-router-dom";
  5. import { HOME_PAGE } from "../../../constants/pages";
  6. import {
  7. CreateOfferContainer,
  8. CreateOfferTitle,
  9. ModalCreateOfferContainer,
  10. ModalHeader,
  11. BackIcon,
  12. // CloseIcon,
  13. } from "./CreateOffer.styled";
  14. import StepProgress from "../../StepProgress/StepProgress";
  15. import FirstPartCreateOffer from "./FirstPart/FirstPartCreateOffer";
  16. import SecondPartCreateOffer from "./SecondPart/SecondPartCreateOffer";
  17. import ThirdPartCreateOffer from "./ThirdPart/ThirdPartCreateOffer";
  18. import {
  19. addOffer,
  20. fetchProfileOffers,
  21. } from "../../../store/actions/offers/offersActions";
  22. import { editOneOffer } from "../../../store/actions/offers/offersActions";
  23. import { ReactComponent as ArrowBack } from "../../../assets/images/svg/arrow-back.svg";
  24. // import { ReactComponent as CloseButton } from "../../../assets/images/svg/close-modal.svg";
  25. import { useTranslation } from "react-i18next";
  26. import BackdropComponent from "../../MUI/BackdropComponent";
  27. import CloseButton from "./CloseButton/CloseButton";
  28. const CreateOffer = ({ history, closeCreateOfferModal, editOffer, offer }) => {
  29. const dispatch = useDispatch();
  30. const [informations, setInformations] = useState({});
  31. const [currentStep, setCurrentStep] = useState(1);
  32. const categories = useSelector((state) => state.categories.categories);
  33. const historyRouter = useHistory();
  34. const { t } = useTranslation();
  35. const handleApiResponseSuccess = () => {
  36. if (editOffer === undefined) {
  37. const userId = historyRouter.location.pathname.slice(
  38. 9,
  39. historyRouter.location.pathname.length
  40. );
  41. dispatch(fetchProfileOffers(userId));
  42. historyRouter.push({
  43. pathname: HOME_PAGE,
  44. state: {
  45. from: history.location.pathname,
  46. },
  47. });
  48. } else {
  49. const userId = offer.userId;
  50. dispatch(fetchProfileOffers(userId));
  51. }
  52. };
  53. const handleNext = (values) => {
  54. setInformations({ ...informations, ...values });
  55. setCurrentStep((prevState) => prevState + 1);
  56. };
  57. const newImgs =
  58. informations.images &&
  59. informations.images
  60. .filter((img) => img !== undefined)
  61. .map((img) =>
  62. img
  63. .replace("data:image/jpg;base64,", "")
  64. .replace("data:image/jpeg;base64,", "")
  65. .replace("data:image/png;base64,", "")
  66. );
  67. let subcategories = [];
  68. for (const element of categories) {
  69. if (element.name === informations.category) {
  70. subcategories = element.subcategories.map((item) => item.name);
  71. }
  72. }
  73. const offerData = {
  74. name: informations.nameOfProduct,
  75. description: informations.description,
  76. location: {
  77. city: informations.location,
  78. },
  79. condition: informations.condition,
  80. category: {
  81. name: informations.category,
  82. subcategories: subcategories,
  83. },
  84. subcategory: informations.subcategory,
  85. images: newImgs,
  86. };
  87. const submitOffer = (values) => {
  88. dispatch(addOffer({ values, handleApiResponseSuccess }));
  89. };
  90. const submitEditOffer = (id, values) => {
  91. dispatch(editOneOffer(id, values));
  92. };
  93. const handleSubmitOffer = () => {
  94. if (editOffer === undefined) {
  95. submitOffer({ offerData, handleApiResponseSuccess });
  96. } else {
  97. const offerId = offer._id;
  98. submitEditOffer({ offerId, offerData, handleApiResponseSuccess });
  99. }
  100. closeCreateOfferModal(false);
  101. };
  102. const backButtonHandler = () => {
  103. setCurrentStep((prevState) => prevState - 1);
  104. };
  105. // const closeModalHandler = () => {
  106. // closeCreateOfferModal(false);
  107. // };
  108. const goStepBack = (stepNumber) => {
  109. setCurrentStep(stepNumber);
  110. const {
  111. category,
  112. condition,
  113. description,
  114. // images,
  115. location,
  116. nameOfProduct,
  117. subcategory,
  118. } = informations;
  119. if (stepNumber === 1) {
  120. setInformations({});
  121. }
  122. if (stepNumber === 2) {
  123. setInformations({
  124. category,
  125. condition,
  126. description,
  127. location,
  128. nameOfProduct,
  129. subcategory,
  130. });
  131. }
  132. };
  133. return (
  134. <>
  135. <BackdropComponent
  136. isLoading
  137. handleClose={closeCreateOfferModal}
  138. position="fixed"
  139. />
  140. <ModalCreateOfferContainer currentStep={currentStep}>
  141. <CreateOfferContainer currentStep={currentStep}>
  142. <ModalHeader>
  143. <BackIcon onClick={backButtonHandler}>
  144. {currentStep !== 1 ? <ArrowBack /> : ""}
  145. </BackIcon>
  146. <CreateOfferTitle component="h1" variant="h5">
  147. {currentStep === 3
  148. ? `${t("offer.review")}`
  149. : `${
  150. editOffer !== undefined
  151. ? `${t("offer.changeOffer")}`
  152. : `${t("offer.newOffer")}`
  153. }`}
  154. </CreateOfferTitle>
  155. <CloseButton closeCreateOfferModal={closeCreateOfferModal} />
  156. {/* <CloseIcon onClick={closeModalHandler}>
  157. <CloseButton />
  158. </CloseIcon> */}
  159. </ModalHeader>
  160. <StepProgress
  161. current={currentStep}
  162. numberOfSteps={3}
  163. functions={[() => goStepBack(1), () => goStepBack(2)]}
  164. />
  165. {currentStep === 1 && (
  166. <FirstPartCreateOffer handleNext={handleNext} offer={offer} />
  167. )}
  168. {currentStep === 2 && (
  169. <SecondPartCreateOffer handleNext={handleNext} offer={offer} />
  170. )}
  171. {currentStep === 3 && (
  172. <ThirdPartCreateOffer
  173. handleSubmitOffer={handleSubmitOffer}
  174. informations={informations}
  175. />
  176. )}
  177. </CreateOfferContainer>
  178. </ModalCreateOfferContainer>
  179. </>
  180. );
  181. };
  182. CreateOffer.propTypes = {
  183. history: PropTypes.shape({
  184. replace: PropTypes.func,
  185. push: PropTypes.func,
  186. location: PropTypes.shape({
  187. pathname: PropTypes.string,
  188. }),
  189. }),
  190. closeCreateOfferModal: PropTypes.func,
  191. editOffer: PropTypes.bool,
  192. offer: PropTypes.object,
  193. };
  194. export default CreateOffer;