listen for the message event from the gateway and display message
This commit is contained in:
parent
f9a5f1698b
commit
f2ef7c5c77
5 changed files with 37 additions and 24 deletions
|
@ -46,9 +46,7 @@ function App({ user, gateway }) {
|
||||||
<Route path="/login">
|
<Route path="/login">
|
||||||
<Login />
|
<Login />
|
||||||
</Route>
|
</Route>
|
||||||
<Route path="/categories/:id">
|
<Route path="/categories/:id" component={ CategoryView } />
|
||||||
<CategoryView />
|
|
||||||
</Route>
|
|
||||||
<Route path="/">
|
<Route path="/">
|
||||||
<Root user={user} />
|
<Root user={user} />
|
||||||
</Route>
|
</Route>
|
||||||
|
|
|
@ -1,12 +1,20 @@
|
||||||
// import CategoryProfile from './CategoryProfile';
|
// import CategoryProfile from './CategoryProfile';
|
||||||
|
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
|
import gatewayConnection from '../../Gateway/globalGatewayConnection';
|
||||||
|
|
||||||
export default function CategoryButton({ category }) {
|
export default function CategoryButton({ category }) {
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
|
const handleClick = () => {
|
||||||
|
if (gatewayConnection.isConnected) {
|
||||||
|
gatewayConnection.subscribeToCategoryChat(category._id);
|
||||||
|
history.push(`/categories/${category._id}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button className="button category-button" onClick={ () => history.push(`/categories/${category._id}`) }>
|
<button className="button category-button" onClick={ handleClick }>
|
||||||
{/*
|
{/*
|
||||||
maybe enable someday...
|
maybe enable someday...
|
||||||
<CategoryProfile category={ category } size="32" />
|
<CategoryProfile category={ category } size="32" />
|
||||||
|
@ -14,5 +22,5 @@ export default function CategoryButton({ category }) {
|
||||||
|
|
||||||
{ category.title }
|
{ category.title }
|
||||||
</button>
|
</button>
|
||||||
)
|
);
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@ import Message from '../Messages/Message';
|
||||||
import { useParams } from 'react-router-dom';
|
import { useParams } from 'react-router-dom';
|
||||||
import { connect } from 'react-redux'
|
import { connect } from 'react-redux'
|
||||||
|
|
||||||
function CategoryView({ categories }) {
|
function CategoryView({ categories, messages }) {
|
||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
|
|
||||||
let category;
|
let category;
|
||||||
|
@ -13,20 +13,7 @@ function CategoryView({ categories }) {
|
||||||
if (categories) {
|
if (categories) {
|
||||||
category = categories.find(x => x._id === id);
|
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) {
|
if (category) {
|
||||||
return (
|
return (
|
||||||
<div className="card main-card main-content-card">
|
<div className="card main-card main-content-card">
|
||||||
|
@ -34,7 +21,7 @@ function CategoryView({ categories }) {
|
||||||
<CategoryProfile category={ category } />
|
<CategoryProfile category={ category } />
|
||||||
</div>
|
</div>
|
||||||
<div className="card message-list-card">
|
<div className="card message-list-card">
|
||||||
<Message message={ dummyMessage1 } />
|
{ messages.map(m => <Message key={ m._id } message={ m } />) }
|
||||||
</div>
|
</div>
|
||||||
<div className="card bar-card-bottom">
|
<div className="card bar-card-bottom">
|
||||||
The hearing of the average cat is at least five times keener than that of a human adult.
|
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 {
|
return {
|
||||||
categories: state?.categories
|
categories: state?.categories,
|
||||||
|
messages: state?.messages[categoryId] || []
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,9 @@ const globalGatewayConnection = new GatewayConnection(config.gatewayUrl);
|
||||||
|
|
||||||
globalGatewayConnection.onConnect = function() {
|
globalGatewayConnection.onConnect = function() {
|
||||||
store.dispatch({ type: 'gateway/connectionstatus', gateway: { isConnected: true } });
|
store.dispatch({ type: 'gateway/connectionstatus', gateway: { isConnected: true } });
|
||||||
|
globalGatewayConnection.socket.on('message', (message) => {
|
||||||
|
store.dispatch({ type: 'messagestore/addmessage', message });
|
||||||
|
});
|
||||||
};
|
};
|
||||||
globalGatewayConnection.onDisconnect = function() {
|
globalGatewayConnection.onDisconnect = function() {
|
||||||
store.dispatch({ type: 'gateway/connectionstatus', gateway: { isConnected: false } });
|
store.dispatch({ type: 'gateway/connectionstatus', gateway: { isConnected: false } });
|
||||||
|
|
|
@ -3,7 +3,8 @@ import { createStore } from 'redux';
|
||||||
const intitialState = {
|
const intitialState = {
|
||||||
user: null,
|
user: null,
|
||||||
categories: null,
|
categories: null,
|
||||||
gateway: { isConnected: false }
|
gateway: { isConnected: false },
|
||||||
|
messages: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
const reducer = (state = intitialState, payload) => {
|
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: {
|
default: {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue