diff --git a/client/src/App.js b/client/src/App.js index 1e00486e..fffd8300 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -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"; @@ -10,6 +11,7 @@ const App = () => { + ); } diff --git a/client/src/components/AllConversations/AllConversations.css b/client/src/components/AllConversations/AllConversations.css new file mode 100644 index 00000000..63b7fe9a --- /dev/null +++ b/client/src/components/AllConversations/AllConversations.css @@ -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; +} diff --git a/client/src/components/AllConversations/AllConversations.js b/client/src/components/AllConversations/AllConversations.js new file mode 100644 index 00000000..1bbcbaec --- /dev/null +++ b/client/src/components/AllConversations/AllConversations.js @@ -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 ( +
+ {conversations.map(conversation => ( +
+ {conversation.name} +
+ ))} +
+ ); +}; + +export default AllConversations; diff --git a/client/src/components/Chat/Chat.js b/client/src/components/Chat/Chat.js index f6de8e3a..2714404b 100644 --- a/client/src/components/Chat/Chat.js +++ b/client/src/components/Chat/Chat.js @@ -53,6 +53,12 @@ const Chat = ({ location }) => { } } + useEffect(() => { + socket.on('newMessage', ({ conversationId, message }) => { + // Handle new message event + }); + }, []); + return (
diff --git a/server/index.js b/server/index.js index 42e02bd7..25804c5e 100644 --- a/server/index.js +++ b/server/index.js @@ -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(); }); @@ -48,4 +51,4 @@ io.on('connect', (socket) => { }) }); -server.listen(process.env.PORT || 5000, () => console.log(`Server has started.`)); \ No newline at end of file +server.listen(process.env.PORT || 5000, () => console.log(`Server has started.`));