diff --git a/frontend/src/app/features/pages/trending/trending.component.ts b/frontend/src/app/features/pages/trending/trending.component.ts index be897f9..73a1fae 100644 --- a/frontend/src/app/features/pages/trending/trending.component.ts +++ b/frontend/src/app/features/pages/trending/trending.component.ts @@ -1,4 +1,6 @@ import { Component } from '@angular/core'; +import { Entry } from '../../../shared/models/entry.model'; +import { TmdbService } from '../../../core/services/tmdb.service'; @Component({ selector: 'app-trending', @@ -6,13 +8,58 @@ import { Component } from '@angular/core'; styleUrls: ['./trending.component.css'], }) export class TrendingComponent { - activeComponent: boolean = true; + entryType: boolean = true; + entries: Entry[] = []; - showComponent(activeComponent: boolean) { - this.activeComponent = activeComponent; + pages: Record> = { + true: {}, + false: {}, + }; + currentPage: number = 1; + + constructor(private tmdbService: TmdbService) {} + + ngOnInit() { + this.fetchEntries(); } - getEndPoint(): string { - return ''; + setEntryType(entryType: boolean) { + this.entryType = entryType; + this.fetchEntries(); + } + + fetchEntries() { + if (this.pages[this.entryType.toString()][this.currentPage]) { + // ? not in cash > fetch entries from TMDB + this.entries = this.pages[this.entryType.toString()][this.currentPage]; + } else { + // ? in cash > use cash entries + this.tmdbService + .getTrending(this.currentPage, this.entryType) + .subscribe((myEntries) => { + // * add fetched entries to the cash + this.pages[this.entryType.toString()][this.currentPage] = myEntries; + + // * add fetched entries to the display list + this.entries = + this.pages[this.entryType.toString()][this.currentPage]; + }); + } + } + + goToPage(page: number) { + this.currentPage = page; + this.fetchEntries(); + } + + get pageNumbers(): number[] { + this.currentPage = this.currentPage <= 0 ? 1 : this.currentPage; // page <= 0 fix + let startPage = this.currentPage <= 3 ? 1 : this.currentPage - 2; + let endPage = startPage + 4; + + return Array.from( + { length: endPage - startPage + 1 }, + (_, i) => startPage + i + ); } }