Skip to content

Commit

Permalink
Merge pull request #371 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 22, 2024
2 parents 70d7626 + 521f848 commit fc53c38
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
41 changes: 34 additions & 7 deletions src/views/AddProductModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ion-title>{{ translate("Add a product") }}</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<ion-searchbar @ionFocus="selectSearchBarText($event)" v-model="queryString" :placeholder="translate('Search SKU or product name')" v-on:keyup.enter="queryString = $event.target.value; getProducts()" />

<template v-if="products.length">
Expand All @@ -27,8 +27,16 @@
<ion-button v-else fill="outline" @click="addtoShipment(product)">{{ translate("Add to Shipment") }}</ion-button>
</ion-item>
</ion-list>

<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="translate('Loading')" />
</ion-infinite-scroll>
</template>
Expand Down Expand Up @@ -85,7 +93,8 @@ export default defineComponent({
},
data() {
return {
queryString: this.selectedSKU ? this.selectedSKU : ''
queryString: this.selectedSKU ? this.selectedSKU : '',
isScrollingEnabled: false
}
},
props: ["selectedSKU"],
Expand All @@ -102,7 +111,21 @@ export default defineComponent({
mounted() {
if(this.selectedSKU) this.getProducts()
},
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 getProducts( vSize?: any, vIndex?: any) {
const viewSize = vSize ? vSize : process.env.VUE_APP_VIEW_SIZE;
const viewIndex = vIndex ? vIndex : 0;
Expand All @@ -119,12 +142,16 @@ export default defineComponent({
}
},
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.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
},
async addtoShipment (product: any) {
product.locationSeqId = this.facilityLocationsByFacilityId(this.currentFacility.facilityId) ? this.facilityLocationsByFacilityId(this.currentFacility.facilityId)[0]?.locationSeqId : ''
Expand Down
31 changes: 25 additions & 6 deletions src/views/AddProductToPOModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ion-title>{{ translate("Add a product") }}</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<ion-searchbar @ionFocus="selectSearchBarText($event)" v-model="queryString" :placeholder="translate('Search SKU or product name')" v-on:keyup.enter="queryString = $event.target.value; getProducts()" />
<template v-if="products.length">
<ion-list v-for="product in products" :key="product.productId">
Expand All @@ -27,7 +27,7 @@
</ion-item>
</ion-list>

<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" :disabled="!isScrollable">
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" v-show="isScrollable" ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="translate('Loading')" />
</ion-infinite-scroll>
</template>
Expand Down Expand Up @@ -84,7 +84,8 @@ export default defineComponent({
},
data() {
return {
queryString: this.selectedSKU ? this.selectedSKU : ''
queryString: this.selectedSKU ? this.selectedSKU : '',
isScrollingEnabled: false
}
},
props: ["selectedSKU"],
Expand All @@ -101,6 +102,9 @@ export default defineComponent({
mounted() {
if(this.selectedSKU) this.getProducts()
},
async ionViewWillEnter() {
this.isScrollingEnabled = false;
},
methods: {
async getProducts( vSize?: any, vIndex?: any) {
const viewSize = vSize ? vSize : process.env.VUE_APP_VIEW_SIZE;
Expand All @@ -117,13 +121,28 @@ export default defineComponent({
showToast(translate("Enter product sku to search"))
}
},
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.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
},
async addtoOrder (product: any) {
product.locationSeqId = this.facilityLocationsByFacilityId(this.currentFacility.facilityId) ? this.facilityLocationsByFacilityId(this.currentFacility.facilityId)[0]?.locationSeqId : ''
Expand Down

0 comments on commit fc53c38

Please sign in to comment.