add loading spinner in search page
This commit is contained in:
1 parent
1b36bc66fe
commit
3aa59eec16
3 files changed
+88
-14
No files matched your search
@@ -1,3 +1,9 @@
|
||||
.ms-auto.d-flex .btn-group button {
|
||||
min-width: 100px;
|
||||
}
|
||||
|
||||
.loading-indicator {
|
||||
text-align: center;
|
||||
padding: 20px;
|
||||
font-size: 1.2em;
|
||||
}
|
||||
@@ -47,6 +47,10 @@
|
||||
</div>
|
||||
|
||||
<app-dynamic-entry-list [entries]="entries" (onEntryUpdate)="entryUpdated($event)"></app-dynamic-entry-list>
|
||||
<div *ngIf="loading" class="loading-indicator">
|
||||
Loading...
|
||||
</div>
|
||||
<button (click)="loadNext()">Load Next</button>
|
||||
|
||||
<!-- Pagination -->
|
||||
<div class="pagination-container d-flex justify-content-end" *ngIf="maxPage > 1">
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { Component, HostListener, 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 } from 'rxjs';
|
||||
import { debounceTime, distinctUntilChanged, forkJoin } from 'rxjs';
|
||||
import { ActivatedRoute } from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-search-page',
|
||||
@@ -15,6 +16,10 @@ export class SearchPageComponent implements OnInit {
|
||||
isSearchLocked: boolean = false;
|
||||
endPoint: string = '';
|
||||
|
||||
loading: boolean = false;
|
||||
|
||||
keyword: string = '';
|
||||
|
||||
pages: Record<string, Record<number, Entry[]>> = {
|
||||
true: {},
|
||||
false: {},
|
||||
@@ -25,7 +30,10 @@ export class SearchPageComponent implements OnInit {
|
||||
totalEntries: number = 0;
|
||||
searchControl = new FormControl();
|
||||
|
||||
constructor(private tmdbService: TmdbService) {}
|
||||
constructor(
|
||||
private tmdbService: TmdbService,
|
||||
private route: ActivatedRoute
|
||||
) {}
|
||||
|
||||
ngOnInit() {
|
||||
this.searchControl.valueChanges
|
||||
@@ -38,6 +46,28 @@ export class SearchPageComponent implements OnInit {
|
||||
this.doSearch(value);
|
||||
// this.search(value); // Call your API search function here
|
||||
});
|
||||
|
||||
this.route.queryParams.subscribe((params: any) => {
|
||||
if (params && params['title']) {
|
||||
let title = params['title'];
|
||||
this.searchControl.setValue(title);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// @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();
|
||||
}
|
||||
}
|
||||
|
||||
setEntryType(entryType: boolean) {
|
||||
@@ -86,22 +116,56 @@ export class SearchPageComponent implements OnInit {
|
||||
// }
|
||||
|
||||
// ? EVENT CATCHER
|
||||
entryUpdated(event: Event) {
|
||||
|
||||
}
|
||||
entryUpdated(event: Event) {}
|
||||
|
||||
// ? FUNCTIONS
|
||||
doSearch(keyword: string) {
|
||||
keyword = keyword.toLowerCase().trim();
|
||||
if (keyword.length <= 0) return;
|
||||
this.keyword = keyword.toLowerCase().trim();
|
||||
if (this.keyword.length <= 0) return;
|
||||
|
||||
let type = this.entryType ? 'movie' : 'tv';
|
||||
this.entries = [];
|
||||
this.currentPage = 0;
|
||||
this.loadNext();
|
||||
// this.tmdbService.getByKeywordMulti(keyword, this.currentPage).subscribe({
|
||||
// next: (result) => {},
|
||||
// });
|
||||
|
||||
// const forkObs = forkJoin([moviesObs, seriesObs]).subscribe({
|
||||
// next: ([movies, series]) => {
|
||||
// this.entries = movies.entries as Entry[];
|
||||
// this.entries = this.entries.concat(series.entries as Entry[]);
|
||||
// this.maxPage =
|
||||
// movies.total_pages > series.total_pages
|
||||
// ? movies.total_pages
|
||||
// : series.total_pages;
|
||||
|
||||
// this.totalEntries = movies.total_results + series.total_results;
|
||||
// },
|
||||
// });
|
||||
|
||||
// this.tmdbService
|
||||
// .getByKeyword(keyword, type, this.currentPage)
|
||||
// .subscribe((result) => {
|
||||
// this.entries = result.entries as Entry[];
|
||||
// this.maxPage = result.total_pages;
|
||||
// this.totalEntries = result.total_results;
|
||||
// });
|
||||
}
|
||||
|
||||
loadNext() {
|
||||
this.loading = true;
|
||||
this.currentPage += 1;
|
||||
this.tmdbService
|
||||
.getByKeyword(keyword, type, this.currentPage)
|
||||
.subscribe((result) => {
|
||||
this.entries = result.entries as Entry[];
|
||||
this.maxPage = result.total_pages;
|
||||
this.totalEntries = result.total_results;
|
||||
.getByKeywordMulti(this.keyword, this.currentPage)
|
||||
.subscribe({
|
||||
next: (result) => {
|
||||
this.entries = this.entries.concat(result.entries as Entry[]);
|
||||
this.loading = false;
|
||||
},
|
||||
error: (error) => {
|
||||
console.log(`Error: can't search entries...`, error);
|
||||
this.loading = false;
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user