Implemented loadmore on scroll in search page

This commit is contained in:
X4Lo committed 2025-01-27 06:16:02 +01:00
1 parent 2a904b4878
commit cfbb77d447
3 files changed
+45 -50

No files matched your search

@@ -6,4 +6,10 @@
text-align: center;
padding: 20px;
font-size: 1.2em;
}
.loading-button {
text-align: center;
padding: 20px;
font-size: 1.2em;
}
@@ -31,12 +31,11 @@
<!--end breadcrumb-->
<!-- Pagination -->
<div class="pagination-container d-flex justify-content-end" *ngIf="maxPage > 1">
<!-- <div class="pagination-container d-flex justify-content-end" *ngIf="maxPage > 1">
<ul class="pagination pagination-sm">
<li class="page-item" [class.disabled]="currentPage === 1">
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a>
</li>
<!-- Dynamically generate page numbers -->
<li class="page-item" *ngFor="let page of pageNumbers" [class.active]="currentPage === page">
<a class="page-link" href="javascript:;" (click)="goToPage(page)">{{ page }}</a>
</li>
@@ -44,21 +43,20 @@
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage + 1)">Next</a>
</li>
</ul>
</div>
</div> -->
<app-dynamic-entry-list [entries]="entries" (onEntryUpdate)="entryUpdated($event)"></app-dynamic-entry-list>
<div *ngIf="loading" class="loading-indicator">
<div *ngIf="isLoading" class="loading-indicator">
Loading...
</div>
<button (click)="loadNext()">Load Next</button>
<button class="loading-button" (click)="loadNext()">Load More</button>
<!-- Pagination -->
<div class="pagination-container d-flex justify-content-end" *ngIf="maxPage > 1">
<!-- <div class="pagination-container d-flex justify-content-end" *ngIf="maxPage > 1">
<ul class="pagination pagination-sm">
<li class="page-item" [class.disabled]="currentPage === 1">
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a>
</li>
<!-- Dynamically generate page numbers -->
<li class="page-item" *ngFor="let page of pageNumbers" [class.active]="currentPage === page">
<a class="page-link" href="javascript:;" (click)="goToPage(page)">{{ page }}</a>
</li>
@@ -66,4 +64,4 @@
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage + 1)">Next</a>
</li>
</ul>
</div>
</div> -->
@@ -1,22 +1,34 @@
import { Component, HostListener, OnInit, ViewChild } from '@angular/core';
import {
Component,
HostListener,
OnDestroy,
OnInit,
ViewChild,
} from '@angular/core';
import { Entry } from '../../../shared/models/entry.model';
import { TmdbService } from '../../../core/services/tmdb.service';
import { FormControl } from '@angular/forms';
import { debounceTime, distinctUntilChanged, forkJoin } from 'rxjs';
import {
debounceTime,
distinctUntilChanged,
forkJoin,
Subscription,
} from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { ScrollService } from '../../../core/services/scroll.service';
@Component({
selector: 'app-search-page',
templateUrl: './search-page.component.html',
styleUrls: ['./search-page.component.css'],
})
export class SearchPageComponent implements OnInit {
export class SearchPageComponent implements OnInit, OnDestroy {
entryType: boolean = true;
entries: Entry[] = [];
isSearchLocked: boolean = false;
endPoint: string = '';
loading: boolean = false;
isLoading: boolean = false;
keyword: string = '';
@@ -30,9 +42,12 @@ export class SearchPageComponent implements OnInit {
totalEntries: number = 0;
searchControl = new FormControl();
private scrollSubscription!: Subscription;
constructor(
private route: ActivatedRoute,
private tmdbService: TmdbService,
private route: ActivatedRoute
private scrollService: ScrollService
) {}
ngOnInit() {
@@ -53,20 +68,18 @@ export class SearchPageComponent implements OnInit {
this.searchControl.setValue(title);
}
});
this.scrollSubscription = this.scrollService.scrollEnd$.subscribe(() => {
if (!this.isLoading) {
this.loadNext();
}
});
}
// @ViewChild(PerfectScrollbarComponent) componentRef?: PerfectScrollbarComponent;
@HostListener('window:scroll', ['$event'])
onScroll(event: Event): void {
console.log(1);
const pos =
(document.documentElement.scrollTop || document.body.scrollTop) +
window.innerHeight;
const max = document.documentElement.scrollHeight;
if (pos >= max - 80 && !this.loading) {
this.loadNext();
ngOnDestroy(): void {
// Unsubscribe to avoid memory leaks
if (this.scrollSubscription) {
this.scrollSubscription.unsubscribe();
}
}
@@ -93,28 +106,6 @@ export class SearchPageComponent implements OnInit {
}
// ? EVENTS
// onInputChange(): void {
// return;
// const inputValue = this.searchInput;
// this.currentPage = 1;
// if (!this.isSearchLocked) {
// console.log(this.isSearchLocked);
// this.isSearchLocked = true;
// this.doSearch(inputValue);
// setTimeout(() => {
// this.isSearchLocked = false;
// }, 3000);
// }
// }
// onEnterPress() {
// this.currentPage = 1;
// this.doSearch(this.searchInput);
// }
// ? EVENT CATCHER
entryUpdated(event: Event) {}
@@ -153,18 +144,18 @@ export class SearchPageComponent implements OnInit {
}
loadNext() {
this.loading = true;
this.isLoading = true;
this.currentPage += 1;
this.tmdbService
.getByKeywordMulti(this.keyword, this.currentPage)
.subscribe({
next: (result) => {
this.entries = this.entries.concat(result.entries as Entry[]);
this.loading = false;
this.isLoading = false;
},
error: (error) => {
console.log(`Error: can't search entries...`, error);
this.loading = false;
this.isLoading = false;
},
});
}