From b321585ee8a03b9c663c16eabdbd9b7c2cc8633b Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Thu, 25 Jul 2019 12:03:35 -0400 Subject: [PATCH] Change 'page' param in DiscussionList when loading more results, properly use existing param --- js/src/forum/components/DiscussionList.js | 34 +++++++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/js/src/forum/components/DiscussionList.js b/js/src/forum/components/DiscussionList.js index 6c065c417..12f5cb492 100644 --- a/js/src/forum/components/DiscussionList.js +++ b/js/src/forum/components/DiscussionList.js @@ -28,6 +28,18 @@ export default class DiscussionList extends Component { */ this.moreResults = false; + /** + * Current page in discussion list + */ + this.page = Number(m.route.param('page')) || 1; + + /** + * Number of discussions to offset for pagination + * + * @type {number} + */ + this.offset = (this.page - 1) * 20; + /** * The discussions in the discussion list. * @@ -140,10 +152,10 @@ export default class DiscussionList extends Component { /** * Load a new page of discussion results. * - * @param {Integer} offset The index to start the page at. + * @param {Number} offset The index to start the page at. * @return {Promise} */ - loadResults(offset) { + loadResults(offset = this.offset) { const preloadedDiscussions = app.preloadedApiDocument(); if (preloadedDiscussions) { @@ -165,7 +177,9 @@ export default class DiscussionList extends Component { loadMore() { this.loading = true; - this.loadResults(this.discussions.length).then(this.parseResults.bind(this)); + this.page++; + + this.loadResults((this.offset += 20)).then(this.parseResults.bind(this)); } /** @@ -180,8 +194,22 @@ export default class DiscussionList extends Component { this.loading = false; this.moreResults = !!results.payload.links.next; + // Construct a URL to this discussion with the updated page, then + // replace it into the window's history and our own history stack. m.lazyRedraw(); + const query = m.route.parseQueryString(document.location.search); + + if (this.page !== query.page) { + if (this.page !== 1) query.page = this.page; + + const url = new URL(document.location.href); + + url.search = m.route.buildQueryString(query); + + window.history.replaceState(null, document.title, url); + } + return results; }