Skip to content

Commit

Permalink
Improved: the logic for fixing infinite scroll(hotwax#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
R-Sourabh committed Apr 17, 2024
1 parent 64ceb83 commit 897b2fd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
25 changes: 19 additions & 6 deletions src/views/catalog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ion-toolbar>
</ion-header>

<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<div class="header">
<div class="filters ion-padding-top">
<ion-toolbar>
Expand Down Expand Up @@ -77,7 +77,7 @@
</ion-item> -->
</div>

<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" :disabled="!isCatalogScrollable" :key="queryString">
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" v-show="isScrollingEnabled && isCatalogScrollable" ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="$t('Loading')" />
</ion-infinite-scroll>
</div>
Expand Down Expand Up @@ -157,7 +157,8 @@ export default defineComponent({
value: 'REMOVED'
}*/],
queryString: '',
preordBckordComputationJob: {} as any
preordBckordComputationJob: {} as any,
isScrollingEnabled: false
}
},
computed: {
Expand All @@ -169,6 +170,7 @@ export default defineComponent({
})
},
async ionViewWillEnter() {
this.isScrollingEnabled = false;
await this.getCatalogProducts()
await this.preparePreordBckordComputationJob()
},
Expand Down Expand Up @@ -200,13 +202,24 @@ export default defineComponent({
await this.store.dispatch("product/findCatalogProducts", payload);
},
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.getCatalogProducts(
undefined,
Math.ceil(this.products.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
},
async applyFilter(value: string) {
if(value !== this.prodCatalogCategoryTypeId) {
Expand Down
31 changes: 24 additions & 7 deletions src/views/orders.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ion-toolbar>
</ion-header>

<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<div class="header">
<div class="search">
<ion-searchbar @ionFocus="selectSearchBarText($event)" @ionClear="query.queryString = ''; updateQuery()" :value="query.queryString" v-on:keyup.enter="query.queryString = $event.target.value; updateQuery()"> </ion-searchbar>
Expand Down Expand Up @@ -143,12 +143,14 @@
</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="loadMoreOrders($event)" threshold="100px" id="infinite-scroll" :disabled="!isScrolleable" :key="query.queryString">
<ion-infinite-scroll @ionInfinite="loadMoreOrders($event)" threshold="100px" id="infinite-scroll" v-show="isScrollingEnabled && isScrolleable" ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="$t('Loading')"></ion-infinite-scroll-content>
</ion-infinite-scroll>
</div>
Expand Down Expand Up @@ -264,6 +266,7 @@ export default defineComponent({
data() {
return {
cusotmerLoyaltyOptions : JSON.parse(process.env?.VUE_APP_CUST_LOYALTY_OPTIONS),
isScrollingEnabled: false
}
},
computed: {
Expand All @@ -284,17 +287,31 @@ export default defineComponent({
query: 'order/getQuery',
}),
},
async ionViewWillEnter() {
this.isScrollingEnabled = false;
},
methods: {
updateQuery() {
this.query.viewSize = parseInt(process.env.VUE_APP_VIEW_SIZE);
this.query.viewIndex = 0;
this.store.dispatch("order/updateQuery", { query: this.query});
},
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 loadMoreOrders(event: any) {
this.query.viewIndex = Math.ceil(this.orders.length / process.env.VUE_APP_VIEW_SIZE);
this.store.dispatch("order/updateQuery", { query: this.query}).then(() => {
event.target.complete();
})
this.store.dispatch("order/updateQuery", { query: this.query}).then(async () => {
await event.target.complete();
});
},
async releaseItems() {
emitter.emit("presentLoader")
Expand Down
27 changes: 21 additions & 6 deletions src/views/products.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ion-toolbar>
</ion-header>

<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<ion-searchbar @ionFocus="selectSearchBarText($event)" :placeholder="$t('Search products')" v-model="queryString" v-on:keyup.enter="getProducts()"></ion-searchbar>

<!-- Empty state -->
Expand All @@ -38,7 +38,7 @@
<ion-badge slot="end" color="success">{{ product.doclist.numFound }} {{ $t("pieces preordered") }}</ion-badge>
</ion-item>

<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" id="infinite-scroll" :disabled="!isScrolleable" :key="queryString">
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" id="infinite-scroll" v-show="isScrollingEnabled && isScrolleable" ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="$t('Loading')"></ion-infinite-scroll-content>
</ion-infinite-scroll>
</div>
Expand Down Expand Up @@ -101,7 +101,8 @@ export default defineComponent({
orderedAfter: '',
orderedBefore: '',
selectedItems: [] as any,
hasQuery: false
hasQuery: false,
isScrollingEnabled: false
}
},
computed: {
Expand All @@ -114,14 +115,28 @@ export default defineComponent({
currentEComStore: 'user/getCurrentEComStore',
})
},
async ionViewWillEnter() {
this.isScrollingEnabled = false;
},
methods: {
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.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
},
async getProducts( vSize?: any, vIndex?: any) {
const viewSize = vSize ? vSize : process.env.VUE_APP_VIEW_SIZE;
Expand Down

0 comments on commit 897b2fd

Please sign in to comment.