1
- import React , { useState , useEffect } from 'react' ;
2
- import { useDispatch , useSelector } from 'react-redux' ;
1
+ import React , { useEffect } from 'react' ;
2
+ import { useSelector } from 'react-redux' ;
3
3
import { RootState } from '../../redux/store' ;
4
- import Box from '@mui/material/Box' ;
5
- import {
6
- initializeSocket ,
7
- getSocket ,
8
- emitEvent ,
9
- disconnectSocket
10
- } from '../../helperFunctions/socket' ;
4
+ import { emitEvent } from '../../helperFunctions/socket' ;
11
5
12
6
const Chatroom = ( props ) : JSX . Element => {
13
7
const collaborationRoom = useSelector ( ( store : RootState ) => store . roomSlice ) ;
14
- // const newMessage = useSelector(
15
- // (store: RootState) => store.roomSlice.newMessage
16
- // );
17
- const socket = getSocket ( ) ;
8
+
9
+ const newMessage = useSelector (
10
+ ( store : RootState ) => store . roomSlice . newMessage
11
+ ) ;
18
12
19
13
const nickName = collaborationRoom . userName ;
20
14
const roomCode = collaborationRoom . roomCode ;
21
- // const newMessage = collaborationRoom.newMessage;
22
15
23
16
const wrapperStyles = {
24
17
border : `2px solid #f2fbf8` ,
25
18
borderRadius : '8px' ,
26
19
width : '70%' ,
27
- height : '90 %' ,
20
+ height : '100 %' ,
28
21
display : 'column' ,
29
22
padding : '20px' ,
30
- // justifyContent: 'center',
31
23
backgroundColor : '#42464C' ,
32
24
overflow : 'auto'
33
- // scrollTop: 'scrollHeight'
34
25
} ;
35
26
36
27
const inputContainerStyles = {
37
28
width : '100%' ,
38
- padding : '10px' ,
29
+ paddingLeft : '30px' ,
30
+ paddingTop : '10px' ,
39
31
display : 'flex' ,
40
32
justifyContent : 'center'
41
33
} ;
@@ -52,7 +44,7 @@ const Chatroom = (props): JSX.Element => {
52
44
const buttonStyles = {
53
45
padding : '10px' ,
54
46
marginLeft : '10px' ,
55
- backgroundColor : '#4CAF50 ' ,
47
+ backgroundColor : '#0070BA ' ,
56
48
color : 'white' ,
57
49
border : 'none' ,
58
50
borderRadius : '5px' ,
@@ -64,31 +56,44 @@ const Chatroom = (props): JSX.Element => {
64
56
const messageInput = document . getElementById (
65
57
'message-input'
66
58
) as HTMLInputElement ;
67
- const message = messageInput . value ;
68
- emitEvent ( 'send-chat-message' , roomCode , { message, nickName } ) ;
69
- messageInput . value = '' ;
59
+ const message = messageInput . value . trim ( ) ;
60
+ if ( message !== '' ) {
61
+ emitEvent ( 'send-chat-message' , roomCode , { message, nickName } ) ;
62
+ messageInput . value = '' ;
63
+ }
64
+ } ;
65
+
66
+ const handleMessageStyle = ( messageNickName : string ) => {
67
+ return {
68
+ color : messageNickName === nickName ? '#66C4EB' : 'white'
69
+ } ;
70
70
} ;
71
71
72
- socket . on ( 'new chat message' , ( remoteData ) => {
73
- const messageContainer = document . getElementById ( 'message-container' ) ;
74
- const messageElement = document . createElement ( 'div' ) ;
75
- messageElement . innerText =
76
- nickName === remoteData . nickName
77
- ? `You: ${ remoteData . message } `
78
- : `${ remoteData . nickName } : ${ remoteData . message } ` ;
79
- messageContainer . append ( messageElement ) ;
80
- } ) ;
72
+ useEffect ( ( ) => {
73
+ if ( newMessage . nickName !== '' && newMessage . message !== '' ) {
74
+ const messageContainer = document . getElementById ( 'message-container' ) ;
75
+ const messageElement = document . createElement ( 'div' ) ;
76
+ messageElement . innerText =
77
+ nickName === newMessage . nickName
78
+ ? `You: ${ newMessage . message } `
79
+ : `${ newMessage . nickName } : ${ newMessage . message } ` ;
80
+ messageElement . style . color = handleMessageStyle (
81
+ newMessage . nickName
82
+ ) . color ;
83
+ messageContainer . append ( messageElement ) ;
84
+ }
85
+ } , [ newMessage ] ) ;
81
86
82
87
return (
83
88
< div
84
89
className = "livechat-panel"
85
90
style = { { paddingLeft : '10px' , width : '100%' , height : '100%' } }
86
91
>
87
- < div className = "roomInfo" style = { { paddingLeft : '10px ' } } >
92
+ < div className = "roomInfo" style = { { paddingLeft : '70px ' } } >
88
93
< p > Current room: { roomCode } </ p >
89
94
< p > Your Nickname: { nickName } </ p >
90
95
</ div >
91
- < div style = { { justifyContent : 'center' , display : 'flex' , height : '90 %' } } >
96
+ < div style = { { justifyContent : 'center' , display : 'flex' , height : '80 %' } } >
92
97
< div id = "message-container" style = { { ...wrapperStyles } } > </ div >
93
98
</ div >
94
99
< form
0 commit comments