add episode seen history in entry page

This commit is contained in:
X4Lo committed 2024-04-22 17:41:30 +01:00
1 parent cf305cabae
commit dbeb0ee524
11 files changed
+288 -64

No files matched your search

@@ -1,10 +1,11 @@
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { trigger, state, style, transition, animate } from '@angular/animations';
import { trigger, style, transition, animate } from '@angular/animations';
import { ActivatedRoute, Router } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { Entry } from '../../../shared/models/entry.model';
import { EpisodeSeenDto } from '../../../shared/models/dtos/episode-seen.dto';
import { DatePipe } from '@angular/common';
@@ -16,6 +17,8 @@ import { EntryRatingsService } from '../../../core/services/entry-ratings.servic
import { TmdbService } from '../../../core/services/tmdb.service';
import { LibraryService } from '../../../core/services/library.service';
import { EntryService } from '../../../core/services/entry.service';
import { EpisodeSeenService } from '../../../core/services/episode-seen.service';
import e from 'express';
@Component({
selector: 'app-entry-page',
@@ -24,14 +27,16 @@ import { EntryService } from '../../../core/services/entry.service';
encapsulation: ViewEncapsulation.None,
animations: [
trigger('fadeInOut', [
transition(':enter', [ // void => *
transition(':enter', [
// void => *
style({ opacity: 0 }),
animate('0.5s ease-in', style({ opacity: 1 }))
animate('0.5s ease-in', style({ opacity: 1 })),
]),
transition(':leave', [ // * => void
animate('0.5s ease-out', style({ opacity: 0 }))
])
])
transition(':leave', [
// * => void
animate('0.5s ease-out', style({ opacity: 0 })),
]),
]),
],
})
export class EntryPageComponent implements OnInit {
@@ -39,12 +44,19 @@ export class EntryPageComponent implements OnInit {
public showAll = false;
public initialCount = 2;
seenHistory: any[] = [];
seenHistoryToShow: any[] = [];
itemsCount: number = 0;
maxPage: number = 10;
currentPage: number = 1;
pageSize: number = 10;
constructor(
private route: ActivatedRoute,
private router: Router,
public dialog: MatDialog,
private tmdbService: TmdbService,
private libraryService: LibraryService,
private episodeSeenService: EpisodeSeenService,
private entryService: EntryService,
private entrySeenService: EntrySeenService,
private entryRatingsService: EntryRatingsService,
@@ -55,7 +67,9 @@ export class EntryPageComponent implements OnInit {
this.route.data.subscribe(
(data) => {
this.entry = data['entry'] as Entry;
this.entry.seasons = this.entry.seasons.sort((a, b) => a.name.localeCompare(b.name));
this.entry.seasons = this.entry.seasons.sort((a, b) =>
a.name.localeCompare(b.name)
);
console.log(this.entry);
if (!this.entry) {
this.router.navigate(['/error'], {
@@ -70,9 +84,11 @@ export class EntryPageComponent implements OnInit {
});
}
);
this.getEpisodesSeenHistory();
}
openDialog() {
openEntrySeenModal() {
if (!this.entry) return;
const dialogRef = this.dialog.open(SeenModalComponent, {
@@ -135,6 +151,10 @@ export class EntryPageComponent implements OnInit {
});
}
onEpisodeWatchedEvent() {
this.getEpisodesSeenHistory();
}
removeEntrySeen(id: number) {
// TODO add confirm modal
this.entrySeenService.removeEntrySeen(id).subscribe({
@@ -180,6 +200,102 @@ export class EntryPageComponent implements OnInit {
});
}
getEpisodesSeenHistory() {
if (!this.entry.is_tvseries) return;
this.episodeSeenService.getEpisodeSeenByEntryId(this.entry.id).subscribe({
next: (data) => {
let episodeSeens = [] as EpisodeSeenDto[];
// mapping
data.forEach((item) => {
let e = {
id: item.id,
entryId: item.entry_id,
episodeId: item.episode_id,
season_number: item.episode.season.season_number,
episode_number: item.episode.episode_number,
date_seen: item.date_seen,
seen_with: item.seen_with,
createdAt: item.createdAt,
updatedAt: item.updatedAt,
} as EpisodeSeenDto;
episodeSeens.push(e);
});
this.seenHistory = episodeSeens;
console.log(`Episode Seen History Received: `, episodeSeens);
this.filterEpisodesSeenHistory();
},
error: (error) => {
console.log(error);
},
});
}
// nextPage(): void {
// this.currentPage++;
// }
// previousPage(): void {
// if (this.currentPage > 1) {
// this.currentPage--;
// }
// }
filterEpisodesSeenHistory() {
var temp = [] as EpisodeSeenDto[];
temp = this.seenHistory;
const startIndex = (this.currentPage - 1) * this.pageSize;
const endIndex = startIndex + this.pageSize;
this.itemsCount = temp.length;
this.maxPage = Math.ceil(this.itemsCount / this.pageSize);
this.seenHistoryToShow = temp.slice(startIndex, endIndex);
}
// totalPageCount(): number {
// return this.seenHistory.length / this.pageSize;
// }
// ? pagination
goToPage(page: number) {
this.currentPage = page;
this.filterEpisodesSeenHistory();
}
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
toggleShowAllSeasons() {
this.showAll = !this.showAll;
}