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.

ItemDetailsHeaderCard.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. MessageIcon,
  5. OfferDetails,
  6. OfferImage,
  7. OfferTitle,
  8. HeaderTop,
  9. UserIcon,
  10. UserIconContainer,
  11. TooltipInnerContainer,
  12. } from "./ItemDetailsHeaderCard.styled";
  13. import { ItemDetailsHeaderContainer } from "./ItemDetailsHeaderCard.styled";
  14. import { ReactComponent as MessageColor } from "../../../assets/images/svg/mailColor.svg";
  15. import { useHistory, useRouteMatch } from "react-router-dom";
  16. import { useSelector } from "react-redux";
  17. import { selectLatestChats } from "../../../store/selectors/chatSelectors";
  18. import { selectUserId } from "../../../store/selectors/loginSelectors";
  19. import StatisticDetails from "./StatisticDetails/StatisticDetails";
  20. import PIBDetail from "./OfferDetail/PIB/PIBDetail";
  21. import CategoryDetail from "./OfferDetail/Category/CategoryDetail";
  22. import useIsMobile from "../../../hooks/useIsMobile";
  23. import { getImageUrl, variants } from "../../../util/helpers/imageUrlGetter";
  24. import { useMemo } from "react";
  25. import itemDetailsData from "../../../notFoundData/itemDetailsData";
  26. import { Tooltip } from "@mui/material";
  27. import { useTranslation } from "react-i18next";
  28. import { replaceInRoute } from "../../../util/helpers/routeHelpers";
  29. import {
  30. ADMIN_SINGLE_USER_PAGE,
  31. DIRECT_CHAT_PAGE,
  32. PROFILE_PAGE,
  33. } from "../../../constants/pages";
  34. import { NEW_CHAT } from "../../../constants/chatConstants";
  35. import { VerifiedUserContainer } from "../../Cards/ProfileCard/ProfileMainInfo/ProfileMainInfo.styled";
  36. import { ReactComponent as VerifiedIcon } from "../../../assets/images/svg/verified-user.svg";
  37. const ItemDetailsHeaderCard = (props) => {
  38. const history = useHistory();
  39. const chats = useSelector(selectLatestChats);
  40. const routeMatch = useRouteMatch();
  41. const { t } = useTranslation();
  42. const offer = useMemo(() => {
  43. if (props.offer) {
  44. if (props.offer._id === routeMatch.params.offerId) {
  45. return props.offer;
  46. }
  47. }
  48. return itemDetailsData;
  49. }, [props.offer]);
  50. const userId = useSelector(selectUserId);
  51. const { isMobile } = useIsMobile();
  52. const handleGoProfile = () => {
  53. if (props?.isAdmin) {
  54. history.push(
  55. replaceInRoute(ADMIN_SINGLE_USER_PAGE, {
  56. profileId: props?.offer?.user?._id,
  57. })
  58. );
  59. } else {
  60. history.push(
  61. replaceInRoute(PROFILE_PAGE, {
  62. profileId: props?.offer?.user?._id,
  63. })
  64. );
  65. }
  66. };
  67. const messageUser = (offer) => {
  68. const chatItem = chats.find((item) => item.offer._id === offer?._id);
  69. if (chatItem !== undefined) {
  70. console.log(chatItem);
  71. history.push(
  72. replaceInRoute(DIRECT_CHAT_PAGE, {
  73. chatId: chatItem._id,
  74. })
  75. );
  76. } else {
  77. if (offer?.user?._id !== userId) {
  78. history.push({
  79. pathname: replaceInRoute(DIRECT_CHAT_PAGE, {
  80. chatId: NEW_CHAT,
  81. }),
  82. state: {
  83. offerId: offer?._id,
  84. },
  85. });
  86. }
  87. }
  88. };
  89. return (
  90. <ItemDetailsHeaderContainer
  91. isMyProfile={props.isMyProfile}
  92. halfwidth={props.halfwidth ? 1 : 0}
  93. >
  94. <HeaderTop>
  95. <OfferImage
  96. src={getImageUrl(
  97. offer?.user?.image ? offer.user.image : "",
  98. variants.profileImage,
  99. isMobile
  100. )}
  101. />
  102. <OfferDetails>
  103. <OfferTitle isMyProfile={props.isMyProfile} onClick={handleGoProfile}>
  104. {offer?.user?.company?.name}
  105. {props.verify && props.verifiedUser && (
  106. <Tooltip title={t("profile.verifiedTooltip")} placement="right">
  107. <VerifiedUserContainer>
  108. <VerifiedIcon />
  109. </VerifiedUserContainer>
  110. </Tooltip>
  111. )}
  112. </OfferTitle>
  113. <PIBDetail offer={offer} isMyProfile={props.isMyProfile} />
  114. <CategoryDetail offer={offer} isMyProfile={props.isMyProfile} />
  115. </OfferDetails>
  116. {props.isMyProfile ? (
  117. <UserIconContainer onClick={handleGoProfile}>
  118. <UserIcon />
  119. </UserIconContainer>
  120. ) : (
  121. <Tooltip title={t("messages.tooltip")}>
  122. <TooltipInnerContainer>
  123. <MessageIcon onClick={() => messageUser(offer)}>
  124. <MessageColor />
  125. </MessageIcon>
  126. </TooltipInnerContainer>
  127. </Tooltip>
  128. )}
  129. </HeaderTop>
  130. {!props.singleOffer && <StatisticDetails offer={offer} />}
  131. </ItemDetailsHeaderContainer>
  132. );
  133. };
  134. ItemDetailsHeaderCard.propTypes = {
  135. children: PropTypes.node,
  136. id: PropTypes.number,
  137. title: PropTypes.string,
  138. description: PropTypes.string,
  139. category: PropTypes.string,
  140. author: PropTypes.string,
  141. location: PropTypes.string,
  142. image: PropTypes.node,
  143. quantity: PropTypes.number,
  144. package: PropTypes.string,
  145. numberOfViews: PropTypes.number,
  146. halfwidth: PropTypes.bool,
  147. sponsored: PropTypes.bool,
  148. offer: PropTypes.any,
  149. isMyProfile: PropTypes.bool,
  150. singleOffer: PropTypes.bool,
  151. isAdmin: PropTypes.bool,
  152. verify: PropTypes.bool,
  153. verifiedUser: PropTypes.bool,
  154. };
  155. ItemDetailsHeaderCard.defaultProps = {
  156. halfwidth: false,
  157. sponsored: false,
  158. };
  159. export default ItemDetailsHeaderCard;