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

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