add edit/delete episode seens in episode seen history

This commit is contained in:
X4Lo committed 2024-04-24 13:22:56 +01:00
1 parent dbeb0ee524
commit a3f1b16376
9 files changed
+326 -57

No files matched your search

@@ -14,11 +14,9 @@ import { SeenModalComponent } from '../../../shared/components/modals/seen-modal
import { EntrySeenService } from '../../../core/services/entry-seen.service';
import { EntryRatingsService } from '../../../core/services/entry-ratings.service';
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';
import { SeasonService } from '../../../core/services/season.service';
@Component({
selector: 'app-entry-page',
@@ -58,6 +56,7 @@ export class EntryPageComponent implements OnInit {
public dialog: MatDialog,
private episodeSeenService: EpisodeSeenService,
private entryService: EntryService,
private seasonService: SeasonService,
private entrySeenService: EntrySeenService,
private entryRatingsService: EntryRatingsService,
private datePipe: DatePipe
@@ -67,10 +66,31 @@ 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)
);
console.log(this.entry);
if (this.entry.is_tvseries) {
// ? sorting seasons
this.entry.seasons = this.entry.seasons.sort((a, b) =>
a.name.localeCompare(b.name)
);
// ? getting seaons episodes
this.entry.seasons.forEach((season) => {
this.seasonService.getSeasonEpisodes(season.id).subscribe({
next: (episodes) => {
console.log(
`Loaded Season ${season.season_number} Episodes (${episodes.length})`
);
season.episodes = episodes;
},
error: (error) => {
console.log(`Couldn't fetch season episodes data:`, error);
// TODO show notification
season.episodes = [];
},
});
});
}
if (!this.entry) {
this.router.navigate(['/error'], {
queryParams: { error: 'Entry not found' },
@@ -106,14 +126,11 @@ export class EntryPageComponent implements OnInit {
return;
}
let watchDate = result.watchDate;
let watchedWith = result.watchedWith.split('\n').join(', ');
this.entrySeenService
.createEntrySeen(
this.entry.id.toString(),
result.watchDate,
watchedWith
)
.createEntrySeen(this.entry.id.toString(), watchDate, watchedWith)
.subscribe({
next: (entrySeen) => {
if (entrySeen) {
@@ -151,10 +168,6 @@ export class EntryPageComponent implements OnInit {
});
}
onEpisodeWatchedEvent() {
this.getEpisodesSeenHistory();
}
removeEntrySeen(id: number) {
// TODO add confirm modal
this.entrySeenService.removeEntrySeen(id).subscribe({
@@ -200,6 +213,13 @@ export class EntryPageComponent implements OnInit {
});
}
// ! EPISODE SEEN
// ? episode marked as seen event
onEpisodeWatchedEvent() {
this.getEpisodesSeenHistory();
}
// ? fetch episodes seen history
getEpisodesSeenHistory() {
if (!this.entry.is_tvseries) return;
@@ -233,21 +253,14 @@ export class EntryPageComponent implements OnInit {
this.filterEpisodesSeenHistory();
},
error: (error) => {
this.seenHistory = [] as EpisodeSeenDto[];
this.filterEpisodesSeenHistory();
console.log(error);
},
});
}
// nextPage(): void {
// this.currentPage++;
// }
// previousPage(): void {
// if (this.currentPage > 1) {
// this.currentPage--;
// }
// }
// ? filter episode seen history
filterEpisodesSeenHistory() {
var temp = [] as EpisodeSeenDto[];
@@ -261,11 +274,90 @@ export class EntryPageComponent implements OnInit {
this.seenHistoryToShow = temp.slice(startIndex, endIndex);
}
// totalPageCount(): number {
// return this.seenHistory.length / this.pageSize;
// }
editEpisodeSeen(episodeSeen: EpisodeSeenDto) {
if (episodeSeen == null) return;
// ? pagination
const dialogRef = this.dialog.open(SeenModalComponent, {
width: '400px',
data: {
watchDate: episodeSeen.date_seen,
watchedWith: episodeSeen.seen_with,
},
});
dialogRef.afterClosed().subscribe((result) => {
// ? dialog closed
if (result == null) {
return console.log('Seen dialog was cancelled');
}
// ? dialog confimed
console.log('Episode seen dialog closed with:', result);
if (result.watchDate == '') {
// TODO show no date was passed notification
return console.log(`Episode seen dialog closed with no date.`);
}
episodeSeen.date_seen = result.watchDate;
episodeSeen.seen_with = result.watchedWith.split('\n').join(', ');
this.episodeSeenService.updateEpisodeSeen(episodeSeen).subscribe({
next: () => {
console.log(`Episode seen updated.`);
this.getEpisodesSeenHistory();
// TODO show notification
},
error: (error) => {
console.log(`Episode seen update error: `, error);
// TODO show notification
},
});
});
}
deleteEpisodeSeen(episodeSeen: EpisodeSeenDto) {
if (episodeSeen.id == null) return;
// first start by calling the service and deleting the episode-seen
this.episodeSeenService.removeEpisodeSeen(episodeSeen.id).subscribe({
next: () => {
console.log(`Episode seen deleted.`);
// call this function to fetch the new episode-seen history
this.getEpisodesSeenHistory();
// trying to update the episodes seen progress bar in the season card
// by fetching a new updated episodes list
this.entry.seasons.forEach(async (season) => {
if (season.season_number == episodeSeen.season_number) {
this.seasonService.getSeasonEpisodes(season.id).subscribe({
next: (episodes) => {
console.log(
`Loaded Season ${season.season_number} Episodes (${episodes.length})`
);
season.episodes = episodes;
},
error: (error) => {
console.log(`Couldn't fetch season episodes data:`, error);
// TODO show notification
season.episodes = [];
},
});
}
});
// TODO show notification
},
error: (error) => {
console.log(`Episode seen delete error: `, error);
},
});
}
triggerEpisodeWatchedDeletedEvent() {
console.log(`Emitted Episode Watched Deleted Event`);
}
// + end EPISODE SEEN
// ! pagination
goToPage(page: number) {
this.currentPage = page;
this.filterEpisodesSeenHistory();
@@ -294,7 +386,7 @@ export class EntryPageComponent implements OnInit {
(_, i) => startPage + i
);
}
// ? end pagination
// + end pagination
toggleShowAllSeasons() {
this.showAll = !this.showAll;