From 323a326c73f4aabf37bf79f8e42350c128983c2d Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Tue, 6 Jun 2017 00:06:08 -0500 Subject: [PATCH] Improve pagination when searching or filtering [ci skip] --- app/assets/javascripts/filterable_list.js | 62 ++++++++++++------- .../javascripts/groups/components/groups.vue | 12 ++-- .../groups/groups_filterable_list.js | 57 +++++++++++++---- app/assets/javascripts/groups/index.js | 35 +++++++++-- .../groups/services/groups_service.js | 6 +- .../javascripts/lib/utils/common_utils.js | 4 +- app/views/dashboard/groups/_groups.html.haml | 2 +- 7 files changed, 130 insertions(+), 48 deletions(-) diff --git a/app/assets/javascripts/filterable_list.js b/app/assets/javascripts/filterable_list.js index 17c39cc7bbb..139206cc185 100644 --- a/app/assets/javascripts/filterable_list.js +++ b/app/assets/javascripts/filterable_list.js @@ -8,7 +8,15 @@ export default class FilterableList { this.filterForm = form; this.listFilterElement = filter; this.listHolderElement = holder; - this.filterUrl = `${this.filterForm.getAttribute('action')}?${$(this.filterForm).serialize()}`; + this.isBusy = false; + } + + getFilterEndpoint() { + return `${this.filterForm.getAttribute('action')}?${$(this.filterForm).serialize()}`; + } + + getPagePath() { + return this.getFilterEndpoint(); } initSearch() { @@ -20,9 +28,19 @@ export default class FilterableList { } onFilterInput() { - const url = this.filterForm.getAttribute('action'); - const data = $(this.filterForm).serialize(); - this.filterResults(url, data, 'filter-input'); + const $form = $(this.filterForm); + const queryData = {}; + const filterGroupsParam = $form.find('[name="filter_groups"]').val(); + + if (filterGroupsParam) { + queryData.filter_groups = filterGroupsParam; + } + + this.filterResults(queryData); + + if (this.setDefaultFilterOption) { + this.setDefaultFilterOption(); + } } bindEvents() { @@ -33,42 +51,44 @@ export default class FilterableList { this.listFilterElement.removeEventListener('input', this.debounceFilter); } - filterResults(url, data, comingFrom) { - const endpoint = url || this.filterForm.getAttribute('action'); - const additionalData = data || $(this.filterForm).serialize(); + filterResults(queryData) { + if (this.isBusy) { + return false; + } $(this.listHolderElement).fadeTo(250, 0.5); return $.ajax({ - url: endpoint, - data: additionalData, + url: this.getFilterEndpoint(), + data: queryData, type: 'GET', dataType: 'json', context: this, complete: this.onFilterComplete, + beforeSend: () => { + this.isBusy = true; + }, success: (response, textStatus, xhr) => { - if (this.preOnFilterSuccess) { - this.preOnFilterSuccess(comingFrom); - } - - this.onFilterSuccess(response, xhr); + this.onFilterSuccess(response, xhr, queryData); }, }); } - onFilterSuccess(data) { - if (data.html) { - this.listHolderElement.innerHTML = data.html; + onFilterSuccess(response, xhr, queryData) { + if (response.html) { + this.listHolderElement.innerHTML = response.html; } - // Change url so if user reload a page - search results are saved - return window.history.replaceState({ - page: this.filterUrl, + // Change url so if user reload a page - search results are saved + const currentPath = this.getPagePath(queryData); - }, document.title, this.filterUrl); + return window.history.replaceState({ + page: currentPath, + }, document.title, currentPath); } onFilterComplete() { + this.isBusy = false; $(this.listHolderElement).fadeTo(250, 1); } } diff --git a/app/assets/javascripts/groups/components/groups.vue b/app/assets/javascripts/groups/components/groups.vue index 6ddf54275d9..759b68c8bb4 100644 --- a/app/assets/javascripts/groups/components/groups.vue +++ b/app/assets/javascripts/groups/components/groups.vue @@ -1,5 +1,6 @@