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
@@ -93,10 +94,6 @@ const Chatroom = (props): JSX.Element => {
93
94
className = "livechat-panel"
94
95
style = { { paddingLeft : '10px' , width : '100%' , height : '100%' } }
95
96
>
96
- < div className = "roomInfo" style = { { paddingLeft : '70px' } } >
97
- < p > Current room: { roomCode } </ p >
98
- < p > Your nickname: { userName } </ p >
99
- </ div >
100
97
< div style = { { justifyContent : 'center' , display : 'flex' , height : '80%' } } >
101
98
< div id = "message-container" style = { wrapperStyles } >
102
99
{ renderMessages ( ) }
@@ -107,7 +104,13 @@ const Chatroom = (props): JSX.Element => {
107
104
style = { inputContainerStyles }
108
105
onSubmit = { handleSubmit }
109
106
>
110
- < input type = "text" id = "message-input" style = { inputStyles } />
107
+ < input
108
+ type = "text"
109
+ id = "message-input"
110
+ onChange = { ( e ) => setInputContent ( e . target . value ) }
111
+ value = { inputContent }
112
+ style = { inputStyles }
113
+ />
111
114
< button type = "submit" id = "send-button" style = { buttonStyles } >
112
115
Send
113
116
</ button >
0 commit comments