選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CreateOffer.js 5.3KB

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