|
| 1 | +import { useContext, useEffect, useRef, useState } from 'react'; |
| 2 | +import './Chat.scss'; |
| 3 | +import { hostUrl } from '../../common/urls'; |
| 4 | +import { UserContext } from '../../context/UserContext'; |
| 5 | + |
| 6 | +export default function Chat() { |
| 7 | + const params = new URLSearchParams(window.location.search); |
| 8 | + const interlocutorId = params.get('interlocutorId'); |
| 9 | + const names = params.get('names') |
| 10 | + const [message, setMessage] = useState(''); |
| 11 | + const [messages, setMessages] = useState(); |
| 12 | + const [chatHistory, setChatHistory] = useState([]); |
| 13 | + const { user } = useContext(UserContext); |
| 14 | + const ref = useRef() |
| 15 | + |
| 16 | + const getMessages = () => { |
| 17 | + fetch(`${hostUrl}/message/${user._id}&${interlocutorId}`) |
| 18 | + .then((resp) => resp.json()) |
| 19 | + .then(setMessages); |
| 20 | + }; |
| 21 | + |
| 22 | + const sortMessages = () => { |
| 23 | + let sorted = Object.entries(messages) |
| 24 | + .map((arr) => |
| 25 | + arr[1].map((v) => ({ |
| 26 | + position: v.sender_id === user._id ? 'right' : 'left', |
| 27 | + text: v.text, |
| 28 | + id: v._id, |
| 29 | + date: v.createdAt, |
| 30 | + })) |
| 31 | + ) |
| 32 | + .flat() |
| 33 | + .sort((a, b) => new Date(a.date) - new Date(b.date)); |
| 34 | + setChatHistory(sorted); |
| 35 | + }; |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + if (!interlocutorId || !user._id) return; |
| 39 | + getMessages(); |
| 40 | + }, [interlocutorId, user._id]); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (!messages) return; |
| 44 | + sortMessages(); |
| 45 | + }, [messages]); |
| 46 | + |
| 47 | + const handleOnMessageChange = (e) => { |
| 48 | + setMessage(e.target.value); |
| 49 | + }; |
| 50 | + |
| 51 | + const postMessage = () => { |
| 52 | + const postBody = { |
| 53 | + sender_id: user._id, |
| 54 | + receiver_id: interlocutorId, |
| 55 | + text: message, |
| 56 | + }; |
| 57 | + fetch(`${hostUrl}/message`, { |
| 58 | + method: 'POST', |
| 59 | + body: JSON.stringify(postBody), |
| 60 | + headers: { |
| 61 | + 'Content-Type': 'application/json', |
| 62 | + }, |
| 63 | + }) |
| 64 | + .then((resp) => resp.json()) |
| 65 | + .then((json) => { |
| 66 | + setChatHistory([...chatHistory, { |
| 67 | + id: json._id, |
| 68 | + text: json.text, |
| 69 | + position: "right" |
| 70 | + }]) |
| 71 | + setMessage(''); |
| 72 | + ref.current.focus() |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + const handleOnMessageSend = () => { |
| 77 | + postMessage() |
| 78 | + }; |
| 79 | + |
| 80 | + const handleOnKeyDown = (e) => { |
| 81 | + if (e.key === "Enter") { |
| 82 | + postMessage() |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className="chat-container"> |
| 88 | + <h2>Chat with {names}</h2> |
| 89 | + <section className="chat-messages"> |
| 90 | + {chatHistory.map((ch) => ( |
| 91 | + <p key={ch.id} className={`message-${ch.position}`}> |
| 92 | + {ch.text} |
| 93 | + </p> |
| 94 | + ))} |
| 95 | + </section> |
| 96 | + <input |
| 97 | + ref={ref} |
| 98 | + className="chat-input" |
| 99 | + type="text" |
| 100 | + value={message} |
| 101 | + onChange={handleOnMessageChange} |
| 102 | + onKeyDown={handleOnKeyDown} |
| 103 | + /> |
| 104 | + <button className="chat-button" onClick={handleOnMessageSend} type="button"> |
| 105 | + Send |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + ); |
| 109 | +} |
0 commit comments