Skip to content

Commit

Permalink
Merge pull request #384 from R-Sourabh/#289-infinite-scroll
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 19, 2024
2 parents 0fba5d7 + 7ce7647 commit 455a801
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 33 deletions.
35 changes: 30 additions & 5 deletions src/views/AssignPickerModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ion-toolbar>
</ion-header>

<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<ion-searchbar v-model="queryString" @keyup.enter="queryString = $event.target.value; searchPicker()"/>

<div class="ion-text-center ion-margin-top" v-if="!availablePickers.length">{{ translate('No picker found') }}</div>
Expand All @@ -25,10 +25,20 @@
</ion-radio-group>
</div>
</ion-list>
<!--
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="loadMorePickers($event)"
threshold="100px"
:disabled="!isScrollable"
v-show="isScrollingEnabled && isScrollable"
ref="infiniteScrollRef"
>
<ion-infinite-scroll-content
loading-spinner="crescent"
Expand Down Expand Up @@ -100,7 +110,8 @@ export default defineComponent({
selectedPicker: '',
queryString: '',
availablePickers: [],
isScrollable: true
isScrollable: true,
isScrollingEnabled: false
}
},
methods: {
Expand All @@ -119,14 +130,25 @@ export default defineComponent({
showToast(translate('Select a picker'))
}
},
enableScrolling() {
const parentElement = (this).$refs.contentRef.$el
const scrollEl = parentElement.shadowRoot.querySelector("main[part='scroll']")
let scrollHeight = scrollEl.scrollHeight, infiniteHeight = (this).$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 loadMorePickers(event) {
this.getPicker(
undefined,
Math.ceil(
this.availablePickers.length / (process.env.VUE_APP_VIEW_SIZE)
).toString()
).then(() => {
event.target.complete();
).then(async () => {
await event.target.complete();
});
},
async getPicker(vSize, vIndex) {
Expand Down Expand Up @@ -193,6 +215,9 @@ export default defineComponent({
// getting picker information on initial load
await this.getPicker();
},
async ionViewWillEnter() {
this.isScrollingEnabled = false;
},
setup() {
const store = useStore();
Expand Down
22 changes: 18 additions & 4 deletions src/views/Catalog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ion-title>{{ translate("Catalog") }}</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" @keypress.enter="queryString = $event.target.value; getProducts()" />
<main v-if="products.list.length">
<ion-card button v-for="product in products.list" :key="product.productId" @click="viewProduct(product)">
Expand All @@ -24,7 +24,8 @@
<ion-infinite-scroll
@ionInfinite="loadMoreProducts($event)"
threshold="100px"
:disabled="!isScrollable"
v-show="isScrollingEnabled && isScrollable"
ref="infiniteScrollRef"
>
<ion-infinite-scroll-content
loading-spinner="crescent"
Expand Down Expand Up @@ -73,6 +74,7 @@ export default defineComponent({
data() {
return {
queryString: "",
isScrollingEnabled: false
};
},
computed: {
Expand All @@ -82,14 +84,25 @@ export default defineComponent({
}),
},
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.list?.length / (process.env.VUE_APP_VIEW_SIZE as any)
).toString()
).then(() => {
event.target.complete();
).then(async () => {
await event.target.complete();
});
},
async getProducts(vSize?: any, vIndex?: any) {
Expand All @@ -115,6 +128,7 @@ export default defineComponent({
},
async ionViewWillEnter() {
this.isScrollingEnabled = false;
this.queryString = this.products.queryString;
this.getProducts();
},
Expand Down
39 changes: 27 additions & 12 deletions src/views/Orders.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</ion-segment>
</div>
</ion-header>
<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<div v-if="segmentSelected === 'open'">
<div v-for="(order, index) in getOrdersByPart(orders)" :key="index" v-show="order.parts.length > 0">
<ion-card button @click.prevent="viewOrder(order, order.part, 'open')">
Expand Down Expand Up @@ -157,7 +157,9 @@
<ion-refresher slot="fixed" @ionRefresh="refreshOrders($event)">
<ion-refresher-content pullingIcon="crescent" refreshingSpinner="crescent" />
</ion-refresher>
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" :disabled="segmentSelected === 'open' ? !isOpenOrdersScrollable : segmentSelected === 'packed' ? !isPackedOrdersScrollable : !isCompletedOrdersScrollable">
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px"
v-show="isScrollingEnabled && (segmentSelected === 'open' ? isOpenOrdersScrollable : segmentSelected === 'packed' ? isPackedOrdersScrollable : isCompletedOrdersScrollable)"
ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="translate('Loading')" />
</ion-infinite-scroll>
</ion-content>
Expand Down Expand Up @@ -257,7 +259,8 @@ export default defineComponent({
},
data() {
return {
queryString: ''
queryString: '',
isScrollingEnabled: false
}
},
methods: {
Expand Down Expand Up @@ -337,28 +340,39 @@ export default defineComponent({

await this.store.dispatch("order/getCompletedOrders", { viewSize, viewIndex, queryString: this.queryString, facilityId: this.currentFacility.facilityId });
},
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) {
if (this.segmentSelected === 'open') {
this.getPickupOrders(
undefined,
Math.ceil(this.orders.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
} else if (this.segmentSelected === 'packed') {
this.getPackedOrders(
undefined,
Math.ceil(this.packedOrders.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
} else {
this.getCompletedOrders(
undefined,
Math.ceil(this.completedOrders.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
}
},
async readyForPickup (order: any, part: any) {
Expand Down Expand Up @@ -459,6 +473,7 @@ export default defineComponent({
}
},
ionViewWillEnter () {
this.isScrollingEnabled = false;
this.queryString = '';

if(this.segmentSelected === 'open') {
Expand Down
39 changes: 27 additions & 12 deletions src/views/ShipToStoreOrders.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</ion-segment>
</div>
</ion-header>
<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<div v-if="segmentSelected === 'incoming'">
<div v-for="(order, index) in incomingOrders" :key="index" v-show="order.items.length">
<ion-card button>
Expand Down Expand Up @@ -90,7 +90,9 @@
<ion-refresher slot="fixed" @ionRefresh="refreshOrders($event)">
<ion-refresher-content pullingIcon="crescent" refreshingSpinner="crescent" />
</ion-refresher>
<ion-infinite-scroll @ionInfinite="loadMoreOrders($event)" threshold="100px" :disabled="segmentSelected === 'incoming' ? !isIncomingOrdersScrollable : segmentSelected === 'readyForPickup' ? !isReadyForPickupOrdersScrollable : !isCompletedOrdersScrollable">
<ion-infinite-scroll @ionInfinite="loadMoreOrders($event)" threshold="100px"
v-show="isScrollingEnabled && (segmentSelected === 'incoming' ? isIncomingOrdersScrollable : segmentSelected === 'readyForPickup' ? isReadyForPickupOrdersScrollable : isCompletedOrdersScrollable)"
ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="translate('Loading')" />
</ion-infinite-scroll>
</ion-content>
Expand Down Expand Up @@ -178,7 +180,8 @@ export default defineComponent({
},
data() {
return {
queryString: ''
queryString: '',
isScrollingEnabled: false
}
},
methods: {
Expand Down Expand Up @@ -212,28 +215,39 @@ export default defineComponent({
await this.store.dispatch("order/getShipToStoreCompletedOrders", { viewSize, viewIndex, queryString: this.queryString, facilityId: this.currentFacility.facilityId });
},
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) {
if (this.segmentSelected === 'incoming') {
this.getIncomingOrders(
undefined,
Math.ceil(this.incomingOrders.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
} else if (this.segmentSelected === 'packed') {
this.getReadyForPickupOrders(
undefined,
Math.ceil(this.readyForPickupOrders.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
} else {
this.getCompletedOrders(
undefined,
Math.ceil(this.completedOrders.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
}
},
segmentChanged (event: CustomEvent) {
Expand Down Expand Up @@ -389,6 +403,7 @@ export default defineComponent({
},
},
ionViewWillEnter() {
this.isScrollingEnabled = false;
this.queryString = '';
if (this.segmentSelected === 'incoming') {
this.getIncomingOrders()
Expand Down

0 comments on commit 455a801

Please sign in to comment.