Skip to content

Commit

Permalink
Improved: the logic for fixing infinite scroll(#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
R-Sourabh committed Apr 17, 2024
1 parent 5199858 commit 14adf4c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
34 changes: 25 additions & 9 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 @@ -107,16 +107,18 @@
</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 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="loadMoreFacilities($event)"
threshold="100px"
:disabled="!isScrollable"
:key="query.queryString"
v-show="isScrollingEnabled && isScrollable"
ref="infiniteScrollRef"
>
<ion-infinite-scroll-content
loading-spinner="crescent"
Expand Down Expand Up @@ -203,7 +205,8 @@ export default defineComponent({
},
data() {
return {
facilityGroups: [] as any
facilityGroups: [] as any,
isScrollingEnabled: false
}
},
computed: {
Expand All @@ -224,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 @@ -243,14 +247,26 @@ export default defineComponent({
async viewFacilityDetails(facilityId: string) {
this.router.push({ path: `/facility-details/${facilityId}` })
},
enableScrolling() {
console.log('scroll');

Check warning on line 251 in src/views/FindFacilities.vue

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (18.x)

Unexpected console statement

Check warning on line 251 in src/views/FindFacilities.vue

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (20.x)

Unexpected console statement
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
29 changes: 24 additions & 5 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,8 +64,8 @@
<ion-infinite-scroll
@ionInfinite="loadMoreGroups($event)"
threshold="100px"
:disabled="!isScrollable"
:key="query.queryString"
v-show="isScrollingEnabled && isScrollable"
ref="infiniteScrollRef"
>
<ion-infinite-scroll-content
loading-spinner="crescent"
Expand Down Expand Up @@ -147,6 +147,11 @@ export default defineComponent({
IonTitle,
IonToolbar
},
data() {
return {
isScrollingEnabled: false
}
},
computed: {
...mapGetters({
groups: "facility/getFacilityGroups",
Expand All @@ -159,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 @@ -173,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 14adf4c

Please sign in to comment.