|
1 | 1 | import React from 'react';
|
| 2 | +import { useState } from 'react'; |
2 | 3 | import { useSelector } from 'react-redux';
|
3 | 4 | import { RootState } from '../../redux/store';
|
4 | 5 | import { emitEvent } from '../../helperFunctions/socket';
|
5 | 6 |
|
6 | 7 | const Chatroom = (props): JSX.Element => {
|
7 | 8 | const userName = useSelector((store: RootState) => store.roomSlice.userName);
|
8 | 9 | const roomCode = useSelector((store: RootState) => store.roomSlice.roomCode);
|
9 |
| - |
10 | 10 | const messages = useSelector((store: RootState) => store.roomSlice.messages);
|
11 | 11 |
|
| 12 | + const [inputContent, setInputContent] = useState(''); |
| 13 | + |
12 | 14 | const wrapperStyles = {
|
13 | 15 | border: `2px solid #f2fbf8`,
|
14 | 16 | borderRadius: '8px',
|
@@ -49,13 +51,12 @@ const Chatroom = (props): JSX.Element => {
|
49 | 51 |
|
50 | 52 | const handleSubmit = (e) => {
|
51 | 53 | e.preventDefault();
|
52 |
| - const messageInput = document.getElementById( |
53 |
| - 'message-input' |
54 |
| - ) as HTMLInputElement; |
55 |
| - const message = messageInput.value.trim(); |
56 |
| - if (message !== '') { |
57 |
| - emitEvent('send-chat-message', roomCode, { userName, message }); |
58 |
| - messageInput.value = ''; |
| 54 | + if (inputContent !== '') { |
| 55 | + emitEvent('send-chat-message', roomCode, { |
| 56 | + userName, |
| 57 | + message: inputContent |
| 58 | + }); |
| 59 | + setInputContent(''); |
59 | 60 | }
|
60 | 61 | };
|
61 | 62 |
|
@@ -107,7 +108,13 @@ const Chatroom = (props): JSX.Element => {
|
107 | 108 | style={inputContainerStyles}
|
108 | 109 | onSubmit={handleSubmit}
|
109 | 110 | >
|
110 |
| - <input type="text" id="message-input" style={inputStyles} /> |
| 111 | + <input |
| 112 | + type="text" |
| 113 | + id="message-input" |
| 114 | + onChange={(e) => setInputContent(e.target.value)} |
| 115 | + value={inputContent} |
| 116 | + style={inputStyles} |
| 117 | + /> |
111 | 118 | <button type="submit" id="send-button" style={buttonStyles}>
|
112 | 119 | Send
|
113 | 120 | </button>
|
|
0 commit comments