diff --git a/frontend/src/app/features/pages/library/library.component.html b/frontend/src/app/features/pages/library/library.component.html index 8fca907..90755c1 100644 --- a/frontend/src/app/features/pages/library/library.component.html +++ b/frontend/src/app/features/pages/library/library.component.html @@ -13,6 +13,16 @@
+ +
@@ -26,4 +36,38 @@
- \ No newline at end of file + + +
+ +
+ + + + + +
+ +
\ No newline at end of file diff --git a/frontend/src/app/features/pages/library/library.component.ts b/frontend/src/app/features/pages/library/library.component.ts index 5751ca1..1b5a2d7 100644 --- a/frontend/src/app/features/pages/library/library.component.ts +++ b/frontend/src/app/features/pages/library/library.component.ts @@ -10,17 +10,20 @@ import { LibraryService } from '../../../core/services/library.service'; export class LibraryComponent { entryType: number = 0; - allEntries: Entry[] = []; - entries: Entry[] = []; + entriesToShow: Entry[] = []; + entriesCount: number = 0; + maxPage: number = 10; currentPage: number = 1; - pageSize: number = 20; + pageSize: number = 10; constructor(private libraryService: LibraryService) {} ngOnInit() { - console.log('initing'); + this.entriesCount = this.entries.length; + this.maxPage = this.entriesCount / this.pageSize; + this.pageSize = this.getDefaultPageSize(); this.fetchEntries(); } @@ -28,7 +31,7 @@ export class LibraryComponent { fetchEntries() { this.libraryService.getAllEntries().subscribe({ next: (entries) => { - this.allEntries = entries; + this.entries = entries; this.filterEntries(); }, error: (error) => { @@ -37,17 +40,19 @@ export class LibraryComponent { }); } - filterEntries() { + async filterEntries() { var temp = [] as Entry[]; // ? type filter if (this.entryType != 0) { - this.allEntries.forEach((entry) => { - if (Boolean(this.entryType - 1) == entry.is_tvseries) temp.push(entry); + await this.entries.forEach((entry) => { + if (this.entryType == 1 && !entry.is_tvseries) temp.push(entry); + else if (this.entryType == 2 && entry.is_tvseries) temp.push(entry); }); } else { - temp = this.allEntries; + temp = this.entries; } + console.log(temp); // ? title search // let len = temp.length; @@ -61,20 +66,44 @@ export class LibraryComponent { const startIndex = (this.currentPage - 1) * this.pageSize; const endIndex = startIndex + this.pageSize; - this.entries = temp.slice(startIndex, endIndex); + this.entriesCount = this.entriesToShow.length; + this.maxPage = this.entriesCount / this.pageSize; + this.entriesToShow = []; + this.entriesToShow = temp.slice(startIndex, endIndex); } // ? pagination goToPage(page: number) { this.currentPage = page; - this.fetchEntries(); + this.filterEntries(); } 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; + this.entriesCount = this.entries.length; + this.maxPage = this.entriesCount / this.pageSize; + // Ensure currentPage is within the bounds. + this.currentPage = Math.max(1, Math.min(this.currentPage, this.maxPage)); + + let pagesToShow = 5; // Total pages to show at any time + let startPage: number, endPage: number; + + if (this.maxPage <= pagesToShow) { + // Case when the total number of pages is less or equal to pagesToShow. + startPage = 1; + endPage = this.maxPage; + } else { + // Calculate the start page. + startPage = Math.max(this.currentPage - Math.floor(pagesToShow / 2), 1); + endPage = startPage + pagesToShow - 1; + + if (endPage >= this.maxPage) { + endPage = this.maxPage; + startPage = endPage - pagesToShow + 1; + } + } + + // Generate the page numbers array. return Array.from( { length: endPage - startPage + 1 }, (_, i) => startPage + i @@ -84,10 +113,16 @@ export class LibraryComponent { // ? setter setEntryType(entryType: number) { this.entryType = entryType; + this.currentPage = 1; this.filterEntries(); } - setPageSize(pageSize: number) { - this.pageSize = pageSize; + setPageSize(event: any) { + this.pageSize = event.target.value; + this.filterEntries() + } + + getDefaultPageSize() { + return 20; } }