Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MyMessages.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React, { useEffect, useState } from "react";
  2. import { useTranslation } from "react-i18next";
  3. import { useDispatch, useSelector } from "react-redux";
  4. import { useHistory } from "react-router-dom";
  5. import { fetchHeaderChats } from "../../../store/actions/chat/chatActions";
  6. import {
  7. selectLatestChats,
  8. selectSelectedChat,
  9. } from "../../../store/selectors/chatSelectors";
  10. import { selectUserId } from "../../../store/selectors/loginSelectors";
  11. import HeaderPopover from "../HeaderPopover/HeaderPopover";
  12. import PropTypes from "prop-types";
  13. import { makeErrorToastMessage } from "../../../store/utils/makeToastMessage";
  14. import { EyeIcon } from "./MyMessages.styled";
  15. import { DIRECT_CHAT_PAGE } from "../../../constants/pages";
  16. import {
  17. dynamicRouteMatches,
  18. replaceInRoute,
  19. } from "../../../util/helpers/routeHelpers";
  20. export const MyMessages = (props) => {
  21. const { t } = useTranslation();
  22. const dispatch = useDispatch();
  23. const userId = useSelector(selectUserId);
  24. const chats = useSelector(selectLatestChats);
  25. const selectedChat = useSelector(selectSelectedChat);
  26. const history = useHistory();
  27. const [lastChats, setLastChats] = useState([]);
  28. const convertMessages = (messages) => {
  29. return messages
  30. .map((item) => {
  31. let interlocutor = userId === item.participants[0]._id ? 1 : 0;
  32. return {
  33. src: item.participants[interlocutor]?.image,
  34. title: item.participants[interlocutor]?.company?.name,
  35. onClick: () => goToMessage(item?._id),
  36. text: "Proizvod: ",
  37. bigText: item.offer.name,
  38. };
  39. })
  40. .slice(0, 2);
  41. };
  42. useEffect(() => {
  43. if (userId?.length > 1) {
  44. dispatch(fetchHeaderChats(userId));
  45. }
  46. }, [userId]);
  47. useEffect(() => {
  48. if (chats?.length > 0) {
  49. setLastChats([...convertMessages(chats)]);
  50. }
  51. }, [chats]);
  52. const goToMessages = () => {
  53. if (lastChats.length !== 0) {
  54. console.log(chats);
  55. goToMessage(chats[0]?._id)
  56. } else {
  57. makeErrorToastMessage(t("messages.noMessagesToast"));
  58. props.closePopover();
  59. }
  60. };
  61. const goToMessage = (chatId) => {
  62. if (
  63. !dynamicRouteMatches(DIRECT_CHAT_PAGE) ||
  64. selectedChat?._id !== chatId
  65. ) {
  66. history.push(
  67. replaceInRoute(DIRECT_CHAT_PAGE, {
  68. chatId,
  69. })
  70. );
  71. }
  72. props.closePopover();
  73. };
  74. return (
  75. <HeaderPopover
  76. title={t("header.myMessages")}
  77. items={lastChats}
  78. buttonOnClick={goToMessages}
  79. buttonText={t("header.checkEverything")}
  80. buttonIcon={<EyeIcon color={chats?.length === 0} />}
  81. noMessages={chats?.length === 0}
  82. />
  83. );
  84. };
  85. MyMessages.propTypes = {
  86. closePopover: PropTypes.func,
  87. };