-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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`.
- Loading branch information
1 parent
c0b49d1
commit 14f13a4
Showing
5 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
client/src/components/AllConversations/AllConversations.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
client/src/components/AllConversations/AllConversations.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters