Skip to content

Commit 14f13a4

Browse files
committed
Add new message notification for allconversations screen
Related to #34 Add a feature to notify user 1 of new messages when they are not inside the chat. * Create a new React component `AllConversations` to display all conversations and bold the ones with new messages. * Add styles for the `AllConversations` component in `AllConversations.css`. * Import the `AllConversations` component and add a route for it in `App.js`. * Add a new socket event in `server/index.js` to notify the allconversations screen of new messages. * Emit a new socket event when a new message is received in `Chat.js`.
1 parent c0b49d1 commit 14f13a4

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

client/src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22

33
import Chat from './components/Chat/Chat';
44
import Join from './components/Join/Join';
5+
import AllConversations from './components/AllConversations/AllConversations';
56

67
import { BrowserRouter as Router, Route } from "react-router-dom";
78

@@ -10,6 +11,7 @@ const App = () => {
1011
<Router>
1112
<Route path="/" exact component={Join} />
1213
<Route path="/chat" component={Chat} />
14+
<Route path="/allconversations" component={AllConversations} />
1315
</Router>
1416
);
1517
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.allConversations {
2+
display: flex;
3+
flex-direction: column;
4+
padding: 10px;
5+
}
6+
7+
.conversation {
8+
padding: 10px;
9+
border-bottom: 1px solid #ccc;
10+
}
11+
12+
.conversation.bold {
13+
font-weight: bold;
14+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React, { useState, useEffect } from 'react';
2+
import io from 'socket.io-client';
3+
4+
import './AllConversations.css';
5+
6+
const ENDPOINT = 'https://project-chat-application.herokuapp.com/';
7+
8+
let socket;
9+
10+
const AllConversations = () => {
11+
const [conversations, setConversations] = useState([]);
12+
13+
useEffect(() => {
14+
socket = io(ENDPOINT);
15+
16+
socket.on('newMessage', ({ conversationId, message }) => {
17+
setConversations(conversations => {
18+
const updatedConversations = conversations.map(conversation => {
19+
if (conversation.id === conversationId) {
20+
return { ...conversation, hasNewMessage: true };
21+
}
22+
return conversation;
23+
});
24+
return updatedConversations;
25+
});
26+
});
27+
28+
return () => {
29+
socket.disconnect();
30+
};
31+
}, []);
32+
33+
return (
34+
<div className="allConversations">
35+
{conversations.map(conversation => (
36+
<div
37+
key={conversation.id}
38+
className={`conversation ${conversation.hasNewMessage ? 'bold' : ''}`}
39+
>
40+
{conversation.name}
41+
</div>
42+
))}
43+
</div>
44+
);
45+
};
46+
47+
export default AllConversations;

client/src/components/Chat/Chat.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ const Chat = ({ location }) => {
5353
}
5454
}
5555

56+
useEffect(() => {
57+
socket.on('newMessage', ({ conversationId, message }) => {
58+
// Handle new message event
59+
});
60+
}, []);
61+
5662
return (
5763
<div className="outerContainer">
5864
<div className="container">

server/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ io.on('connect', (socket) => {
3535

3636
io.to(user.room).emit('message', { user: user.name, text: message });
3737

38+
// Notify allconversations screen of new message
39+
socket.broadcast.emit('newMessage', { conversationId: user.room, message });
40+
3841
callback();
3942
});
4043

@@ -48,4 +51,4 @@ io.on('connect', (socket) => {
4851
})
4952
});
5053

51-
server.listen(process.env.PORT || 5000, () => console.log(`Server has started.`));
54+
server.listen(process.env.PORT || 5000, () => console.log(`Server has started.`));

0 commit comments

Comments
 (0)