Skip to content

Commit 2697a8e

Browse files
committed
Multiple messages received
1 parent 58001b2 commit 2697a8e

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import React, { useState, useEffect } from 'react';
2+
import { useDispatch, useSelector } from 'react-redux';
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';
11+
12+
const Chatroom = (props): JSX.Element => {
13+
const collaborationRoom = useSelector((store: RootState) => store.roomSlice);
14+
// const newMessage = useSelector(
15+
// (store: RootState) => store.roomSlice.newMessage
16+
// );
17+
const socket = getSocket();
18+
19+
const nickName = collaborationRoom.userName;
20+
const roomCode = collaborationRoom.roomCode;
21+
// const newMessage = collaborationRoom.newMessage;
22+
23+
const wrapperStyles = {
24+
border: `2px solid #f2fbf8`,
25+
borderRadius: '8px',
26+
width: '70%',
27+
height: '90%',
28+
display: 'column',
29+
padding: '20px',
30+
// justifyContent: 'center',
31+
backgroundColor: '#42464C',
32+
overflow: 'auto'
33+
// scrollTop: 'scrollHeight'
34+
};
35+
36+
const inputContainerStyles = {
37+
width: '100%',
38+
padding: '10px',
39+
display: 'flex',
40+
justifyContent: 'center'
41+
};
42+
43+
const inputStyles = {
44+
width: '70%',
45+
padding: '10px',
46+
border: 'none',
47+
borderRadius: '5px',
48+
backgroundColor: '#333333',
49+
color: 'white'
50+
};
51+
52+
const buttonStyles = {
53+
padding: '10px',
54+
marginLeft: '10px',
55+
backgroundColor: '#4CAF50',
56+
color: 'white',
57+
border: 'none',
58+
borderRadius: '5px',
59+
cursor: 'pointer'
60+
};
61+
62+
const handleSubmit = (e) => {
63+
e.preventDefault();
64+
const messageInput = document.getElementById(
65+
'message-input'
66+
) as HTMLInputElement;
67+
const message = messageInput.value;
68+
emitEvent('send-chat-message', roomCode, { message, nickName });
69+
messageInput.value = '';
70+
};
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+
});
81+
82+
return (
83+
<div
84+
className="livechat-panel"
85+
style={{ paddingLeft: '10px', width: '100%', height: '100%' }}
86+
>
87+
<div className="roomInfo" style={{ paddingLeft: '10px' }}>
88+
<p>Current room: {roomCode}</p>
89+
<p>Your Nickname: {nickName}</p>
90+
</div>
91+
<div style={{ justifyContent: 'center', display: 'flex', height: '90%' }}>
92+
<div id="message-container" style={{ ...wrapperStyles }}></div>
93+
</div>
94+
<form
95+
id="send-container"
96+
style={inputContainerStyles}
97+
onSubmit={handleSubmit}
98+
>
99+
<input type="text" id="message-input" style={inputStyles} />
100+
<button type="submit" id="send-button" style={buttonStyles}>
101+
Send
102+
</button>
103+
</form>
104+
</div>
105+
);
106+
};
107+
108+
export default Chatroom;

0 commit comments

Comments
 (0)