From 7bc5f4e863b14febf26bb447b5267216d9d8ce42 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Wed, 29 Jul 2015 11:30:54 -0400 Subject: reset number of posts to fetch on channel switch and fresh page load --- web/react/components/channel_loader.jsx | 3 ++- web/react/utils/async_client.jsx | 9 ++++++--- web/react/utils/constants.jsx | 1 + web/react/utils/utils.jsx | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/web/react/components/channel_loader.jsx b/web/react/components/channel_loader.jsx index b7cb248db..6b80f6012 100644 --- a/web/react/components/channel_loader.jsx +++ b/web/react/components/channel_loader.jsx @@ -8,6 +8,7 @@ var BrowserStore = require('../stores/browser_store.jsx'); var AsyncClient = require('../utils/async_client.jsx'); var SocketStore = require('../stores/socket_store.jsx'); +var ChannelStore = require('../stores/channel_store.jsx'); var Constants = require('../utils/constants.jsx'); module.exports = React.createClass({ @@ -15,7 +16,7 @@ module.exports = React.createClass({ /* Start initial aysnc loads */ AsyncClient.getMe(); - AsyncClient.getPosts(true); + AsyncClient.getPosts(true, ChannelStore.getCurrentId(), Constants.POST_CHUNK_SIZE); AsyncClient.getChannels(true, true); AsyncClient.getChannelExtraInfo(true); AsyncClient.findTeams(); diff --git a/web/react/utils/async_client.jsx b/web/react/utils/async_client.jsx index 00bd83ed1..8a1ff129d 100644 --- a/web/react/utils/async_client.jsx +++ b/web/react/utils/async_client.jsx @@ -272,17 +272,20 @@ module.exports.search = function(terms) { ); } -module.exports.getPosts = function(force, id) { +module.exports.getPosts = function(force, id, maxPosts) { if (PostStore.getCurrentPosts() == null || force) { var channelId = id ? id : ChannelStore.getCurrentId(); if (isCallInProgress("getPosts_"+channelId)) return; var post_list = PostStore.getCurrentPosts(); + + if (!maxPosts) { maxPosts = Constants.POST_CHUNK_SIZE * Constants.MAX_POST_CHUNKS }; + // if we already have more than POST_CHUNK_SIZE posts, // let's get the amount we have but rounded up to next multiple of POST_CHUNK_SIZE, - // with a max at 180 - var numPosts = post_list && post_list.order.length > 0 ? Math.min(180, Constants.POST_CHUNK_SIZE * Math.ceil(post_list.order.length / Constants.POST_CHUNK_SIZE)) : Constants.POST_CHUNK_SIZE; + // with a max at maxPosts + var numPosts = post_list && post_list.order.length > 0 ? Math.min(maxPosts, Constants.POST_CHUNK_SIZE * Math.ceil(post_list.order.length / Constants.POST_CHUNK_SIZE)) : Constants.POST_CHUNK_SIZE; if (channelId != null) { callTracker["getPosts_"+channelId] = utils.getTimestamp(); diff --git a/web/react/utils/constants.jsx b/web/react/utils/constants.jsx index 77ce19530..4fbcc0341 100644 --- a/web/react/utils/constants.jsx +++ b/web/react/utils/constants.jsx @@ -55,6 +55,7 @@ module.exports = { DEFAULT_CHANNEL: 'town-square', OFFTOPIC_CHANNEL: 'off-topic', POST_CHUNK_SIZE: 60, + MAX_POST_CHUNKS: 3, RESERVED_TEAM_NAMES: [ "www", "web", diff --git a/web/react/utils/utils.jsx b/web/react/utils/utils.jsx index 8a4d92b85..2f9170f8d 100644 --- a/web/react/utils/utils.jsx +++ b/web/react/utils/utils.jsx @@ -720,7 +720,7 @@ module.exports.switchChannel = function(channel, teammate_name) { AsyncClient.getChannels(true, true, true); AsyncClient.getChannelExtraInfo(true); - AsyncClient.getPosts(true, channel.id); + AsyncClient.getPosts(true, channel.id, Constants.POST_CHUNK_SIZE); $('.inner__wrap').removeClass('move--right'); $('.sidebar--left').removeClass('move--right'); -- cgit v1.2.3-1-g7c22 From 0c627913f775044b382f9bc115244e4eb40f4833 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Thu, 30 Jul 2015 08:33:52 -0400 Subject: removed ternary operator to make if statement clearer --- web/react/utils/async_client.jsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/web/react/utils/async_client.jsx b/web/react/utils/async_client.jsx index 8a1ff129d..dc4fc1096 100644 --- a/web/react/utils/async_client.jsx +++ b/web/react/utils/async_client.jsx @@ -285,7 +285,10 @@ module.exports.getPosts = function(force, id, maxPosts) { // if we already have more than POST_CHUNK_SIZE posts, // let's get the amount we have but rounded up to next multiple of POST_CHUNK_SIZE, // with a max at maxPosts - var numPosts = post_list && post_list.order.length > 0 ? Math.min(maxPosts, Constants.POST_CHUNK_SIZE * Math.ceil(post_list.order.length / Constants.POST_CHUNK_SIZE)) : Constants.POST_CHUNK_SIZE; + var numPosts = Math.min(maxPosts, Constants.POST_CHUNK_SIZE); + if (post_list && post_list.order.length > 0) { + numPosts = Math.min(maxPosts, Constants.POST_CHUNK_SIZE * Math.ceil(post_list.order.length / Constants.POST_CHUNK_SIZE)); + } if (channelId != null) { callTracker["getPosts_"+channelId] = utils.getTimestamp(); -- cgit v1.2.3-1-g7c22