From f68d5cbb8938fb230659fcd973b59cbf6f133428 Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Sun, 28 Apr 2024 00:26:58 +0100 Subject: [PATCH] add watchlist page --- .../watchlist/watchlist.component.css | 36 ++++++ .../watchlist/watchlist.component.html | 74 ++++++++++++ .../watchlist/watchlist.component.spec.ts | 23 ++++ .../features/watchlist/watchlist.component.ts | 111 ++++++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 frontend/src/app/features/watchlist/watchlist.component.css create mode 100644 frontend/src/app/features/watchlist/watchlist.component.html create mode 100644 frontend/src/app/features/watchlist/watchlist.component.spec.ts create mode 100644 frontend/src/app/features/watchlist/watchlist.component.ts diff --git a/frontend/src/app/features/watchlist/watchlist.component.css b/frontend/src/app/features/watchlist/watchlist.component.css new file mode 100644 index 0000000..2709cf5 --- /dev/null +++ b/frontend/src/app/features/watchlist/watchlist.component.css @@ -0,0 +1,36 @@ +.custom-group .form-select { + border-top-right-radius: 0; + border-bottom-right-radius: 0; +} + +.custom-group .form-select, +.custom-group .input-group-text { + border: 1px black solid; +} + +.custom-group .btn-group .btn { + border-radius: 0; +} + +.custom-group .btn-group .left-btn { + /* border-top-left-radius: 0.25rem; */ + /* border-bottom-left-radius: 0.25rem; */ + border-left: 0; + border-right: 0; +} + +.custom-group .btn-group .right-btn { + border-top-right-radius: 0.25rem; + border-bottom-right-radius: 0.25rem; + border-left: 0; + +} + +.custom-group .form-select, +.custom-group .btn-outline-dark { + margin-right: -1px; +} + +#single-select-field { + max-width: 120px; +} \ No newline at end of file diff --git a/frontend/src/app/features/watchlist/watchlist.component.html b/frontend/src/app/features/watchlist/watchlist.component.html new file mode 100644 index 0000000..7d3ceba --- /dev/null +++ b/frontend/src/app/features/watchlist/watchlist.component.html @@ -0,0 +1,74 @@ + + + + + +
+ +
+ + + + + +
+ +
\ No newline at end of file diff --git a/frontend/src/app/features/watchlist/watchlist.component.spec.ts b/frontend/src/app/features/watchlist/watchlist.component.spec.ts new file mode 100644 index 0000000..2cde8d7 --- /dev/null +++ b/frontend/src/app/features/watchlist/watchlist.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { WatchlistComponent } from './watchlist.component'; + +describe('WatchlistComponent', () => { + let component: WatchlistComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [WatchlistComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(WatchlistComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/features/watchlist/watchlist.component.ts b/frontend/src/app/features/watchlist/watchlist.component.ts new file mode 100644 index 0000000..89951b0 --- /dev/null +++ b/frontend/src/app/features/watchlist/watchlist.component.ts @@ -0,0 +1,111 @@ +import { Component } from '@angular/core'; +import { Entry } from '../../shared/models/entry.model'; +import { LibraryService } from '../../core/services/library.service'; + +@Component({ + selector: 'app-watchlist', + templateUrl: './watchlist.component.html', + styleUrl: './watchlist.component.css', +}) +export class WatchlistComponent { + entryType: number = 0; + + entries: Entry[] = []; + entriesToShow: Entry[] = []; + + entriesCount: number = 0; + maxPage: number = 10; + currentPage: number = 1; + pageSize: number = 10; + + constructor(private libraryService: LibraryService) {} + + ngOnInit() { + this.pageSize = this.getDefaultPageSize(); + this.getEntries(); + } + + // uses libraryService to get library all content + getEntries(forceFetch: boolean = false) { + this.libraryService.getAllEntries(forceFetch).subscribe({ + next: (entries) => { + this.entries = entries.filter((e) => e.watchlist); + + this.filterEntries(); + }, + error: (error) => { + console.log(`Couldn't get entries.`, error); + // TODO show notification + }, + }); + } + + async filterEntries() { + var temp = [] as Entry[]; + + // ? type filter + if (this.entryType != 0) { + 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.entries; + } + + const startIndex = (this.currentPage - 1) * this.pageSize; + const endIndex = startIndex + this.pageSize; + + this.entriesCount = temp.length; + this.maxPage = Math.ceil(this.entriesCount / this.pageSize); + this.entriesToShow = temp.slice(startIndex, endIndex); + } + + // ? pagination + goToPage(page: number) { + this.currentPage = page; + this.filterEntries(); + } + + get pageNumbers(): number[] { + let pagesToShow = 5; // Total pages to show at any time + let startPage: number, endPage: number; + + if (this.maxPage <= pagesToShow) { + startPage = 1; + endPage = this.maxPage; + } else { + 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 + ); + } + // ? end pagination + + // ? setter + setEntryType(entryType: number) { + this.entryType = entryType; + this.currentPage = 1; + + this.filterEntries(); + } + + setPageSize(event: any) { + this.pageSize = event.target.value; + this.filterEntries(); + } + + getDefaultPageSize() { + return 20; + } +}