Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import React, { useState } from "react";
  2. import PropTypes from "prop-types";
  3. import { useDispatch, useSelector } from "react-redux";
  4. import {
  5. CreateOfferContainer,
  6. CreateOfferTitle,
  7. ModalCreateOfferContainer,
  8. ModalHeader,
  9. } from "./CreateOffer.styled";
  10. import StepProgress from "../../StepProgress/StepProgress";
  11. import FirstPartCreateOffer from "./FirstPart/FirstPartCreateOffer";
  12. import SecondPartCreateOffer from "./SecondPart/SecondPartCreateOffer";
  13. import ThirdPartCreateOffer from "./ThirdPart/ThirdPartCreateOffer";
  14. import {
  15. addOffer,
  16. // fetchOffers,
  17. // fetchOneOffer,
  18. fetchProfileOffers,
  19. } from "../../../store/actions/offers/offersActions";
  20. import { selectUserId } from "../../../store/selectors/loginSelectors";
  21. import { editOneOffer } from "../../../store/actions/offers/offersActions";
  22. import { useTranslation } from "react-i18next";
  23. import BackdropComponent from "../../MUI/BackdropComponent";
  24. import CloseButton from "./CloseButton/CloseButton";
  25. import BackButton from "./BackButton/BackButton";
  26. import selectedTheme from "../../../themes";
  27. import { useMemo } from "react";
  28. // import { useLocation } from "react-router-dom";
  29. import { useHistory } from "react-router-dom";
  30. import { PROFILE_PAGE } from "../../../constants/pages";
  31. import { replaceInRoute } from "../../../util/helpers/routeHelpers";
  32. // import { routeMatches } from "../../../util/helpers/routeHelpers";
  33. const CreateOffer = ({ closeCreateOfferModal, editOffer, offer }) => {
  34. const dispatch = useDispatch();
  35. // const location = useLocation();
  36. const history = useHistory();
  37. const [informations, setInformations] = useState({});
  38. const [currentStep, setCurrentStep] = useState(1);
  39. const { t } = useTranslation();
  40. const userId = useSelector(selectUserId);
  41. const handleApiResponseSuccess = () => {
  42. // if (routeMatches(BASE_PAGE) || routeMatches(HOME_PAGE))
  43. // dispatch(fetchOffers({ queryString: "" }));
  44. // if (location.pathname.includes("profile"))
  45. // dispatch(fetchProfileOffers(userId));
  46. // if (location.pathname.includes("proizvodi"))
  47. // dispatch(fetchOneOffer(offer._id));
  48. dispatch(fetchProfileOffers(userId));
  49. history.push(
  50. replaceInRoute(PROFILE_PAGE, {
  51. idProfile: userId,
  52. })
  53. );
  54. };
  55. // Go to next step and save typed values
  56. const handleNext = (values) => {
  57. setInformations({ ...informations, ...values });
  58. setCurrentStep((prevState) => prevState + 1);
  59. };
  60. // Get new images from 2nd step
  61. const newImgs = useMemo(
  62. () =>
  63. informations.images &&
  64. informations.images.filter((img) => img !== undefined),
  65. [informations.images]
  66. );
  67. // Make offer data object with typed values
  68. const offerData = useMemo(
  69. () => ({
  70. name: informations.nameOfProduct,
  71. description: informations.description,
  72. location: {
  73. city: informations.location,
  74. },
  75. condition: informations.condition,
  76. category: {
  77. name: informations.category,
  78. },
  79. subcategory: informations.subcategory,
  80. images: newImgs,
  81. }),
  82. [informations, newImgs]
  83. );
  84. // Create (or edit) offer
  85. const handleSubmitOffer = () => {
  86. if (editOffer) {
  87. dispatch(
  88. editOneOffer({
  89. offerId: offer._id,
  90. offerData,
  91. handleApiResponseSuccess,
  92. })
  93. );
  94. } else {
  95. dispatch(addOffer({ offerData, handleApiResponseSuccess }));
  96. }
  97. closeCreateOfferModal(false);
  98. };
  99. const goStepBack = (stepNumber) => {
  100. setCurrentStep(stepNumber);
  101. // Here goes any additional logic
  102. };
  103. return (
  104. <>
  105. <BackdropComponent
  106. isLoading
  107. handleClose={closeCreateOfferModal}
  108. position="fixed"
  109. />
  110. <ModalCreateOfferContainer currentstep={currentStep}>
  111. <CreateOfferContainer currentstep={currentStep}>
  112. {/* Modal header */}
  113. <ModalHeader>
  114. <BackButton
  115. currentStep={currentStep}
  116. setCurrentStep={setCurrentStep}
  117. />
  118. <CreateOfferTitle component="h1" variant="h5">
  119. {currentStep === 3
  120. ? `${t("offer.review")}`
  121. : `${
  122. editOffer
  123. ? `${t("offer.changeOffer")}`
  124. : `${t("offer.newOffer")}`
  125. }`}
  126. </CreateOfferTitle>
  127. <CloseButton closeCreateOfferModal={closeCreateOfferModal} />
  128. </ModalHeader>
  129. {/* ^^^^^^^^ */}
  130. <StepProgress
  131. lineColor={selectedTheme.colors.stepProgressAltColor}
  132. current={currentStep}
  133. numberOfSteps={3}
  134. functions={[() => goStepBack(1), () => goStepBack(2)]}
  135. />
  136. {currentStep === 1 && (
  137. <FirstPartCreateOffer
  138. handleNext={handleNext}
  139. offer={offer}
  140. editOffer={editOffer}
  141. informations={informations}
  142. />
  143. )}
  144. {currentStep === 2 && (
  145. <SecondPartCreateOffer
  146. handleNext={handleNext}
  147. offer={offer}
  148. informations={informations}
  149. />
  150. )}
  151. {currentStep === 3 && (
  152. <ThirdPartCreateOffer
  153. handleSubmitOffer={handleSubmitOffer}
  154. informations={informations}
  155. />
  156. )}
  157. </CreateOfferContainer>
  158. </ModalCreateOfferContainer>
  159. </>
  160. );
  161. };
  162. CreateOffer.propTypes = {
  163. history: PropTypes.shape({
  164. replace: PropTypes.func,
  165. push: PropTypes.func,
  166. location: PropTypes.shape({
  167. pathname: PropTypes.string,
  168. }),
  169. }),
  170. closeCreateOfferModal: PropTypes.func,
  171. editOffer: PropTypes.bool,
  172. offer: PropTypes.object,
  173. };
  174. export default CreateOffer;