subscribe to categories only once, when the category is loaded. also fixed the shadow on the main page

This commit is contained in:
hippoz 2021-01-16 17:41:46 +02:00
parent b638422e35
commit 5e2f4fd7c3
No known key found for this signature in database
GPG key ID: 7C52899193467641
5 changed files with 33 additions and 8 deletions

View file

@ -43,10 +43,9 @@ function App({ user, gateway }) {
<BrowserRouter> <BrowserRouter>
{ user && <Sidebar /> } { user && <Sidebar /> }
<Switch> <Switch>
<Route path="/login"> <Route path="/login" component={ Login } />
<Login />
</Route>
<Route path="/categories/:id" component={ CategoryView } /> <Route path="/categories/:id" component={ CategoryView } />
<Route path="/"> <Route path="/">
<Root user={user} /> <Root user={user} />
</Route> </Route>

View file

@ -8,7 +8,6 @@ export default function CategoryButton({ category }) {
const handleClick = () => { const handleClick = () => {
if (gatewayConnection.isConnected) { if (gatewayConnection.isConnected) {
gatewayConnection.subscribeToCategoryChat(category._id);
history.push(`/categories/${category._id}`); history.push(`/categories/${category._id}`);
} }
}; };

View file

@ -4,14 +4,15 @@ import Message from '../Messages/Message';
import gatewayConnection from '../../Gateway/globalGatewayConnection'; import gatewayConnection from '../../Gateway/globalGatewayConnection';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { connect } from 'react-redux'; import { connect, useDispatch } from 'react-redux';
import { useState, useRef, useEffect } from 'react'; import { useState, useRef, useEffect } from 'react';
function CategoryView({ categories, messages }) { function CategoryView({ categories, messages, isHandlingRoute=true, subscribedToCategories, gateway: { isConnected: isGatewayConnected } }) {
const { id } = useParams(); const { id } = useParams();
const [ textInput, setTextInput ] = useState(''); const [ textInput, setTextInput ] = useState('');
const textInputRef = useRef(null); const textInputRef = useRef(null);
const invisibleBottomMessageRef = useRef(null); const invisibleBottomMessageRef = useRef(null);
const dispatch = useDispatch();
let category; let category;
@ -32,6 +33,17 @@ function CategoryView({ categories, messages }) {
useEffect(scrollToBottom, [messages]); useEffect(scrollToBottom, [messages]);
useEffect(() => {
if (isHandlingRoute) {
if (!id || id === '') return;
if (!isGatewayConnected ) return;
if (subscribedToCategories.includes(id)) return;
dispatch({ type: 'categories/selectcategory', categoryId: id });
gatewayConnection.subscribeToCategoryChat(id);
}
}, [dispatch, id, isHandlingRoute, subscribedToCategories, isGatewayConnected]);
if (categories) { if (categories) {
category = categories.find(x => x._id === id); category = categories.find(x => x._id === id);
} }
@ -74,7 +86,9 @@ const stateToProps = (state, ownProps) => {
return { return {
categories: state?.categories, categories: state?.categories,
messages: state?.messages[categoryId] || [] messages: state?.messages[categoryId] || [],
subscribedToCategories: state?.subscribedToCategories || [],
gateway: state?.gateway
}; };
}; };

View file

@ -112,6 +112,8 @@ body {
.main-card { .main-card {
@include card; @include card;
box-shadow: none;
&.main-content-card { &.main-content-card {
flex-grow: 1; flex-grow: 1;
display: flex; display: flex;

View file

@ -4,7 +4,8 @@ const intitialState = {
user: null, user: null,
categories: null, categories: null,
gateway: { isConnected: false }, gateway: { isConnected: false },
messages: {} messages: {},
subscribedToCategories: []
}; };
const reducer = (state = intitialState, payload) => { const reducer = (state = intitialState, payload) => {
@ -45,6 +46,16 @@ const reducer = (state = intitialState, payload) => {
} }
} }
case 'categories/selectcategory': {
return {
...state,
subscribedToCategories: [
...state.subscribedToCategories || [],
payload.categoryId
]
}
}
default: { default: {
return state; return state;
} }