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.

DirectChatNewMessage.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React, { useCallback, useEffect, useState } from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. DirectChatNewMessageContainer,
  5. NewMessageField,
  6. SendButton,
  7. } from "./DirectChatNewMessage.styled";
  8. import { useTranslation } from "react-i18next";
  9. import selectedTheme from "../../../themes";
  10. import { useDispatch } from "react-redux";
  11. import { sendMessage } from "../../../socket/socket";
  12. import { useSelector } from "react-redux";
  13. import { selectJwtToken, selectUserId } from "../../../store/selectors/loginSelectors";
  14. import {
  15. addNewMessage,
  16. startNewChat,
  17. } from "../../../store/actions/chat/chatActions";
  18. import { useHistory, useLocation } from "react-router-dom";
  19. import { selectExchange } from "../../../store/selectors/exchangeSelector";
  20. import { validateExchange } from "../../../store/actions/exchange/exchangeActions";
  21. import NotAllowedChat from "./NotAllowedChat/NotAllowedChat";
  22. import { convertLocalDateToUTCDate } from "../../../util/helpers/dateHelpers";
  23. import { selectAmIBlocked } from "../../../store/selectors/profileSelectors";
  24. const DirectChatNewMessage = (props) => {
  25. const [typedValue, setTypedValue] = useState("");
  26. const [isFocused, setIsFocused] = useState(false);
  27. const exchange = useSelector(selectExchange);
  28. const dispatch = useDispatch();
  29. const mineProfileBlocked = useSelector(selectAmIBlocked);
  30. const { t } = useTranslation();
  31. const location = useLocation();
  32. const history = useHistory();
  33. const token = useSelector(selectJwtToken)
  34. const userId = useSelector(selectUserId);
  35. const handleSend = useCallback(
  36. (newChatId = undefined) => {
  37. if (typedValue?.length === 0) return;
  38. if (props?.chat?._id) {
  39. const chatId = props.chat?._id || newChatId;
  40. sendMessage(chatId, userId, typedValue, props.interlocutor._id, token);
  41. dispatch(
  42. addNewMessage({
  43. _id: chatId,
  44. message: {
  45. user: {
  46. _id: userId,
  47. },
  48. text: typedValue,
  49. _created: convertLocalDateToUTCDate(new Date()),
  50. },
  51. })
  52. );
  53. if (props.chat?._id) {
  54. if (!exchange.valid && exchange.seller.user._id === userId) {
  55. dispatch(validateExchange(exchange._id));
  56. }
  57. }
  58. } else {
  59. initiateNewChat(typedValue);
  60. }
  61. setTypedValue("");
  62. },
  63. [typedValue, props.chat?._id, userId, props.interlocutor]
  64. );
  65. const handleMessageSendSuccess = (newChatId) => {
  66. history.replace(`${newChatId}`);
  67. };
  68. const listener = useCallback(
  69. (event) => {
  70. // Event key code 13 = ENTER key
  71. if (event.keyCode === 13) handleSend();
  72. },
  73. [isFocused, typedValue]
  74. );
  75. useEffect(() => {
  76. if (isFocused) window.addEventListener("keypress", listener);
  77. return () => window.removeEventListener("keypress", listener);
  78. }, [typedValue, isFocused]);
  79. const initiateNewChat = (typedValue) => {
  80. const offerId = location.state.offerId;
  81. dispatch(
  82. startNewChat({
  83. offerId,
  84. message: typedValue,
  85. interlocutorUserId: props.interlocutor._id,
  86. handleMessageSendSuccess,
  87. })
  88. );
  89. };
  90. console.log(props)
  91. if (mineProfileBlocked) {
  92. return <NotAllowedChat mineProfileBlocked />;
  93. }
  94. if (props?.interlocutor?._deleted) {
  95. return <NotAllowedChat deleted />;
  96. }
  97. if (props?.chat?.offer?._deleted) {
  98. return <NotAllowedChat />;
  99. }
  100. // if (props?.interlocutor?._blocked) {
  101. // return <NotAllowedChat blocked />;
  102. // }
  103. return (
  104. <>
  105. <DirectChatNewMessageContainer>
  106. <NewMessageField
  107. placeholder={t("messages.sendPlaceholder")}
  108. fullWidth
  109. onFocus={() => setIsFocused(true)}
  110. onBlur={() => setIsFocused(false)}
  111. italicPlaceholder
  112. value={typedValue}
  113. onChange={(typed) => setTypedValue(typed.target.value)}
  114. />
  115. <SendButton
  116. onClick={handleSend}
  117. buttoncolor={selectedTheme.colors.primaryPurple}
  118. variant="contained"
  119. >
  120. {t("messages.send")}
  121. </SendButton>
  122. </DirectChatNewMessageContainer>
  123. </>
  124. );
  125. };
  126. DirectChatNewMessage.propTypes = {
  127. children: PropTypes.node,
  128. chatId: PropTypes.any,
  129. refreshChat: PropTypes.func,
  130. interlocutor: PropTypes.any,
  131. chat: PropTypes.any,
  132. };
  133. export default DirectChatNewMessage;