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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 { 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 userId = useSelector(selectUserId);
  34. const handleSend = useCallback(
  35. (newChatId = undefined) => {
  36. if (typedValue?.length === 0) return;
  37. if (props?.chat?._id) {
  38. const chatId = props.chat?._id || newChatId;
  39. sendMessage(chatId, userId, typedValue, props.interlocutor._id);
  40. dispatch(
  41. addNewMessage({
  42. _id: chatId,
  43. message: {
  44. user: {
  45. _id: userId,
  46. },
  47. text: typedValue,
  48. _created: convertLocalDateToUTCDate(new Date()),
  49. },
  50. })
  51. );
  52. if (props.chat?._id) {
  53. if (!exchange.valid && exchange.seller.user._id === userId) {
  54. dispatch(validateExchange(exchange._id));
  55. }
  56. }
  57. } else {
  58. initiateNewChat(typedValue);
  59. }
  60. setTypedValue("");
  61. },
  62. [typedValue, props.chat?._id, userId, props.interlocutor]
  63. );
  64. const handleMessageSendSuccess = (newChatId) => {
  65. history.replace(`${newChatId}`);
  66. };
  67. const listener = useCallback(
  68. (event) => {
  69. // Event key code 13 = ENTER key
  70. if (event.keyCode === 13) handleSend();
  71. },
  72. [isFocused, typedValue]
  73. );
  74. useEffect(() => {
  75. if (isFocused) window.addEventListener("keypress", listener);
  76. return () => window.removeEventListener("keypress", listener);
  77. }, [typedValue, isFocused]);
  78. const initiateNewChat = (typedValue) => {
  79. const offerId = location.state.offerId;
  80. dispatch(
  81. startNewChat({
  82. offerId,
  83. message: typedValue,
  84. interlocutorUserId: props.interlocutor._id,
  85. handleMessageSendSuccess,
  86. })
  87. );
  88. };
  89. if (mineProfileBlocked) {
  90. return <NotAllowedChat mineProfileBlocked />;
  91. }
  92. if (props?.chat?.interlocutor?._deleted) {
  93. return <NotAllowedChat deleted />;
  94. }
  95. if (props?.chat?.offer?.offer?._deleted) {
  96. return <NotAllowedChat />;
  97. }
  98. if (props?.chat?.interlocutor?._blocked) {
  99. return <NotAllowedChat blocked />;
  100. }
  101. return (
  102. <>
  103. <DirectChatNewMessageContainer>
  104. <NewMessageField
  105. placeholder={t("messages.sendPlaceholder")}
  106. fullWidth
  107. onFocus={() => setIsFocused(true)}
  108. onBlur={() => setIsFocused(false)}
  109. italicPlaceholder
  110. value={typedValue}
  111. onChange={(typed) => setTypedValue(typed.target.value)}
  112. />
  113. <SendButton
  114. onClick={handleSend}
  115. buttoncolor={selectedTheme.colors.primaryPurple}
  116. variant="contained"
  117. >
  118. {t("messages.send")}
  119. </SendButton>
  120. </DirectChatNewMessageContainer>
  121. </>
  122. );
  123. };
  124. DirectChatNewMessage.propTypes = {
  125. children: PropTypes.node,
  126. chatId: PropTypes.any,
  127. refreshChat: PropTypes.func,
  128. interlocutor: PropTypes.any,
  129. chat: PropTypes.any,
  130. };
  131. export default DirectChatNewMessage;