Skip to content

Commit

Permalink
Merge pull request #252 from R-Sourabh/dxp-289
Browse files Browse the repository at this point in the history
Fixed: infinite scroll issue when used with searchbar(dxp-289)
  • Loading branch information
ymaheshwari1 authored Apr 23, 2024
2 parents 72886db + 51ea33b commit 46e4a45
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
31 changes: 25 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 @@ -28,7 +28,7 @@
<ion-icon :icon="checkmarkOutline" />
</ion-fab-button>
</ion-fab>
<ion-infinite-scroll @ionInfinite="loadMoreTags($event)" threshold="100px" :disabled="!isScrollable">
<ion-infinite-scroll @ionInfinite="loadMoreTags($event)" threshold="100px" v-show="isScrollable" ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="$t('Loading')"/>
</ion-infinite-scroll>
</ion-content>
Expand Down Expand Up @@ -85,7 +85,8 @@ export default defineComponent({
facetOptions: [] as any,
isFilterChanged: false,
isScrollable: true,
selectedValues: [] as Array<string>
selectedValues: [] as Array<string>,
isScrollingEnabled: false
}
},
computed: {
Expand All @@ -97,6 +98,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 @@ -131,13 +135,28 @@ 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){
// Added this check here as if added on infinite-scroll component the Loading content does not gets displayed
if(!(this.isScrollingEnabled && this.isScrollable)) {
await event.target.complete();
}
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
38 changes: 32 additions & 6 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 @@ -130,7 +130,16 @@
</section>
<hr />
</div>
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" :disabled="!isScrollable">
<!--
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 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" v-show="isScrollable" ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="$t('Loading')"/>
</ion-infinite-scroll>
</main>
Expand Down Expand Up @@ -255,7 +264,8 @@ export default defineComponent({
isFilterChanged: false,
isServiceScheduling: false,
job: {} as any,
jobId: "" as any
jobId: "" as any,
isScrollingEnabled: false
}
},
methods: {
Expand Down Expand Up @@ -329,13 +339,28 @@ 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){
// Added this check here as if added on infinite-scroll component the Loading content does not gets displayed
if(!(this.isScrollingEnabled && this.isScrollable)) {
await event.target.complete();
}
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 @@ -510,6 +535,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 46e4a45

Please sign in to comment.