From 5194c9828c67acaeae93245857ed8f077d305e42 Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Wed, 27 Jun 2018 16:06:15 -0400 Subject: [PATCH] Add proper pagination and fix small issues with /connect (#503) * Add proper pagination and fix small issues with /connect --- app/javascript/chat/actions.js | 5 ++- app/javascript/chat/channels.jsx | 2 +- app/javascript/chat/chat.jsx | 45 +++++++++++++++++-- .../src/utils/getUnopenedChannels.jsx | 15 +++++-- app/models/message.rb | 1 + 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/app/javascript/chat/actions.js b/app/javascript/chat/actions.js index 6d5121085..2dd5d4bd1 100644 --- a/app/javascript/chat/actions.js +++ b/app/javascript/chat/actions.js @@ -72,11 +72,12 @@ export function conductModeration( .catch(failureCb); } -export function getChannels(query,retrievalID, props, successCb, failureCb) { +export function getChannels(query,retrievalID, props, paginationNumber, successCb, failureCb) { const client = algoliasearch(props.algoliaId, props.algoliaKey); const index = client.initIndex(props.algoliaIndex); index.search(query,{ - hitsPerPage: 30, + hitsPerPage: 24, + page: paginationNumber }) .then(function(content) { let channels = content.hits diff --git a/app/javascript/chat/channels.jsx b/app/javascript/chat/channels.jsx index b5b407131..3d69cf72c 100644 --- a/app/javascript/chat/channels.jsx +++ b/app/javascript/chat/channels.jsx @@ -112,7 +112,7 @@ const Channels = ({ activeChannelId, } return (
-
+
{topNotice} {channels} {channelsListFooter} diff --git a/app/javascript/chat/chat.jsx b/app/javascript/chat/chat.jsx index a2e37d3f5..ab770945c 100644 --- a/app/javascript/chat/chat.jsx +++ b/app/javascript/chat/chat.jsx @@ -29,6 +29,8 @@ export default class Chat extends Component { chatChannels, filterQuery: '', channelsLoaded: false, + channelPaginationNum: 0, + fetchingPaginatedChannels: false, activeChannelId: chatOptions.activeChannelId, activeChannel: null, showChannelsList: chatOptions.showChannelsList, @@ -65,13 +67,13 @@ export default class Chat extends Component { notificationsPermission: getNotificationState(), }); if (this.state.showChannelsList) { - getChannels('', this.state.activeChannelId, this.props, this.loadChannels); + getChannels('', this.state.activeChannelId, this.props, this.state.channelPaginationNum, this.loadChannels); } if (!this.state.isMobileDevice) { document.getElementById("messageform").focus(); } + document.getElementById('chatchannels__channelslist').addEventListener('scroll', this.handleChannelScroll); } - componentDidUpdate() { if (!this.state.scrolled) { scrollToBottom(); @@ -134,6 +136,7 @@ export default class Chat extends Component { chatChannels: channels, scrolled: false, channelsLoaded: true, + channelPaginationNum: 0, activeChannel: this.filterForActiveChannel(channels, this.state.activeChannelId) }); } if (this.state.activeChannelId) { @@ -142,12 +145,14 @@ export default class Chat extends Component { scrolled: false, chatChannels: channels, channelsLoaded: true, + channelPaginationNum: 0, filterQuery: query }); } else if (channels.length > 0) { this.setState({ chatChannels: channels, channelsLoaded: true, + channelPaginationNum: 0, scrolled: false, }); const channel = channels[0] @@ -168,6 +173,23 @@ export default class Chat extends Component { }); } + loadPaginatedChannels = (channels) => { + const currentChannelIds = this.state.chatChannels.map((channel, index) => { + return channel.id + }) + let newChannels = this.state.chatChannels + channels.forEach((channel, index) => { + if (!currentChannelIds.includes(channel.id)) { + newChannels.push(channel) + } + }); + this.setState({ + chatChannels: newChannels, + fetchingPaginatedChannels: false, + channelPaginationNum: this.state.channelPaginationNum + 1 + }) + } + setupChannel = channelId => { if (!this.state.messages[channelId] || this.state.messages[channelId].length === 0 || this.state.messages[channelId][0].reception_method === 'pushed'){ @@ -268,6 +290,23 @@ export default class Chat extends Component { this.setState({ messages: newMessages }); }; + handleChannelScroll = e => { + if (this.state.fetchingPaginatedChannels || this.state.chatChannels.length < 24) { + return + } + this.setState({fetchingPaginatedChannels: true}) + const target = e.target; + if((target.scrollTop + target.offsetHeight + 500) > target.scrollHeight) + { + getChannels( + this.state.filterQuery, + this.state.activeChannelId, + this.props, + this.state.channelPaginationNum, + this.loadPaginatedChannels); + } + } + handleKeyDown = e => { const enterPressed = e.keyCode === 13; const targetValue = e.target.value; @@ -464,7 +503,7 @@ export default class Chat extends Component { }; triggerChannelFilter = e => { - getChannels(e.target.value, null, this.props, this.loadChannels); + getChannels(e.target.value, null, this.props, 0, this.loadChannels); } toggleExpand = () => { diff --git a/app/javascript/src/utils/getUnopenedChannels.jsx b/app/javascript/src/utils/getUnopenedChannels.jsx index ce0a84675..0dda7d4d3 100644 --- a/app/javascript/src/utils/getUnopenedChannels.jsx +++ b/app/javascript/src/utils/getUnopenedChannels.jsx @@ -27,7 +27,6 @@ class UnopenedChannelNotice extends Component { } receiveNewMessage = e => { - console.log(location.pathname) if (location.pathname.startsWith("/connect")) { return } @@ -37,12 +36,20 @@ class UnopenedChannelNotice extends Component { newObj.adjusted_slug != `@${window.currentUser.username}`) { channels.push(newObj); } - this.setState({visible: channels.length > 0, unopenedChannels: channels}) + this.setState({ + visible: (channels.length > 0 && e.user_id != window.currentUser.id), + unopenedChannels: channels + }) const number = document.getElementById("connect-number") number.classList.add("showing") number.innerHTML = channels.length const component = this; + if (channels.length === 0) { + number.classList.remove("showing") + } else { + document.getElementById("connect-link").href = `/connect/${channels[0].adjusted_slug}` + } setTimeout(function(){ component.setState({visible: false}); }, 7500) @@ -102,10 +109,12 @@ export default function getUnopenedChannels(user, successCb) { }) .then(response => response.json()) .then(json => { + const number = document.getElementById("connect-number") if (json.length > 0) { - const number = document.getElementById("connect-number") number.classList.add("showing") number.innerHTML = json.length + } else { + number.classList.remove("showing") } }) .catch(error => { diff --git a/app/models/message.rb b/app/models/message.rb index 9aee16f8f..175d105d6 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -45,6 +45,7 @@ class Message < ApplicationRecord def update_chat_channel_last_message_at chat_channel.touch(:last_message_at) chat_channel.index! + chat_channel.delay.index! end def update_all_has_unopened_messages_statuses