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.

OfferDetails.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. Details,
  5. OfferDescriptionTitle,
  6. OfferImage,
  7. OfferLittleDetails,
  8. OfferTitle,
  9. ScrollerHorizontal,
  10. ScrollerVertical,
  11. OfferInfoContainer,
  12. DesciprtionPostDate,
  13. } from "./OfferDetails.styled";
  14. import { useTranslation } from "react-i18next";
  15. import { formatDateLocale } from "../../../../util/helpers/dateHelpers";
  16. import useIsMobile from "../../../../hooks/useIsMobile";
  17. import { getImageUrl, variants } from "../../../../util/helpers/imageUrlGetter";
  18. import { useEffect } from "react";
  19. import { useState } from "react";
  20. import ImagesCarousel from "../ImagesCarousel/ImagesCarousel";
  21. import OfferDescription from "./OfferDescription/OfferDescription";
  22. const OfferDetails = (props) => {
  23. const offer = props.offer;
  24. const { t } = useTranslation();
  25. const { isMobile } = useIsMobile();
  26. const [images, setImages] = useState([]);
  27. const [imagesCarouselModal, setImagesCarouselModal] = useState(false);
  28. useEffect(() => {
  29. if (props?.offer?.images) {
  30. props.offer.images.map((file) => {
  31. if (file) {
  32. if (typeof file !== "string") {
  33. var reader = new FileReader();
  34. reader.readAsDataURL(file);
  35. reader.onload = function () {
  36. setImages((prevImages) => [...prevImages, reader.result]);
  37. };
  38. reader.onerror = function (error) {
  39. console.log("Error: ", error);
  40. };
  41. } else {
  42. setImages((prevImages) => [
  43. ...prevImages,
  44. getImageUrl(file, variants.offerCard, isMobile),
  45. ]);
  46. }
  47. }
  48. });
  49. }
  50. return () => {
  51. setImages([]);
  52. };
  53. }, [props?.offer?.images]);
  54. const date = formatDateLocale(new Date(offer?._created));
  55. const onModalClose = () => {
  56. setImagesCarouselModal(false);
  57. };
  58. console.log(props);
  59. return (
  60. <>
  61. <Details
  62. hasScrollBar={!props.showPublishButton}
  63. exchange={props.showExchangeButton}
  64. singleOffer={props.singleOffer}
  65. previewCard={props.previewCard}
  66. >
  67. {!isMobile && props.singleOffer && (
  68. <ScrollerVertical>
  69. {images.map((item, index) => (
  70. <OfferImage
  71. src={images[index]}
  72. alt={t("offer.imageAlt")}
  73. key={item}
  74. previewCard={props.previewCard}
  75. onClick={() =>
  76. !props.previewCard && setImagesCarouselModal(true)
  77. }
  78. />
  79. ))}
  80. </ScrollerVertical>
  81. )}
  82. <OfferInfoContainer
  83. singleOffer={props.singleOffer}
  84. previewCard={props.previewCard}
  85. >
  86. <OfferTitle singleOffer={props.singleOffer}>{offer?.name}</OfferTitle>
  87. {isMobile && (
  88. <ScrollerHorizontal>
  89. {images.map((item, index) => {
  90. if (!item) return;
  91. return (
  92. <OfferImage
  93. src={images[index]}
  94. key={item}
  95. previewCard={props.previewCard}
  96. onClick={() =>
  97. !props.previewCard && setImagesCarouselModal(true)
  98. }
  99. />
  100. );
  101. })}
  102. </ScrollerHorizontal>
  103. )}
  104. <OfferLittleDetails
  105. singleOffer={props.singleOffer}
  106. previewCard={props.previewCard}
  107. >
  108. <OfferDescriptionTitle>
  109. {t("itemDetailsCard.description")}
  110. </OfferDescriptionTitle>
  111. <OfferDescription value={offer?.description} />
  112. <DesciprtionPostDate previewCard={props.previewCard}>
  113. {date}
  114. </DesciprtionPostDate>
  115. </OfferLittleDetails>
  116. </OfferInfoContainer>
  117. </Details>
  118. {imagesCarouselModal && (
  119. <ImagesCarousel
  120. offer={props.offer}
  121. images={images}
  122. onModalClose={onModalClose}
  123. />
  124. )}
  125. </>
  126. );
  127. };
  128. OfferDetails.propTypes = {
  129. offer: PropTypes.any,
  130. showExchangeButton: PropTypes.bool,
  131. showPublishButton: PropTypes.bool,
  132. singleOffer: PropTypes.bool,
  133. previewCard: PropTypes.bool,
  134. createOffer: PropTypes.bool,
  135. };
  136. export default OfferDetails;