Skip to content

Commit

Permalink
Improved: the logic for fixing infinite scroll(#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
R-Sourabh committed Apr 17, 2024
1 parent 4665303 commit 714054b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
27 changes: 21 additions & 6 deletions src/components/ProductFilterModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ion-toolbar>
</ion-header>

<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<ion-searchbar :placeholder="$t(`Search ${label}`)" v-model="queryString" @keyup.enter="queryString = $event.target.value; search($event)"/>

<ion-list>
Expand All @@ -29,7 +29,7 @@
<ion-icon :icon="checkmarkOutline" />
</ion-fab-button>
</ion-fab>
<ion-infinite-scroll @ionInfinite="loadMoreTags($event)" threshold="100px" :disabled="!isScrollable" :key="queryString">
<ion-infinite-scroll @ionInfinite="loadMoreTags($event)" threshold="100px" v-show="isScrollingEnabled && isScrollable" ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="$t('Loading')"/>
</ion-infinite-scroll>
</ion-content>
Expand Down Expand Up @@ -88,7 +88,8 @@ export default defineComponent({
facetOptions: [] as any,
isFilterChanged: false,
isScrollable: true,
selectedValues: [] as Array<string>
selectedValues: [] as Array<string>,
isScrollingEnabled: false
}
},
computed: {
Expand All @@ -100,6 +101,9 @@ export default defineComponent({
mounted() {
this.selectedValues = JSON.parse(JSON.stringify(this.appliedFilters[this.type][this.searchfield])).list;
},
async ionViewWillEnter() {
this.isScrollingEnabled = false;
},
methods: {
closeModal() {
modalController.dismiss({ dismissed: true, isFilterChanged: this.isFilterChanged });
Expand Down Expand Up @@ -134,13 +138,24 @@ export default defineComponent({
this.isScrollable = false;
}
},
enableScrolling() {
const parentElement = (this as any).$refs.contentRef.$el
const scrollEl = parentElement.shadowRoot.querySelector("main[part='scroll']")
let scrollHeight = scrollEl.scrollHeight, infiniteHeight = (this as any).$refs.infiniteScrollRef.$el.offsetHeight, scrollTop = scrollEl.scrollTop, threshold = 100, height = scrollEl.offsetHeight
const distanceFromInfinite = scrollHeight - infiniteHeight - scrollTop - threshold - height
if(distanceFromInfinite < 0) {
this.isScrollingEnabled = false;
} else {
this.isScrollingEnabled = true;
}
},
async loadMoreTags(event: any){
this.getTags(
undefined,
Math.ceil(this.facetOptions.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
},
updateSelectedValues(value: string) {
this.selectedValues.includes(value) ? this.selectedValues.splice(this.selectedValues.indexOf(value), 1) : this.selectedValues.push(value);
Expand Down
33 changes: 24 additions & 9 deletions src/views/SelectProduct.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ion-toolbar>
</ion-header>

<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<div class="find">

<aside class="filters desktop-only">
Expand Down Expand Up @@ -133,14 +133,16 @@
</section>
<hr />
</div>
<!--
<!--
When searching for a keyword, and if the user moves to the last item, then the didFire value inside infinite scroll becomes true and thus the infinite scroll does not trigger again on the same page(https://github.com/hotwax/users/issues/84).
Also if we are at the section that has been loaded by infinite-scroll and then move to the details page then the list infinite scroll does not work after coming back to the page
In ionic v7.6.0, an issue related to infinite scroll has been fixed that when more items can be added to the DOM, but infinite scroll does not fire as the window is not completely filled with the content(https://github.com/ionic-team/ionic-framework/issues/18071).
The above fix in ionic 7.6.0 is resulting in the issue of infinite scroll not being called again.
To fix this, we have added a key with value as queryString(searched keyword), so that the infinite scroll component can be re-rendered
whenever the searched string is changed resulting in the correct behaviour for infinite scroll
To fix this we have maintained another variable `isScrollingEnabled` to check whether the scrolling can be performed or not.
If we do not define an extra variable and just use v-show to check for `isScrollable` then when coming back to the page infinite-scroll is called programatically.
We have added an ionScroll event on ionContent to check whether the infiniteScroll can be enabled or not by toggling the value of isScrollingEnabled whenever the height < 0.
-->
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" :disabled="!isScrollable" :key="queryString">
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" v-show="isScrollingEnabled && isScrollable" ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="$t('Loading')"/>
</ion-infinite-scroll>
</main>
Expand Down Expand Up @@ -265,7 +267,8 @@ export default defineComponent({
isFilterChanged: false,
isServiceScheduling: false,
job: {} as any,
jobId: "" as any
jobId: "" as any,
isScrollingEnabled: false
}
},
methods: {
Expand Down Expand Up @@ -339,13 +342,24 @@ export default defineComponent({
const viewIndex = vIndex ? vIndex : 0;
this.store.dispatch("product/updateQuery", { viewSize, viewIndex, queryString: this.queryString })
},
enableScrolling() {
const parentElement = (this as any).$refs.contentRef.$el
const scrollEl = parentElement.shadowRoot.querySelector("main[part='scroll']")
let scrollHeight = scrollEl.scrollHeight, infiniteHeight = (this as any).$refs.infiniteScrollRef.$el.offsetHeight, scrollTop = scrollEl.scrollTop, threshold = 100, height = scrollEl.offsetHeight
const distanceFromInfinite = scrollHeight - infiniteHeight - scrollTop - threshold - height
if(distanceFromInfinite < 0) {
this.isScrollingEnabled = false;
} else {
this.isScrollingEnabled = true;
}
},
async loadMoreProducts(event: any){
this.getProducts(
undefined,
Math.ceil(this.products.list.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
},
async updateThreshold() {
this.isServiceScheduling = true;
Expand Down Expand Up @@ -525,6 +539,7 @@ export default defineComponent({
emitter.off("productStoreChanged", this.getProducts);
},
async ionViewWillEnter(){
this.isScrollingEnabled = false;
this.jobId = this.$route.query.id
this.isFilterChanged = false;
if (this.jobId) {
Expand Down

0 comments on commit 714054b

Please sign in to comment.