From f2ef7c5c779fb4722076f89583841a1bcd07f8b5 Mon Sep 17 00:00:00 2001 From: hippoz Date: Fri, 8 Jan 2021 22:48:16 +0200 Subject: [PATCH] listen for the message event from the gateway and display message --- bfrontend/src/Components/App.js | 4 +-- .../Components/Categories/CategoryButton.js | 12 +++++++-- .../src/Components/Categories/CategoryView.js | 26 ++++++------------- .../src/Gateway/globalGatewayConnection.js | 3 +++ bfrontend/src/store.js | 16 +++++++++++- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/bfrontend/src/Components/App.js b/bfrontend/src/Components/App.js index 0c80063..907b900 100644 --- a/bfrontend/src/Components/App.js +++ b/bfrontend/src/Components/App.js @@ -46,9 +46,7 @@ function App({ user, gateway }) { - - - + diff --git a/bfrontend/src/Components/Categories/CategoryButton.js b/bfrontend/src/Components/Categories/CategoryButton.js index fca373b..859fdab 100644 --- a/bfrontend/src/Components/Categories/CategoryButton.js +++ b/bfrontend/src/Components/Categories/CategoryButton.js @@ -1,12 +1,20 @@ // import CategoryProfile from './CategoryProfile'; import { useHistory } from 'react-router-dom'; +import gatewayConnection from '../../Gateway/globalGatewayConnection'; export default function CategoryButton({ category }) { const history = useHistory(); + const handleClick = () => { + if (gatewayConnection.isConnected) { + gatewayConnection.subscribeToCategoryChat(category._id); + history.push(`/categories/${category._id}`); + } + }; + return ( - - ) + ); } \ No newline at end of file diff --git a/bfrontend/src/Components/Categories/CategoryView.js b/bfrontend/src/Components/Categories/CategoryView.js index 2504afa..968d2ad 100644 --- a/bfrontend/src/Components/Categories/CategoryView.js +++ b/bfrontend/src/Components/Categories/CategoryView.js @@ -5,7 +5,7 @@ import Message from '../Messages/Message'; import { useParams } from 'react-router-dom'; import { connect } from 'react-redux' -function CategoryView({ categories }) { +function CategoryView({ categories, messages }) { const { id } = useParams(); let category; @@ -13,20 +13,7 @@ function CategoryView({ categories }) { if (categories) { category = categories.find(x => x._id === id); } - - const dummyMessage1 = { - author: { - username: '__SYSTEM', - _id: '5fc69864f15a7c5e504c9a1f' - }, - category: { - title: 'alan', - _id: 'y8fyerghgh' - }, - content: 'yes hello', - _id: 'i8ru354y89ty549ytg' - }; - + if (category) { return (
@@ -34,7 +21,7 @@ function CategoryView({ categories }) {
- + { messages.map(m => ) }
The hearing of the average cat is at least five times keener than that of a human adult. @@ -60,9 +47,12 @@ function CategoryView({ categories }) { } -const stateToProps = (state) => { +const stateToProps = (state, ownProps) => { + const categoryId = ownProps.match.params.id; // NOTE(hippoz): kind of a hack, but it works and idk if theres any other solution + return { - categories: state?.categories + categories: state?.categories, + messages: state?.messages[categoryId] || [] }; }; diff --git a/bfrontend/src/Gateway/globalGatewayConnection.js b/bfrontend/src/Gateway/globalGatewayConnection.js index f63b9bc..e99ebce 100644 --- a/bfrontend/src/Gateway/globalGatewayConnection.js +++ b/bfrontend/src/Gateway/globalGatewayConnection.js @@ -6,6 +6,9 @@ const globalGatewayConnection = new GatewayConnection(config.gatewayUrl); globalGatewayConnection.onConnect = function() { store.dispatch({ type: 'gateway/connectionstatus', gateway: { isConnected: true } }); + globalGatewayConnection.socket.on('message', (message) => { + store.dispatch({ type: 'messagestore/addmessage', message }); + }); }; globalGatewayConnection.onDisconnect = function() { store.dispatch({ type: 'gateway/connectionstatus', gateway: { isConnected: false } }); diff --git a/bfrontend/src/store.js b/bfrontend/src/store.js index cd56a63..8d865dd 100644 --- a/bfrontend/src/store.js +++ b/bfrontend/src/store.js @@ -3,7 +3,8 @@ import { createStore } from 'redux'; const intitialState = { user: null, categories: null, - gateway: { isConnected: false } + gateway: { isConnected: false }, + messages: {} }; const reducer = (state = intitialState, payload) => { @@ -31,6 +32,19 @@ const reducer = (state = intitialState, payload) => { } } + case 'messagestore/addmessage': { + return { + ...state, + messages: { + ...state.messages, + [payload.message.category._id]: [ + ...state.messages[payload.message.category._id] || [], + payload.message + ] + } + } + } + default: { return state; }