implemented advanced library filter
This commit is contained in:
1 parent
7fc1bdd875
commit
ea5289dc67
5 files changed
+493
-125
No files matched your search
@@ -5,15 +5,13 @@ import { catchError, switchMap, tap } from 'rxjs/operators';
|
||||
import { Entry } from '../../shared/models/entry.model';
|
||||
import { EntryId } from '../../shared/models/entryId.model';
|
||||
import { EntryRating } from '../../shared/models/entryRating.model';
|
||||
// import { environment } from '../../../environments/environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class LibraryService implements OnInit {
|
||||
// libraryEntries: Entry[] = [];
|
||||
private libraryEntriesSubject = new ReplaySubject<Entry[]>(1);
|
||||
private entriesLoaded = false; // Flag to track if the entries have been loaded
|
||||
private entriesLoaded = false;
|
||||
|
||||
private backend_apiUrl = ' http://localhost:4000/api/v1';
|
||||
|
||||
@@ -27,28 +25,18 @@ export class LibraryService implements OnInit {
|
||||
if (this.entriesLoaded && !forceFetch) {
|
||||
return this.libraryEntriesSubject.asObservable();
|
||||
} else {
|
||||
// Note: This now properly chains the observable without directly subscribing here.
|
||||
return this.fetchAllEntries(forceFetch);
|
||||
}
|
||||
}
|
||||
|
||||
// public refreshEntries(forceFetch: boolean = true): Observable<Entry[]> {
|
||||
// return this.getAllEntries(forceFetch);
|
||||
// }
|
||||
public refrechEntries() {
|
||||
return this.fetchAllEntries(true);
|
||||
}
|
||||
|
||||
// private apiUrl = environment.apiBaseUrl;
|
||||
|
||||
/**
|
||||
* Fetches data from the specified endpoint and returns an Observable.
|
||||
* @param {string} endPoint The URL of the API endpoint to fetch data from.
|
||||
* @param {Object} params Optional query parameters to append to the request URL.
|
||||
* @returns {Observable<any>} An Observable that emits the fetched data.
|
||||
*/
|
||||
fetchData(
|
||||
endPoint: string,
|
||||
params?: { [param: string]: string | number }
|
||||
): Observable<any> {
|
||||
// Initialize HttpParams from the provided params object
|
||||
let httpParams = new HttpParams();
|
||||
if (params) {
|
||||
Object.keys(params).forEach((key) => {
|
||||
@@ -75,7 +63,6 @@ export class LibraryService implements OnInit {
|
||||
this.entriesLoaded = true;
|
||||
}),
|
||||
catchError((error) => {
|
||||
// Log and rethrow the error for the caller to handle.
|
||||
console.error('Failed to fetch entries:', error);
|
||||
this.entriesLoaded = false; // Allow for a retry on subsequent calls.
|
||||
return throwError(() => new Error('Failed to fetch entries'));
|
||||
@@ -84,36 +71,6 @@ export class LibraryService implements OnInit {
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Fetches library data with page and pagesize and returns an Observable.
|
||||
// * @param {string} endPoint The URL of the API endpoint to fetch data from.
|
||||
// * @param {Object} params Optional query parameters to append to the request URL.
|
||||
// * @returns {Observable<Entry[]>} An Observable that emits the fetched data.
|
||||
// */
|
||||
// getEntries(page: number, pageSize: number): Observable<Entry[]> {
|
||||
// var endPoint = `${this.backend_apiUrl}/entries`;
|
||||
// return this.fetchData(endPoint, { page: page, pageSize: pageSize }).pipe(
|
||||
// map((response) =>
|
||||
// response.results.map((movie: any) => this.mapMovieToEntry(movie))
|
||||
// )
|
||||
// );
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Retrieves a specific page of entries from the library.
|
||||
// *
|
||||
// * @param page The page number to retrieve.
|
||||
// * @param pageSize Defines the number of entries on each page.
|
||||
// * @returns An Observable emitting the requested page of entries.
|
||||
// */
|
||||
// getEntriesUsingEndPoint(endPoint: string): Observable<Entry[]> {
|
||||
// return this.fetchData(endPoint).pipe(
|
||||
// map((response) =>
|
||||
// response.results.map((movie: any) => this.mapMovieToEntry(movie))
|
||||
// )
|
||||
// );
|
||||
// }
|
||||
|
||||
addEntry(tmdb_id: string, is_tvserie: boolean): Observable<Entry | null> {
|
||||
return this.getAllEntries().pipe(
|
||||
switchMap((entries) => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* ! buttons style */
|
||||
.custom-group .form-select {
|
||||
border-top-right-radius: 0;
|
||||
border-bottom-right-radius: 0;
|
||||
@@ -33,4 +34,31 @@
|
||||
|
||||
#single-select-field {
|
||||
max-width: 120px;
|
||||
}
|
||||
|
||||
.align-items-baseline {
|
||||
align-items: baseline;
|
||||
}
|
||||
|
||||
/* ! filter switches style */
|
||||
.form-check-label {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
a.clear-filter-link {
|
||||
display: inline-block;
|
||||
padding: 0.375rem 0;
|
||||
}
|
||||
|
||||
.filter-switches {
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
/* ! filter title, year, genre */
|
||||
.select2 .select2-container .select2-container--default {
|
||||
height: 50px !important;
|
||||
}
|
||||
|
||||
.select2 .select2-container .select2-container--default .selection span .select2-selection .select2-selection--multiple {
|
||||
padding: 0 !important;
|
||||
}
|
||||
@@ -12,48 +12,115 @@
|
||||
</ol>
|
||||
</nav>
|
||||
</div>
|
||||
<div class="ms-auto">
|
||||
<div class="input-group custom-group">
|
||||
<div class="input-group-text">Show</div>
|
||||
<select class="form-select" id="single-select-field" data-placeholder="Choose one thing" style="max-width: 150px;"
|
||||
[(ngModel)]="pageSize" (change)="setPageSize($event)">
|
||||
<option value="20">20</option>
|
||||
<option value="50">50</option>
|
||||
<option value="100">100</option>
|
||||
<option value="250">250</option>
|
||||
<option value="500">500</option>
|
||||
<option value="1000">1000</option>
|
||||
</select>
|
||||
</div>
|
||||
<!--end breadcrumb-->
|
||||
|
||||
<div class="btn-group">
|
||||
<button type="button" class="btn btn-outline-dark left-btn" [class.active]="entryType === 0"
|
||||
(click)="setEntryType(0)">All</button>
|
||||
<button type="button" class="btn btn-outline-dark" [class.active]="entryType === 1"
|
||||
(click)="setEntryType(1)">Movies</button>
|
||||
<button type="button" class="btn btn-outline-dark right-btn" [class.active]="entryType === 2"
|
||||
(click)="setEntryType(2)">TV-Series</button>
|
||||
|
||||
<!-- filter -->
|
||||
|
||||
<div class="d-flex align-items-baseline gap-3 flex-wrap">
|
||||
<div class="product-count d-flex align-items-center gap-3 gap-lg-4 mb-4 fw-medium flex-wrap font-text1">
|
||||
<a href="javascript:;" (click)="onEntryTypeChange(0)"><span class="me-1">All</span><span
|
||||
class="text-secondary">({{entries.length}})</span></a>
|
||||
<a href="javascript:;" (click)="onEntryTypeChange(1)"><span class="me-1">Movies</span><span
|
||||
class="text-secondary">({{moviesCount()}})</span></a>
|
||||
<a href="javascript:;" (click)="onEntryTypeChange(2)"><span class="me-1">Tv-Series</span><span
|
||||
class="text-secondary">({{seriesCount()}})</span></a>
|
||||
<a href="javascript:;" (click)="setFavorit()"><span class="me-1">Favorit</span><span
|
||||
class="text-secondary">({{favoritCount()}})</span></a>
|
||||
<a href="javascript:;" (click)="setHidden()"><span class="me-1">Hidden</span><span
|
||||
class="text-secondary">({{hiddenCount()}})</span></a>
|
||||
</div>
|
||||
<!-- filter switches -->
|
||||
<div class="d-flex align-items-baseline gap-3 flex-wrap filter-switches">
|
||||
<!-- Favorite -->
|
||||
<div class="form-check form-switch">
|
||||
<input class="form-check-input" type="checkbox" role="switch" id="favoritSwitch"
|
||||
[(ngModel)]="filterCriteria.favorit" (ngModelChange)="onFilterChange()">
|
||||
<label class="form-check-label" for="favoritSwitch">Favorit</label>
|
||||
</div>
|
||||
|
||||
<!-- Hidden -->
|
||||
<div class="form-check form-switch form-check-danger">
|
||||
<input class="form-check-input" type="checkbox" role="switch" id="hiddenSwitch"
|
||||
[(ngModel)]="filterCriteria.hidden" (ngModelChange)="onFilterChange()">
|
||||
<label class="form-check-label" for="hiddenSwitch">Show Hidden</label>
|
||||
</div>
|
||||
|
||||
<!-- Match All Criteria -->
|
||||
<div class="form-check form-switch">
|
||||
<input class="form-check-input" type="checkbox" role="switch" id="matchAllSwitch"
|
||||
[(ngModel)]="filterCriteria.matchAll" (ngModelChange)="onFilterChange()">
|
||||
<label class="form-check-label" for="matchAllSwitch">Match All Criteria</label>
|
||||
</div>
|
||||
|
||||
<!-- Clear Filter Link -->
|
||||
<a href="javascript:;" class="clear-filter-link" (click)="clearFilter()">Clear Filter</a>
|
||||
</div>
|
||||
<!-- filter switches END -->
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<div class="containder">
|
||||
<div class="row">
|
||||
<!-- Search Input for Title -->
|
||||
<div class="col-sm-4">
|
||||
<div class="input-group">
|
||||
<span class="input-group-text material-icons-outlined">search</span>
|
||||
<input class="form-control" type="search" placeholder="Title..." [(ngModel)]="filterCriteria.title"
|
||||
(ngModelChange)="onFilterChange()">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Year Selector -->
|
||||
<div class="col-sm-4">
|
||||
<div class="d-flex justify-content-start align-items-center">
|
||||
<select class="form-control select2" id="year-select" multiple (change)="onYearsChange($event)">
|
||||
<option *ngFor="let year of years" [value]="year">{{ year }}</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Genre Selector -->
|
||||
<div class="col-sm-4">
|
||||
<div class="d-flex justify-content-start align-items-center">
|
||||
<select class="form-control select2" id="genre-select" multiple (change)="onGenresChange($event)">
|
||||
<option *ngFor="let genre of genres" [value]="genre">{{ genre }}</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!--end breadcrumb-->
|
||||
<!-- filter end -->
|
||||
|
||||
<br>
|
||||
|
||||
<!-- Pagination -->
|
||||
<div class="pagination-container d-flex justify-content-end" *ngIf="maxPage != 1 && entries.length != 0">
|
||||
<ul class="pagination pagination-sm">
|
||||
<li class="page-item" [class.disabled]="currentPage === 1">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a>
|
||||
</li>
|
||||
<!-- Dynamically generate page numbers -->
|
||||
<li class="page-item" *ngFor="let page of pageNumbers" [class.active]="currentPage === page">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(page)">{{ page }}</a>
|
||||
</li>
|
||||
<li class="page-item" [class.disabled]="currentPage == maxPage">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage + 1)">Next</a>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="row mb-2">
|
||||
<div class="col-sm-12 d-flex justify-content-between align-items-center" style="max-height: 40px;">
|
||||
<div class="dataTables_info" role="status" aria-live="polite">
|
||||
Total entries {{entriesCount}}
|
||||
</div>
|
||||
<div class="pagination-container d-flex justify-content-end" *ngIf="maxPage != 1 && entries.length != 0">
|
||||
<ul class="pagination pagination-sm">
|
||||
<li class="page-item" [class.disabled]="currentPage === 1">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a>
|
||||
</li>
|
||||
<!-- Dynamically generate page numbers -->
|
||||
<li class="page-item" *ngFor="let page of pageNumbers" [class.active]="currentPage === page">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(page)">{{ page }}</a>
|
||||
</li>
|
||||
<li class="page-item" [class.disabled]="currentPage == maxPage">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage + 1)">Next</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<app-dynamic-entry-list [entries]="entriesToShow" (removedEvent)="entryRemoved($event)"></app-dynamic-entry-list>
|
||||
|
||||
|
||||
|
||||
@@ -1,42 +1,101 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { AfterViewInit, Component, OnDestroy, OnInit } from '@angular/core';
|
||||
import { Entry } from '../../shared/models/entry.model';
|
||||
import { LibraryService } from '../../core/services/library.service';
|
||||
import { Event, NavigationEnd, Router } from '@angular/router';
|
||||
import { ActivatedRoute, Event, NavigationEnd, Router } from '@angular/router';
|
||||
import { LibraryQueryParams } from '../../shared/models/LibraryQueryParams';
|
||||
|
||||
declare var $: any;
|
||||
@Component({
|
||||
selector: 'app-library',
|
||||
templateUrl: './library.component.html',
|
||||
styleUrls: ['./library.component.css'],
|
||||
})
|
||||
export class LibraryComponent {
|
||||
entryType: number = 0;
|
||||
|
||||
export class LibraryComponent implements OnInit, AfterViewInit {
|
||||
entries: Entry[] = [];
|
||||
entriesToShow: Entry[] = [];
|
||||
|
||||
// ? pagination
|
||||
entriesCount: number = 0;
|
||||
maxPage: number = 10;
|
||||
currentPage: number = 1;
|
||||
pageSize: number = 10;
|
||||
|
||||
constructor(private router: Router, private libraryService: LibraryService) {}
|
||||
// ? filter
|
||||
years: number[] = [];
|
||||
genres: string[] = [
|
||||
'Action & Adventure',
|
||||
'Animation',
|
||||
'Comedy',
|
||||
'Crime',
|
||||
'Documentary',
|
||||
'Drama',
|
||||
'Family',
|
||||
'Kids',
|
||||
'Mystery',
|
||||
'News',
|
||||
'Reality',
|
||||
'Sci-Fi & Fantasy',
|
||||
'Soap',
|
||||
'Talk',
|
||||
'War & Politics',
|
||||
'Western',
|
||||
'Action',
|
||||
'Adventure',
|
||||
'Fantasy',
|
||||
'History',
|
||||
'Horror',
|
||||
'Music',
|
||||
'Romance',
|
||||
'Science Fiction',
|
||||
'TV Movie',
|
||||
'Thriller',
|
||||
'War',
|
||||
];
|
||||
|
||||
ngOnInit() {
|
||||
this.pageSize = this.getDefaultPageSize();
|
||||
this.getEntries(true);
|
||||
filterCriteria: {
|
||||
title?: string;
|
||||
type: number;
|
||||
years?: number[];
|
||||
genres?: string[];
|
||||
favorit: boolean;
|
||||
watchlist: boolean;
|
||||
hidden: boolean;
|
||||
matchAll: boolean;
|
||||
} = {
|
||||
title: '',
|
||||
type: 0,
|
||||
years: [],
|
||||
genres: [],
|
||||
favorit: false,
|
||||
watchlist: false,
|
||||
hidden: false,
|
||||
matchAll: true,
|
||||
};
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
private route: ActivatedRoute,
|
||||
private libraryService: LibraryService
|
||||
) {}
|
||||
|
||||
async ngOnInit() {
|
||||
this.pageSize = await this.getDefaultPageSize();
|
||||
await this.getEntries(true);
|
||||
// this.router.events.subscribe((event) => {
|
||||
// if (event instanceof NavigationEnd && event.url === '/library') {
|
||||
// this.pageSize = this.getDefaultPageSize();
|
||||
// this.getEntries(true);
|
||||
// }
|
||||
// });
|
||||
await this.populateYears();
|
||||
}
|
||||
|
||||
// ? uses libraryService to get library all entries
|
||||
getEntries(forceFetch: boolean = false) {
|
||||
this.libraryService.getAllEntries(forceFetch).subscribe({
|
||||
next: (entries) => {
|
||||
this.entries = entries;
|
||||
this.entries = entries.filter((e) => !e.watchlist);
|
||||
this.getQueryParameters();
|
||||
this.filterEntries();
|
||||
},
|
||||
error: (error) => {
|
||||
@@ -46,24 +105,198 @@ export class LibraryComponent {
|
||||
});
|
||||
}
|
||||
|
||||
getQueryParameters() {
|
||||
this.route.queryParams.subscribe((params) => {
|
||||
this.currentPage = params['page'] ? parseInt(params['page'], 10) : 1;
|
||||
this.filterCriteria.title = params['title'] || '';
|
||||
this.filterCriteria.favorit = params.hasOwnProperty('favorit')
|
||||
? params['favorit']
|
||||
: false;
|
||||
this.filterCriteria.years = params['years']
|
||||
? params['years'].split(',')
|
||||
: [];
|
||||
this.filterCriteria.genres = params['genres']
|
||||
? params['genres'].split(',')
|
||||
: [];
|
||||
this.filterCriteria.type = params.hasOwnProperty('type')
|
||||
? params['type']
|
||||
: 0;
|
||||
this.filterCriteria.hidden = params.hasOwnProperty('hidden')
|
||||
? params['hidden']
|
||||
: false;
|
||||
this.filterCriteria.matchAll = params.hasOwnProperty('matchAll')
|
||||
? params['matchAll']
|
||||
: true;
|
||||
});
|
||||
}
|
||||
|
||||
setQueryParameters() {
|
||||
let params = { page: this.currentPage } as LibraryQueryParams;
|
||||
|
||||
if (this.filterCriteria.title?.trim() != '') {
|
||||
params.title = this.filterCriteria.title?.trim();
|
||||
} else {
|
||||
delete params.title;
|
||||
}
|
||||
|
||||
if (this.filterCriteria.years?.length) {
|
||||
params.years = this.filterCriteria.years.join(',');
|
||||
} else {
|
||||
delete params.years;
|
||||
}
|
||||
|
||||
if (this.filterCriteria.genres?.length) {
|
||||
params.genres = this.filterCriteria.genres.join(',');
|
||||
} else {
|
||||
delete params.genres;
|
||||
}
|
||||
|
||||
if (this.filterCriteria.type != 0) {
|
||||
params.type = this.filterCriteria.type;
|
||||
} else {
|
||||
delete params.type;
|
||||
}
|
||||
|
||||
if (this.filterCriteria.favorit != false) {
|
||||
params.favorit = this.filterCriteria.favorit;
|
||||
} else {
|
||||
delete params.favorit;
|
||||
}
|
||||
|
||||
if (this.filterCriteria.hidden != false) {
|
||||
params.hidden = this.filterCriteria.hidden;
|
||||
} else {
|
||||
delete params.hidden;
|
||||
}
|
||||
|
||||
if (this.filterCriteria.matchAll != true) {
|
||||
params.matchAll = this.filterCriteria.matchAll;
|
||||
} else {
|
||||
delete params.matchAll;
|
||||
}
|
||||
|
||||
this.router.navigate([], {
|
||||
relativeTo: this.route,
|
||||
queryParams: params,
|
||||
queryParamsHandling: '',
|
||||
});
|
||||
}
|
||||
|
||||
// ! FILTER
|
||||
ngAfterViewInit() {
|
||||
$('#year-select')
|
||||
.select2({
|
||||
placeholder: 'Year(s)...',
|
||||
allowClear: true,
|
||||
closeOnSelect: false,
|
||||
})
|
||||
.on('change', (event: { target: any }) => {
|
||||
this.onYearsChange(event);
|
||||
});
|
||||
|
||||
$('#genre-select')
|
||||
.select2({
|
||||
placeholder: 'Genre(s)...',
|
||||
allowClear: true,
|
||||
closeOnSelect: false,
|
||||
})
|
||||
.on('change', (event: { target: any }) => {
|
||||
this.onGenresChange(event);
|
||||
});
|
||||
}
|
||||
|
||||
populateYears() {
|
||||
const currentYear = new Date().getFullYear();
|
||||
for (let year = currentYear; year >= 1950; year--) {
|
||||
this.years.push(year);
|
||||
}
|
||||
}
|
||||
|
||||
initializeSelect2() {
|
||||
setTimeout(() => {
|
||||
// Ensure that the DOM is fully loaded
|
||||
$('.select2').select2({
|
||||
placeholder: 'Select an option',
|
||||
allowClear: true,
|
||||
});
|
||||
}, 500);
|
||||
}
|
||||
|
||||
clearFilter() {
|
||||
this.filterCriteria = {
|
||||
title: '',
|
||||
type: 0,
|
||||
years: [],
|
||||
genres: [],
|
||||
favorit: false,
|
||||
watchlist: false,
|
||||
hidden: false,
|
||||
matchAll: true,
|
||||
};
|
||||
|
||||
this.setQueryParameters();
|
||||
this.filterEntries();
|
||||
}
|
||||
|
||||
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 formattedTitle =
|
||||
this.filterCriteria.title?.toLowerCase().trim() || '';
|
||||
temp = await this.entries.filter((entry) => {
|
||||
// Type check
|
||||
let filterType = this.filterCriteria.type - 1 == 0 ? false : true;
|
||||
if (this.filterCriteria.type != 0 && filterType != entry.is_tvseries) {
|
||||
return false; // Skip entry if the type does not match, unless type is 'all' (0)
|
||||
}
|
||||
|
||||
// Hidden check
|
||||
// skip hidden if show hidden is not check
|
||||
if (!this.filterCriteria.hidden && entry.hidden) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const year = new Date(entry.release_date).getFullYear();
|
||||
// ! Match all criteria logic
|
||||
if (this.filterCriteria.matchAll) {
|
||||
return (
|
||||
(formattedTitle
|
||||
? entry.title.toLowerCase().includes(formattedTitle) ||
|
||||
entry.original_title.toLowerCase().includes(formattedTitle)
|
||||
: true) &&
|
||||
(this.filterCriteria.genres?.length
|
||||
? this.filterCriteria.genres.every((genre) =>
|
||||
entry.genres.includes(genre)
|
||||
)
|
||||
: true) &&
|
||||
(this.filterCriteria.years?.length
|
||||
? this.filterCriteria.years.includes(year)
|
||||
: true) &&
|
||||
(this.filterCriteria.favorit ? entry.favorit : true) &&
|
||||
(this.filterCriteria.watchlist ? entry.watchlist : true) &&
|
||||
(this.filterCriteria.hidden ? entry.hidden : true)
|
||||
);
|
||||
}
|
||||
|
||||
// ! Match at least one criteria logic
|
||||
return (
|
||||
(formattedTitle
|
||||
? entry.title.toLowerCase().includes(formattedTitle) ||
|
||||
entry.original_title.toLowerCase().includes(formattedTitle)
|
||||
: true) ||
|
||||
(this.filterCriteria.genres?.length &&
|
||||
this.filterCriteria.genres.some((genre) =>
|
||||
entry.genres.includes(genre)
|
||||
)) ||
|
||||
(this.filterCriteria.years?.length &&
|
||||
this.filterCriteria.years.includes(year)) ||
|
||||
(this.filterCriteria.favorit && entry.favorit) ||
|
||||
(this.filterCriteria.watchlist && entry.watchlist)
|
||||
);
|
||||
});
|
||||
|
||||
// ? sort filter
|
||||
temp = temp.sort(
|
||||
(a, b) =>
|
||||
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
|
||||
);
|
||||
temp = await this.sortEntries(temp);
|
||||
|
||||
this.entriesCount = temp.length;
|
||||
this.maxPage = Math.ceil(this.entriesCount / this.pageSize);
|
||||
@@ -74,20 +307,112 @@ export class LibraryComponent {
|
||||
|
||||
this.entriesToShow = temp.slice(startIndex, endIndex);
|
||||
}
|
||||
// + FILTER END
|
||||
|
||||
// ! sorting
|
||||
sortEntries(entries: Entry[]) {
|
||||
return entries.sort(
|
||||
(a, b) =>
|
||||
new Date(b.date_last_seen).getTime() -
|
||||
new Date(a.date_last_seen).getTime()
|
||||
);
|
||||
}
|
||||
|
||||
// ! calculated fields
|
||||
moviesCount() {
|
||||
return this.entries.filter((e) => !e.is_tvseries).length;
|
||||
}
|
||||
|
||||
seriesCount() {
|
||||
return this.entries.filter((e) => e.is_tvseries).length;
|
||||
}
|
||||
|
||||
favoritCount() {
|
||||
return this.entries.filter((e) => e.favorit).length;
|
||||
}
|
||||
hiddenCount() {
|
||||
return this.entries.filter((e) => e.hidden).length;
|
||||
}
|
||||
|
||||
// ! events
|
||||
entryRemoved(event: Event) {
|
||||
this.entries = this.entries.filter(
|
||||
(e) => e.id.toString() != event.toString()
|
||||
);
|
||||
this.pageSize = this.getDefaultPageSize();
|
||||
this.filterEntries();
|
||||
// this.getEntries(true);
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
// * filter events
|
||||
onYearsChange(event: any) {
|
||||
this.currentPage = 1;
|
||||
|
||||
const selected = $(event.target).val();
|
||||
this.filterCriteria.years = Array.isArray(selected)
|
||||
? selected.map(Number)
|
||||
: [Number(selected)];
|
||||
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
onGenresChange(event: any) {
|
||||
this.currentPage = 1;
|
||||
|
||||
const selected = $(event.target).val();
|
||||
this.filterCriteria.genres = Array.isArray(selected) ? selected : [];
|
||||
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
onFilterChange() {
|
||||
this.currentPage = 1;
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
onEntryTypeChange(entryType: number) {
|
||||
this.filterCriteria.type = entryType;
|
||||
|
||||
this.currentPage = 1;
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
setFavorit() {
|
||||
this.filterCriteria.favorit = !this.filterCriteria.favorit;
|
||||
|
||||
this.currentPage = 1;
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
setHidden() {
|
||||
this.filterCriteria.hidden = !this.filterCriteria.hidden;
|
||||
|
||||
this.currentPage = 1;
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
// + end filter event
|
||||
|
||||
// ! pagination
|
||||
setPageSize(event: any) {
|
||||
this.pageSize = event.target.value;
|
||||
this.filterEntries();
|
||||
}
|
||||
|
||||
getDefaultPageSize() {
|
||||
// TODO get default page size from settings service
|
||||
return 54;
|
||||
}
|
||||
|
||||
// ? pagination
|
||||
goToPage(page: number) {
|
||||
this.currentPage = page;
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
get pageNumbers(): number[] {
|
||||
@@ -113,24 +438,4 @@ export class LibraryComponent {
|
||||
(_, 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();
|
||||
}
|
||||
|
||||
// ? getters
|
||||
getDefaultPageSize() {
|
||||
// TODO get default page size from settings service
|
||||
return 20;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
export interface LibraryQueryParams {
|
||||
page: number;
|
||||
title?: string;
|
||||
type?: number;
|
||||
years?: string;
|
||||
genres?: string;
|
||||
favorit?: boolean;
|
||||
watchlist?: boolean;
|
||||
hidden?: boolean;
|
||||
matchAll?: boolean;
|
||||
}
|
||||
Reference in new issue
Block a user