Skip to content

Commit

Permalink
Merge pull request #287 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 7749fa6 + 9cf1ba1 commit 503167e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 18 deletions.
29 changes: 23 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">
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" v-show="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,28 @@ 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){
// Added this check here as if added on infinite-scroll component the Loading content does not gets displayed
if(!(this.isScrollingEnabled && this.isCatalogScrollable)) {
await event.target.complete();
}
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
39 changes: 33 additions & 6 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 @@ -141,8 +141,16 @@
</ion-card>
</div>
</div>

<ion-infinite-scroll @ionInfinite="loadMoreOrders($event)" threshold="100px" id="infinite-scroll" :disabled="!isScrolleable">
<!--
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="loadMoreOrders($event)" threshold="100px" id="infinite-scroll" v-show="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 @@ -258,6 +266,7 @@ export default defineComponent({
data() {
return {
cusotmerLoyaltyOptions : JSON.parse(process.env?.VUE_APP_CUST_LOYALTY_OPTIONS),
isScrollingEnabled: false
}
},
computed: {
Expand All @@ -278,17 +287,35 @@ 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) {
// Added this check here as if added on infinite-scroll component the Loading content does not gets displayed
if(!(this.isScrollingEnabled && this.isScrolleable)) {
await event.target.complete();
}
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
31 changes: 25 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">
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" id="infinite-scroll" v-show="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,32 @@ 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) {
// Added this check here as if added on infinite-scroll component the Loading content does not gets displayed
if(!(this.isScrollingEnabled && this.isScrolleable)) {
await event.target.complete();
}
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 503167e

Please sign in to comment.