infinite loading for search results

This commit is contained in:
2020-07-18 15:03:19 +02:00
parent fb4cb676ac
commit f8e04edb86
2 changed files with 44 additions and 8 deletions
+8 -2
View File
@@ -16,6 +16,9 @@
<li v-for="result in results" :key="result.uuid" class="result"> <li v-for="result in results" :key="result.uuid" class="result">
<ResultItem :result="result" /> <ResultItem :result="result" />
</li> </li>
<infinite-loading
@infinite="nextResultsBatch"
/>
</ul> </ul>
</div> </div>
</section> </section>
@@ -34,7 +37,7 @@
<script> <script>
import ResultItem from '../Content/ResultItem' import ResultItem from '../Content/ResultItem'
import { mapState } from 'vuex' import { mapState, mapActions } from 'vuex'
export default { export default {
name: 'Results', name: 'Results',
@@ -56,7 +59,10 @@ export default {
close () { close () {
console.log('clicked on close results') console.log('clicked on close results')
this.opened = false this.opened = false
} },
...mapActions({
nextResultsBatch: 'Search/nextResultsBatch'
})
} }
} }
</script> </script>
+36 -6
View File
@@ -19,6 +19,8 @@ export default {
results: [], results: [],
resultsCount: '', resultsCount: '',
isloading: false, isloading: false,
limit: 10,
offset: 0,
opened: false opened: false
}, },
@@ -31,11 +33,17 @@ export default {
state.keys = keys state.keys = keys
}, },
setResults (state, content) { setResults (state, content) {
state.results = content state.results = state.results.concat(content)
},
resetResults (state) {
state.results = []
}, },
setResultsCount (state, quantity) { setResultsCount (state, quantity) {
state.resultsCount = `${quantity.quantity} ${quantity.unit}` state.resultsCount = `${quantity.quantity} ${quantity.unit}`
}, },
incrementOffset (state) {
state.offset += state.limit
},
setIsloading (state, isloading) { setIsloading (state, isloading) {
state.isloading = isloading state.isloading = isloading
}, },
@@ -89,35 +97,57 @@ export default {
// actions // actions
actions: { actions: {
getResults ({ dispatch, commit, state }) { getResults ({ dispatch, commit, state }, $infiniteLoadingState = null) {
console.log('getResults', state.keys) console.log('getResults', state.keys, $infiniteLoadingState)
// reset results on new search
if (!$infiniteLoadingState) {
commit('resetResults')
}
commit('setIsloading', true) commit('setIsloading', true)
let params = { let params = {
search: `${state.keys}`, search: `${state.keys}`,
start: 1, start: state.offset,
count: 30 count: state.limit
} }
if (state.searchTypeValue.code !== 'text') { if (state.searchTypeValue.code !== 'text') {
params.type = state.searchTypeValue.code params.type = state.searchTypeValue.code
} }
// params.filterPersons = ['nomLouisXIII', 'nomChampagnePhilippeDe']
// if () // if ()
// console.log('Search getResults params', params); // console.log('Search getResults params', params);
let q = qs.stringify(params) let q = qs.stringify(params)
return REST.get(`${window.apipath}/search?` + q) return REST.get(`${window.apipath}/search?` + q)
.then(({ data }) => { .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('setIsloading', false)
commit('setOpened', true) commit('setOpened', true)
commit('setResults', data.content) commit('setResults', data.content)
commit('setResultsCount', data.meta.quantity) commit('setResultsCount', data.meta.quantity)
commit('setFilters', data.meta.filters) 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) => { .catch((error) => {
console.warn('Issue with search', error) console.warn('Issue with search', error)
commit('setIsloading', false) commit('setIsloading', false)
$infiniteLoadingState.error()
Promise.reject(error) Promise.reject(error)
}) })
}, },
nextResultsBatch ({ dispatch, commit, state }, $infiniteLoadingState) {
console.log('nextResultsBatch', $infiniteLoadingState)
commit('incrementOffset')
dispatch('getResults', $infiniteLoadingState)
},
setSearchTypeValue ({ dispatch, commit, state }, value) { setSearchTypeValue ({ dispatch, commit, state }, value) {
commit('setSearchTypeValue', value) commit('setSearchTypeValue', value)
}, },