From 6c8a60656d7875135f3138074caaa2aa77f21b9d Mon Sep 17 00:00:00 2001 From: X4Lo Date: Mon, 1 Apr 2024 21:21:18 +0000 Subject: [PATCH] add library page component --- .../pages/library/library.component.css | 0 .../pages/library/library.component.html | 29 ++++++ .../pages/library/library.component.spec.ts | 21 +++++ .../pages/library/library.component.ts | 93 +++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 frontend/src/app/features/pages/library/library.component.css create mode 100644 frontend/src/app/features/pages/library/library.component.html create mode 100644 frontend/src/app/features/pages/library/library.component.spec.ts create mode 100644 frontend/src/app/features/pages/library/library.component.ts diff --git a/frontend/src/app/features/pages/library/library.component.css b/frontend/src/app/features/pages/library/library.component.css new file mode 100644 index 0000000..e69de29 diff --git a/frontend/src/app/features/pages/library/library.component.html b/frontend/src/app/features/pages/library/library.component.html new file mode 100644 index 0000000..14032d8 --- /dev/null +++ b/frontend/src/app/features/pages/library/library.component.html @@ -0,0 +1,29 @@ + + + + + \ No newline at end of file diff --git a/frontend/src/app/features/pages/library/library.component.spec.ts b/frontend/src/app/features/pages/library/library.component.spec.ts new file mode 100644 index 0000000..23e4b04 --- /dev/null +++ b/frontend/src/app/features/pages/library/library.component.spec.ts @@ -0,0 +1,21 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { LibraryComponent } from './library.component'; + +describe('LibraryComponent', () => { + let component: LibraryComponent; + let fixture: ComponentFixture; + + beforeEach(() => { + TestBed.configureTestingModule({ + declarations: [LibraryComponent] + }); + fixture = TestBed.createComponent(LibraryComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/features/pages/library/library.component.ts b/frontend/src/app/features/pages/library/library.component.ts new file mode 100644 index 0000000..5751ca1 --- /dev/null +++ b/frontend/src/app/features/pages/library/library.component.ts @@ -0,0 +1,93 @@ +import { Component } from '@angular/core'; +import { Entry } from '../../../shared/models/entry.model'; +import { LibraryService } from '../../../core/services/library.service'; + +@Component({ + selector: 'app-library', + templateUrl: './library.component.html', + styleUrls: ['./library.component.css'], +}) +export class LibraryComponent { + entryType: number = 0; + + allEntries: Entry[] = []; + + entries: Entry[] = []; + + currentPage: number = 1; + pageSize: number = 20; + + constructor(private libraryService: LibraryService) {} + + ngOnInit() { + console.log('initing'); + this.fetchEntries(); + } + + // uses libraryService to get library all content + fetchEntries() { + this.libraryService.getAllEntries().subscribe({ + next: (entries) => { + this.allEntries = entries; + this.filterEntries(); + }, + error: (error) => { + // Handle any errors, possibly informing the user. + }, + }); + } + + 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); + }); + } else { + temp = this.allEntries; + } + + // ? title search + // let len = temp.length; + // for (let i = 0; i < len; i++) { + // if (!temp[i].title.toLocaleLowerCase().includes('od')) { + // temp.splice(i); + // len--; + // } + // } + + const startIndex = (this.currentPage - 1) * this.pageSize; + const endIndex = startIndex + this.pageSize; + + this.entries = temp.slice(startIndex, endIndex); + } + + // ? pagination + 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 + ); + } + + // ? setter + setEntryType(entryType: number) { + this.entryType = entryType; + this.filterEntries(); + } + + setPageSize(pageSize: number) { + this.pageSize = pageSize; + } +}