Skip to content

Commit 34f4604

Browse files
committed
bug fix that messages missing when leave the tab
1 parent 4effcbc commit 34f4604

File tree

4 files changed

+61
-72
lines changed

4 files changed

+61
-72
lines changed

app/src/components/bottom/chatRoom.tsx

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import React, { useEffect } from 'react';
1+
import React from 'react';
22
import { useSelector } from 'react-redux';
33
import { RootState } from '../../redux/store';
44
import { emitEvent } from '../../helperFunctions/socket';
55

66
const Chatroom = (props): JSX.Element => {
7-
const nickName = useSelector((store: RootState) => store.roomSlice.userName);
7+
const userName = useSelector((store: RootState) => store.roomSlice.userName);
88
const roomCode = useSelector((store: RootState) => store.roomSlice.roomCode);
99

10-
const newMessage = useSelector(
11-
(store: RootState) => store.roomSlice.newMessage
12-
);
13-
14-
const userChange = useSelector(
15-
(store: RootState) => store.roomSlice.userChange
16-
);
10+
const messages = useSelector((store: RootState) => store.roomSlice.messages);
1711

1812
const wrapperStyles = {
1913
border: `2px solid #f2fbf8`,
@@ -60,44 +54,39 @@ const Chatroom = (props): JSX.Element => {
6054
) as HTMLInputElement;
6155
const message = messageInput.value.trim();
6256
if (message !== '') {
63-
emitEvent('send-chat-message', roomCode, { message, nickName });
57+
emitEvent('send-chat-message', roomCode, { userName, message });
6458
messageInput.value = '';
6559
}
6660
};
6761

68-
const handleMessageStyle = (messageNickName: string) => {
69-
return {
70-
color: messageNickName === nickName ? '#66C4EB' : 'white'
71-
};
72-
};
73-
74-
useEffect(() => {
75-
if (newMessage.nickName !== '' && newMessage.message !== '') {
76-
const messageContainer = document.getElementById('message-container');
77-
const messageElement = document.createElement('div');
78-
messageElement.innerText =
79-
nickName === newMessage.nickName
80-
? `You: ${newMessage.message}`
81-
: `${newMessage.nickName}: ${newMessage.message}`;
82-
messageElement.style.color = handleMessageStyle(
83-
newMessage.nickName
84-
).color;
85-
messageContainer.append(messageElement);
62+
const handleMessageContainerStyle = (message: object) => {
63+
if (message['type'] === 'activity') {
64+
return { color: 'yellow' };
65+
} else {
66+
if (message['userName'] === userName) return { color: '#66C4EB' };
67+
return { color: 'white' };
8668
}
87-
}, [newMessage]);
69+
};
8870

89-
useEffect(() => {
90-
if (userChange.nickName !== '' && userChange.status !== '') {
91-
const messageContainer = document.getElementById('message-container');
92-
const messageElement = document.createElement('div');
93-
messageElement.innerText =
94-
userChange.status === 'JOIN'
95-
? `${userChange.nickName} joined the chat room`
96-
: `${userChange.nickName} left the chat room`;
97-
messageElement.style.color = 'yellow';
98-
messageContainer.append(messageElement);
99-
}
100-
}, [userChange]);
71+
const renderMessages = () => {
72+
return messages.map((message, index) => {
73+
if (message.type === 'activity') {
74+
return (
75+
<div key={index} style={handleMessageContainerStyle(message)}>
76+
{message.message}
77+
</div>
78+
);
79+
} else if (message.type === 'chat') {
80+
return (
81+
<div key={index} style={handleMessageContainerStyle(message)}>
82+
{message.userName === userName ? 'You' : message.userName}:{' '}
83+
{message.message}
84+
</div>
85+
);
86+
}
87+
return null;
88+
});
89+
};
10190

10291
return (
10392
<div
@@ -106,10 +95,12 @@ const Chatroom = (props): JSX.Element => {
10695
>
10796
<div className="roomInfo" style={{ paddingLeft: '70px' }}>
10897
<p>Current room: {roomCode}</p>
109-
<p>Your nickname: {nickName}</p>
98+
<p>Your nickname: {userName}</p>
11099
</div>
111100
<div style={{ justifyContent: 'center', display: 'flex', height: '80%' }}>
112-
<div id="message-container" style={wrapperStyles}></div>
101+
<div id="message-container" style={wrapperStyles}>
102+
{renderMessages()}
103+
</div>
113104
</div>
114105
<form
115106
id="send-container"

app/src/components/left/RoomsContainer.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ import {
4141
setUserName,
4242
setUserJoined,
4343
setUserList,
44-
setNewMessage,
45-
setUserChange
44+
setMessages
4645
} from '../../redux/reducers/slice/roomSlice';
4746
import { codePreviewCooperative } from '../../redux/reducers/slice/codePreviewSlice';
4847
import { cooperativeStyle } from '../../redux/reducers/slice/styleSlice';
@@ -71,13 +70,7 @@ const RoomsContainer = () => {
7170
const userJoined = useSelector(
7271
(store: RootState) => store.roomSlice.userJoined
7372
);
74-
const newMessage = useSelector(
75-
(store: RootState) => store.roomSlice.newMessage
76-
);
77-
78-
const userChange = useSelector(
79-
(store: RootState) => store.roomSlice.userChange
80-
);
73+
const messages = useSelector((store: RootState) => store.roomSlice.messages);
8174

8275
// for websockets - initialize socket connection passing in roomCode
8376
function initSocketConnection(roomCode: string) {
@@ -113,16 +106,14 @@ const RoomsContainer = () => {
113106
socket.on('updateUserList', (messageData) => {
114107
//console.log('user list received from server');
115108
dispatch(setUserList(messageData.userList));
116-
if (
117-
JSON.stringify(messageData.activity) !== JSON.stringify(userChange)
118-
) {
119-
dispatch(setUserChange(messageData.activity));
120-
}
121109
});
122110

123111
socket.on('new chat message', (messageData) => {
124-
if (JSON.stringify(messageData) !== JSON.stringify(newMessage)) {
125-
dispatch(setNewMessage(messageData));
112+
if (
113+
messages.length === 0 ||
114+
JSON.stringify(messageData) !== JSON.stringify(messages[-1])
115+
) {
116+
dispatch(setMessages(messageData));
126117
}
127118
});
128119

app/src/redux/reducers/slice/roomSlice.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const initialState = {
66
userName: '',
77
userList: [],
88
userJoined: false,
9-
newMessage: { nickName: '', message: '' },
10-
userChange: { nickName: '', status: '' }
9+
messages: []
1110
};
1211

1312
// Creates new slice with the name , initial state, and reducer function
@@ -27,11 +26,8 @@ const roomSlice = createSlice({
2726
setUserJoined: (state, action) => {
2827
state.userJoined = action.payload;
2928
},
30-
setNewMessage: (state, action) => {
31-
state.newMessage = action.payload;
32-
},
33-
setUserChange: (state, action) => {
34-
state.userChange = action.payload;
29+
setMessages: (state, action) => {
30+
state.messages = [...state.messages, action.payload];
3531
}
3632
}
3733
});
@@ -42,8 +38,7 @@ export const {
4238
setUserName,
4339
setUserList,
4440
setUserJoined,
45-
setNewMessage,
46-
setUserChange
41+
setMessages
4742
} = roomSlice.actions;
4843
// Exports so we can combine in rootReducer
4944
export default roomSlice.reducer;

server/server.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ io.on('connection', (client) => {
137137
activity: { nickName: userName, status: 'JOIN' }
138138
} // send updated userList to all users in room
139139
);
140+
io.to(roomCode).emit('new chat message', {
141+
userName,
142+
message: `${userName} joined chat room`,
143+
type: 'activity'
144+
});
140145
}
141146
} catch (error) {
142147
//if joining event is having an error and time out
@@ -156,24 +161,31 @@ io.on('connection', (client) => {
156161
//disconnecting functionality
157162
client.on('disconnecting', () => {
158163
const roomCode = Array.from(client.rooms)[1]; //grabbing current room client was in when disconnecting
159-
const nickName = roomLists[roomCode][client.id];
164+
const userName = roomLists[roomCode][client.id];
160165
delete roomLists[roomCode][client.id];
161166
//if room empty, delete room from room list
162167
if (!Object.keys(roomLists[roomCode]).length) {
163168
delete roomLists[roomCode];
164169
} else {
165170
//else emit updated user list
166171
io.to(roomCode).emit('updateUserList', {
167-
userList: Object.values(roomLists[roomCode]),
168-
activity: { nickName, status: 'LEAVE' }
172+
userList: Object.values(roomLists[roomCode])
173+
});
174+
io.to(roomCode).emit('new chat message', {
175+
userName,
176+
message: `${userName} left chat room`,
177+
type: 'activity'
169178
});
170179
}
171180
});
172181

173182
//-------Socket events for state synchronization in collab room------------------
174183
client.on('send-chat-message', (roomCode: string, messageData: object) => {
175184
if (roomCode) {
176-
io.to(roomCode).emit('new chat message', messageData);
185+
io.to(roomCode).emit('new chat message', {
186+
...messageData,
187+
type: 'chat'
188+
});
177189
}
178190
});
179191
client.on('addChildAction', (roomCode: string, childData: object) => {

0 commit comments

Comments
 (0)