From 3aa59eec16bb7caa43c82c6371589b36beb9de3e Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Thu, 29 Aug 2024 18:59:03 +0100 Subject: [PATCH] add loading spinner in search page --- .../search-page/search-page.component.css | 6 ++ .../search-page/search-page.component.html | 4 + .../search-page/search-page.component.ts | 92 ++++++++++++++++--- 3 files changed, 88 insertions(+), 14 deletions(-) diff --git a/frontend/src/app/features/discover/search-page/search-page.component.css b/frontend/src/app/features/discover/search-page/search-page.component.css index f87c161..0e15440 100644 --- a/frontend/src/app/features/discover/search-page/search-page.component.css +++ b/frontend/src/app/features/discover/search-page/search-page.component.css @@ -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; } \ No newline at end of file diff --git a/frontend/src/app/features/discover/search-page/search-page.component.html b/frontend/src/app/features/discover/search-page/search-page.component.html index 5a9e837..d86e263 100644 --- a/frontend/src/app/features/discover/search-page/search-page.component.html +++ b/frontend/src/app/features/discover/search-page/search-page.component.html @@ -47,6 +47,10 @@ +
+ Loading... +
+
diff --git a/frontend/src/app/features/discover/search-page/search-page.component.ts b/frontend/src/app/features/discover/search-page/search-page.component.ts index 27b34e8..bde1602 100644 --- a/frontend/src/app/features/discover/search-page/search-page.component.ts +++ b/frontend/src/app/features/discover/search-page/search-page.component.ts @@ -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> = { 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; + }, }); } }