Skip to content

Commit

Permalink
Merge pull request #240 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 571d038 + 61b40b2 commit 874ba5d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
34 changes: 28 additions & 6 deletions src/views/FindFacilities.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ion-toolbar>
</ion-header>

<ion-content id="filter-content">
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()" id="filter-content">
<div class="find">
<section class="search">
<ion-searchbar :placeholder="translate('Search facilities')" v-model="query.queryString" @keyup.enter="updateQuery()" />
Expand Down Expand Up @@ -105,11 +105,20 @@
</ion-fab-button>
</ion-fab-list>
</ion-fab>

<!--
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="loadMoreFacilities($event)"
threshold="100px"
:disabled="!isScrollable"
v-show="isScrollingEnabled && isScrollable"
ref="infiniteScrollRef"
>
<ion-infinite-scroll-content
loading-spinner="crescent"
Expand Down Expand Up @@ -196,7 +205,8 @@ export default defineComponent({
},
data() {
return {
facilityGroups: [] as any
facilityGroups: [] as any,
isScrollingEnabled: false
}
},
computed: {
Expand All @@ -217,6 +227,7 @@ export default defineComponent({
// fetching facilities information in the ionViewWillEnter hook as when updating facilityGroup or fulfillment limit
// from the details page and again coming to the list page, the UI does not gets updated when fetching information in
// the mounted hook
this.isScrollingEnabled = false;
await this.fetchFacilities();
},
methods: {
Expand All @@ -236,14 +247,25 @@ export default defineComponent({
async viewFacilityDetails(facilityId: string) {
this.router.push({ path: `/facility-details/${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 loadMoreFacilities(event: any) {
this.fetchFacilities(
undefined,
Math.ceil(
this.facilities?.length / (process.env.VUE_APP_VIEW_SIZE as any)
).toString()
).then(() => {
event.target.complete();
).then(async () => {
await event.target.complete();
});
},
async changeOrderLimitPopover(ev: Event, facility: any) {
Expand Down
28 changes: 24 additions & 4 deletions src/views/FindGroups.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</ion-toolbar>
</ion-header>

<ion-content id="filter-content">
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()" id="filter-content">
<div class="find">
<section class="search">
<ion-searchbar :placeholder="translate('Search groups')" v-model="query.queryString" @keyup.enter="updateQuery()" />
Expand Down Expand Up @@ -64,7 +64,8 @@
<ion-infinite-scroll
@ionInfinite="loadMoreGroups($event)"
threshold="100px"
:disabled="!isScrollable"
v-show="isScrollingEnabled && isScrollable"
ref="infiniteScrollRef"
>
<ion-infinite-scroll-content
loading-spinner="crescent"
Expand Down Expand Up @@ -146,6 +147,11 @@ export default defineComponent({
IonTitle,
IonToolbar
},
data() {
return {
isScrollingEnabled: false
}
},
computed: {
...mapGetters({
groups: "facility/getFacilityGroups",
Expand All @@ -158,6 +164,9 @@ export default defineComponent({
await this.fetchGroups();
await this.store.dispatch('util/fetchFacilityGroupTypes')
},
async ionViewWillEnter() {
this.isScrollingEnabled = false;
},
methods: {
async updateQuery() {
await this.store.dispatch('facility/updateGroupQuery', this.query)
Expand All @@ -172,14 +181,25 @@ export default defineComponent({
};
await this.store.dispatch('facility/fetchFacilityGroups', 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 loadMoreGroups(event: any) {
this.fetchGroups(
undefined,
Math.ceil(
this.groups?.length / (process.env.VUE_APP_VIEW_SIZE as any)
).toString()
).then(() => {
event.target.complete();
).then(async () => {
await event.target.complete();
});
},
async openCreateFacilityGroupModal() {
Expand Down

0 comments on commit 874ba5d

Please sign in to comment.