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.

ItemDetailsCard.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import React, { useEffect, useMemo } from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. CheckButton,
  5. ItemDetailsCardContainer,
  6. OfferInfo,
  7. Info,
  8. PostDate,
  9. CategoryIcon,
  10. SubcategoryIcon,
  11. QuantityIcon,
  12. EyeIcon,
  13. } from "./ItemDetailsCard.styled";
  14. import selectedTheme from "../../../themes";
  15. import { useDispatch, useSelector } from "react-redux";
  16. import { selectLatestChats } from "../../../store/selectors/chatSelectors";
  17. import { increaseCounter } from "../../../store/actions/counter/counterActions";
  18. import _ from "lodash";
  19. import { selectUserId } from "../../../store/selectors/loginSelectors";
  20. import { formatDateLocale } from "../../../util/helpers/dateHelpers";
  21. import { startChat } from "../../../util/helpers/chatHelper";
  22. import Information from "./Information/Information";
  23. import { useTranslation } from "react-i18next";
  24. import OfferDetails from "./OfferDetails/OfferDetails";
  25. const ItemDetailsCard = (props) => {
  26. const offer = props.offer;
  27. const chats = useSelector(selectLatestChats);
  28. const userId = useSelector(selectUserId);
  29. const { t } = useTranslation();
  30. const dispatch = useDispatch();
  31. const increaseOfferCounter = useMemo(() => {
  32. return _.once(function (id) {
  33. dispatch(increaseCounter(id));
  34. });
  35. }, []);
  36. useEffect(() => {
  37. if (offer?.offer?._id) {
  38. increaseOfferCounter(offer?.offer?._id);
  39. }
  40. }, [offer]);
  41. const date = formatDateLocale(new Date(offer?.offer?._created));
  42. const startExchange = () => {
  43. startChat(chats, offer?.offer, userId);
  44. };
  45. return (
  46. <ItemDetailsCardContainer
  47. sponsored={props.sponsored.toString()}
  48. halfwidth={props.halfwidth ? 1 : 0}
  49. className={props.className}
  50. >
  51. <OfferInfo>
  52. <Info>
  53. <Information
  54. icon={<CategoryIcon />}
  55. value={offer?.offer?.category?.name}
  56. />
  57. <Information
  58. icon={<SubcategoryIcon />}
  59. value={offer?.offer?.subcategory}
  60. />
  61. <Information
  62. icon={<QuantityIcon />}
  63. value={offer?.offer?.condition}
  64. />
  65. <Information icon={<EyeIcon />} value={offer?.offer?.views?.count} />
  66. </Info>
  67. <PostDate>{date}</PostDate>
  68. </OfferInfo>
  69. <OfferDetails offer={offer}/>
  70. {!props.halfwidth && props.showExchangeButton && (
  71. <CheckButton
  72. variant={props.sponsored ? "contained" : "outlined"}
  73. buttoncolor={selectedTheme.primaryPurple}
  74. textcolor={props.sponsored ? "white" : selectedTheme.primaryPurple}
  75. onClick={startExchange}
  76. >
  77. {t("itemDetailsCard.startExchangeButton")}
  78. </CheckButton>
  79. )}
  80. </ItemDetailsCardContainer>
  81. );
  82. };
  83. ItemDetailsCard.propTypes = {
  84. halfwidth: PropTypes.bool,
  85. sponsored: PropTypes.bool,
  86. offer: PropTypes.any,
  87. hideViews: PropTypes.bool,
  88. showExchangeButton: PropTypes.bool,
  89. showBarterButton: PropTypes.bool,
  90. showPublishButton: PropTypes.bool,
  91. className: PropTypes.string,
  92. };
  93. ItemDetailsCard.defaultProps = {
  94. halfwidth: false,
  95. sponsored: false,
  96. showExchangeButton: true,
  97. };
  98. export default ItemDetailsCard;