Autoload results after clicking loadmore, also enabled loadLess again

This commit is contained in:
2022-03-05 12:52:08 +01:00
parent bf44668a12
commit c180bdf98a

View File

@@ -1,5 +1,5 @@
<template> <template>
<div> <div ref="resultSection">
<list-header v-bind="{ title, info, shortList }" :sticky="true" /> <list-header v-bind="{ title, info, shortList }" :sticky="true" />
<div <div
@@ -14,8 +14,8 @@
<results-list v-bind="{ results, shortList }" /> <results-list v-bind="{ results, shortList }" />
<loader v-if="loading" /> <loader v-if="loading" />
<div v-if="!shortList && page < totalPages" class="load-button"> <div v-if="!shortList && page != totalPages" class="load-button">
<seasoned-button @click="loadMore" :fullWidth="true" <seasoned-button ref="loadMoreButton" @click="loadMore" :fullWidth="true"
>load more</seasoned-button >load more</seasoned-button
> >
</div> </div>
@@ -52,9 +52,11 @@ export default {
results: [], results: [],
page: 1, page: 1,
loadedPages: [], loadedPages: [],
totalPages: 0, totalPages: -1,
totalResults: 0, totalResults: 0,
loading: true loading: true,
autoLoad: false,
observer: undefined
}; };
}, },
computed: { computed: {
@@ -72,10 +74,11 @@ export default {
} }
}, },
methods: { methods: {
prettify(listName) {
return listName.includes("_") ? listName.split("_").join(" ") : listName;
},
loadMore() { loadMore() {
if (!this.autoLoad) {
this.autoLoad = true;
}
this.loading = true; this.loading = true;
let maxPage = [...this.loadedPages].slice(-1)[0]; let maxPage = [...this.loadedPages].slice(-1)[0];
@@ -115,7 +118,8 @@ export default {
getListResults(front = false) { getListResults(front = false) {
this.apiFunction(this.page) this.apiFunction(this.page)
.then(results => { .then(results => {
this.results = this.results.concat(...results.results); if (!front) this.results = this.results.concat(...results.results);
else this.results = results.results.concat(...this.results);
this.page = results.page; this.page = results.page;
this.loadedPages.push(this.page); this.loadedPages.push(this.page);
this.loadedPages = this.loadedPages.sort(); this.loadedPages = this.loadedPages.sort();
@@ -124,18 +128,38 @@ export default {
}) })
.then(this.updateQueryParams) .then(this.updateQueryParams)
.finally(() => (this.loading = false)); .finally(() => (this.loading = false));
},
setupAutoloadObserver() {
this.observer = new IntersectionObserver(this.handleButtonIntersection, {
root: this.$refs.resultSection.$el,
rootMargin: "0px",
threshold: 1.0
});
this.observer.observe(this.$refs.loadMoreButton.$el);
},
handleButtonIntersection(entries) {
entries.map(entry =>
entry.isIntersecting && this.autoLoad ? this.loadMore() : null
);
} }
}, },
created() { created() {
this.page = this.getPageFromUrl() || this.page; this.page = this.getPageFromUrl() || this.page;
// this.listName = this.getListNameFromUrl();
if (this.results.length === 0) this.getListResults(); if (this.results.length === 0) this.getListResults();
store.dispatch( store.dispatch(
"documentTitle/updateTitle", "documentTitle/updateTitle",
`${this.$router.history.current.name} ${this.$route.params.name}` `${this.$router.history.current.name} ${this.$route.params.name}`
); );
},
mounted() {
if (!this.shortList) {
this.setupAutoloadObserver();
}
},
beforeDestroy() {
this.observer = undefined;
} }
}; };
</script> </script>