From f8e04edb86077c7daf01ef5d03a7fbf67e22eb95 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Sat, 18 Jul 2020 15:03:19 +0200 Subject: [PATCH] infinite loading for search results --- src/components/nav/Results.vue | 10 ++++++-- src/store/modules/search.js | 42 +++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/components/nav/Results.vue b/src/components/nav/Results.vue index ee66886..c4b638d 100644 --- a/src/components/nav/Results.vue +++ b/src/components/nav/Results.vue @@ -16,6 +16,9 @@
  • + @@ -34,7 +37,7 @@ diff --git a/src/store/modules/search.js b/src/store/modules/search.js index dfddeea..f320308 100644 --- a/src/store/modules/search.js +++ b/src/store/modules/search.js @@ -19,6 +19,8 @@ export default { results: [], resultsCount: '', isloading: false, + limit: 10, + offset: 0, opened: false }, @@ -31,11 +33,17 @@ export default { state.keys = keys }, setResults (state, content) { - state.results = content + state.results = state.results.concat(content) + }, + resetResults (state) { + state.results = [] }, setResultsCount (state, quantity) { state.resultsCount = `${quantity.quantity} ${quantity.unit}` }, + incrementOffset (state) { + state.offset += state.limit + }, setIsloading (state, isloading) { state.isloading = isloading }, @@ -89,35 +97,57 @@ export default { // actions actions: { - getResults ({ dispatch, commit, state }) { - console.log('getResults', state.keys) + getResults ({ dispatch, commit, state }, $infiniteLoadingState = null) { + console.log('getResults', state.keys, $infiniteLoadingState) + // reset results on new search + if (!$infiniteLoadingState) { + commit('resetResults') + } commit('setIsloading', true) let params = { search: `${state.keys}`, - start: 1, - count: 30 + start: state.offset, + count: state.limit } if (state.searchTypeValue.code !== 'text') { params.type = state.searchTypeValue.code } + // params.filterPersons = ['nomLouisXIII', 'nomChampagnePhilippeDe'] // if () // console.log('Search getResults params', params); let q = qs.stringify(params) return REST.get(`${window.apipath}/search?` + q) .then(({ data }) => { - console.log('search REST: data', data) + // console.log('search REST: data', data.meta.quantity.quantity, state.offset + state.limit, data) commit('setIsloading', false) commit('setOpened', true) commit('setResults', data.content) commit('setResultsCount', data.meta.quantity) commit('setFilters', data.meta.filters) + if ($infiniteLoadingState) { + if (state.offset + state.limit > data.meta.quantity.quantity) { + console.log('Search infinite completed') + // tell to vue-infinite-loading plugin that there si no new page + $infiniteLoadingState.complete() + } else { + console.log('Search infinite loaded') + // tell to vue-infinite-loading plugin that newpage is loaded + $infiniteLoadingState.loaded() + } + } }) .catch((error) => { console.warn('Issue with search', error) commit('setIsloading', false) + $infiniteLoadingState.error() Promise.reject(error) }) }, + nextResultsBatch ({ dispatch, commit, state }, $infiniteLoadingState) { + console.log('nextResultsBatch', $infiniteLoadingState) + commit('incrementOffset') + dispatch('getResults', $infiniteLoadingState) + }, setSearchTypeValue ({ dispatch, commit, state }, value) { commit('setSearchTypeValue', value) },