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

MyMessages.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 { selectLatestChats } from "../../../store/selectors/chatSelectors";
  7. import { selectUserId } from "../../../store/selectors/loginSelectors";
  8. import HeaderPopover from "../HeaderPopover/HeaderPopover";
  9. import PropTypes from "prop-types";
  10. export const MyMessages = (props) => {
  11. const { t } = useTranslation();
  12. const dispatch = useDispatch();
  13. const userId = useSelector(selectUserId);
  14. const chats = useSelector(selectLatestChats);
  15. const history = useHistory();
  16. const [lastChats, setLastChats] = useState([]);
  17. const convertMessages = (messages) => {
  18. return messages
  19. .map((item) => ({
  20. src: item.interlocutorData.image,
  21. title: item.interlocutorData.name,
  22. onClick: () => goToMessage(item?.chat?._id),
  23. text: item?.chat?.messages[item?.chat?.messages?.length - 1]?.text,
  24. }))
  25. .slice(0, 2);
  26. };
  27. useEffect(() => {
  28. if (userId?.length > 1 && chats?.length === 0) {
  29. dispatch(fetchHeaderChats(userId));
  30. }
  31. }, [userId]);
  32. useEffect(() => {
  33. if (chats?.length > 0) {
  34. setLastChats([...convertMessages(chats)]);
  35. }
  36. }, [chats]);
  37. const goToMessages = () => {
  38. history.push(`/messages/${chats[0].chat?._id}`);
  39. props.closePopover();
  40. };
  41. const goToMessage = (chatId) => {
  42. history.push(`/messages/${chatId}`);
  43. props.closePopover();
  44. };
  45. return (
  46. <HeaderPopover
  47. title={t("header.myMessages")}
  48. items={lastChats}
  49. buttonOnClick={goToMessages}
  50. buttonText={t("header.checkEverything")}
  51. />
  52. );
  53. };
  54. MyMessages.propTypes = {
  55. closePopover: PropTypes.func,
  56. };