Skip to content

Commit

Permalink
Add new message notification for allconversations screen
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
vishwamartur committed Nov 12, 2024
1 parent c0b49d1 commit 14f13a4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';

import Chat from './components/Chat/Chat';
import Join from './components/Join/Join';
import AllConversations from './components/AllConversations/AllConversations';

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

Expand All @@ -10,6 +11,7 @@ const App = () => {
<Router>
<Route path="/" exact component={Join} />
<Route path="/chat" component={Chat} />
<Route path="/allconversations" component={AllConversations} />
</Router>
);
}
Expand Down
14 changes: 14 additions & 0 deletions client/src/components/AllConversations/AllConversations.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.allConversations {
display: flex;
flex-direction: column;
padding: 10px;
}

.conversation {
padding: 10px;
border-bottom: 1px solid #ccc;
}

.conversation.bold {
font-weight: bold;
}
47 changes: 47 additions & 0 deletions client/src/components/AllConversations/AllConversations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { useState, useEffect } from 'react';
import io from 'socket.io-client';

import './AllConversations.css';

const ENDPOINT = 'https://project-chat-application.herokuapp.com/';

let socket;

const AllConversations = () => {
const [conversations, setConversations] = useState([]);

useEffect(() => {
socket = io(ENDPOINT);

socket.on('newMessage', ({ conversationId, message }) => {
setConversations(conversations => {
const updatedConversations = conversations.map(conversation => {
if (conversation.id === conversationId) {
return { ...conversation, hasNewMessage: true };
}
return conversation;
});
return updatedConversations;
});
});

return () => {
socket.disconnect();
};
}, []);

return (
<div className="allConversations">
{conversations.map(conversation => (
<div
key={conversation.id}
className={`conversation ${conversation.hasNewMessage ? 'bold' : ''}`}
>
{conversation.name}
</div>
))}
</div>
);
};

export default AllConversations;
6 changes: 6 additions & 0 deletions client/src/components/Chat/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ const Chat = ({ location }) => {
}
}

useEffect(() => {
socket.on('newMessage', ({ conversationId, message }) => {
// Handle new message event
});
}, []);

return (
<div className="outerContainer">
<div className="container">
Expand Down
5 changes: 4 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ io.on('connect', (socket) => {

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

// Notify allconversations screen of new message
socket.broadcast.emit('newMessage', { conversationId: user.room, message });

callback();
});

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

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

0 comments on commit 14f13a4

Please sign in to comment.