add confirm delete for ep/entry seen + implemented actionsLock + fixed seasons order

This commit is contained in:
X4Lo committed 2024-05-11 05:33:30 +01:00
1 parent 01e79b262a
commit 5807652843
3 files changed
+189 -119

No files matched your search

@@ -7,16 +7,17 @@ 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';
import { UserCollectionsModalComponent } from '../../../shared/components/modals/list-selection-modal/user-collections-modal.component';
import { SeenModalComponent } from '../../../shared/components/modals/seen-modal/seen-modal.component';
import { ConfirmModalComponent } from '../../../shared/components/modals/confirm-modal/confirm-modal.component';
import { EntrySeenService } from '../../../core/services/entry-seen.service';
import { EntryRatingsService } from '../../../core/services/entry-ratings.service';
import { EntryService } from '../../../core/services/entry.service';
import { EpisodeSeenService } from '../../../core/services/episode-seen.service';
import { SeasonService } from '../../../core/services/season.service';
import { LibraryService } from '../../../core/services/library.service';
import { ToastrService } from 'ngx-toastr';
@Component({
selector: 'app-entry-page',
@@ -42,6 +43,8 @@ export class EntryPageComponent implements OnInit {
public showAll = false;
public initialCount = 2;
lockActions: boolean = false;
seenHistory: any[] = [];
seenHistoryToShow: any[] = [];
@@ -51,15 +54,16 @@ export class EntryPageComponent implements OnInit {
pageSize: number = 10;
constructor(
private route: ActivatedRoute,
private router: Router,
public dialog: MatDialog,
private episodeSeenService: EpisodeSeenService,
private entryService: EntryService,
private seasonService: SeasonService,
private entrySeenService: EntrySeenService,
private entryRatingsService: EntryRatingsService,
private datePipe: DatePipe
private libraryService: LibraryService,
private dialog: MatDialog,
private route: ActivatedRoute,
private router: Router,
private toastr: ToastrService
) {}
ngOnInit(): void {
@@ -69,22 +73,33 @@ export class EntryPageComponent implements OnInit {
if (this.entry.is_tvseries) {
// ? sorting seasons
this.entry.seasons = this.entry.seasons.sort((a, b) =>
a.name.localeCompare(b.name)
);
this.entry.seasons = this.entry.seasons.sort((a, b) => {
if (b.season_number == 0) b.season_number = 30; // this will make the special season go last
if (a.season_number == 0) a.season_number = 30; // this will make the special season go last
return a.season_number - b.season_number;
});
// ? 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})`
);
// 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
this.toastr.success(
'Error',
`Couldn't get season ${season.season_number} episodes.`,
{
tapToDismiss: true,
progressBar: true,
progressAnimation: 'increasing',
}
);
season.episodes = [];
},
});
@@ -115,11 +130,9 @@ export class EntryPageComponent implements OnInit {
width: '360px',
});
console.log('Seen dialog opened');
dialogRef.afterClosed().subscribe((result) => {
if (result) {
console.log('Seen dialog closed with:', result);
// console.log('Seen dialog closed with:', result);
if (result.watchDate == '') {
// TODO show no was passed date notification
@@ -135,8 +148,12 @@ export class EntryPageComponent implements OnInit {
next: (entrySeen) => {
if (entrySeen) {
// TODO show confirm notification
console.log(`EntrySeen Added`, entrySeen);
// console.log(`EntrySeen Added`, entrySeen);
this.entry.entrySeen.push(entrySeen);
if (this.entry.watchlist) {
this.libraryService.refrechEntries().subscribe();
}
}
},
error: (error) => {
@@ -169,31 +186,72 @@ export class EntryPageComponent implements OnInit {
}
removeEntrySeen(id: number) {
// TODO add confirm modal
this.entrySeenService.removeEntrySeen(id).subscribe({
next: () => {
this.entry.entrySeen = this.entry.entrySeen.filter(
(item) => item.id !== id
);
},
error: (error) => {
// TODO show notification
console.error('Error deleting entrySeen:', error);
const dialogRef = this.dialog.open(ConfirmModalComponent, {
width: '400px',
data: {
title: 'Confirmation',
message: `Are you sure you want to delete this?`,
type: 'danger',
},
});
dialogRef.afterClosed().subscribe((result) => {
if (result) {
if (result.confirmed != true) return;
this.entrySeenService.removeEntrySeen(id).subscribe({
next: () => {
this.toastr.success('success', `Removed successfuly.`, {
tapToDismiss: true,
progressBar: true,
progressAnimation: 'increasing',
});
this.entry.entrySeen = this.entry.entrySeen.filter(
(item) => item.id !== id
);
},
error: (error) => {
// TODO show notification
console.error('Error deleting entrySeen:', error);
},
});
}
});
}
toggleFavorite() {
this.lockActions = true;
this.entry.favorit = !this.entry.favorit;
this.entryService.updateEntry(this.entry).subscribe({
next: (entry) => {
console.log(`Entry updated: `, entry);
// console.log(`Entry updated: `, entry);
this.entry = entry;
},
error: (error) => {
console.log(`Error updating entry: `, error);
this.entry.favorit = !this.entry.favorit;
},
complete: () => {
this.lockActions = false;
},
});
}
toggleHidden() {
this.lockActions = true;
this.entry.hidden = !this.entry.hidden;
this.entryService.updateEntry(this.entry).subscribe({
next: (entry) => {
// console.log(`Entry updated: `, entry);
this.entry = entry;
},
error: (error) => {
console.log(`Error updating entry: `, error);
this.entry.favorit = !this.entry.favorit;
},
complete: () => {
this.lockActions = false;
}
});
}
@@ -214,16 +272,20 @@ export class EntryPageComponent implements OnInit {
}
refrechEntry() {
this.lockActions = true;
this.entryService.refrechEntryById(this.entry.id).subscribe({
next: (entry) => {
this.entry = entry;
console.log(`Entry refreched: `, entry);
// console.log(`Entry refreched: `, entry);
// TODO show updated notification
},
error: (error) => {
// TODO show error notification
console.error('Error refreching movie info:', error);
},
complete: () => {
this.lockActions = false;
},
});
}
@@ -263,13 +325,13 @@ export class EntryPageComponent implements OnInit {
});
this.seenHistory = episodeSeens;
console.log(`Episode Seen History Received: `, episodeSeens);
// console.log(`Episode Seen History Received: `, episodeSeens);
this.filterEpisodesSeenHistory();
},
error: (error) => {
this.seenHistory = [] as EpisodeSeenDto[];
this.filterEpisodesSeenHistory();
console.log(error);
console.log(`Error getting episodes watch history`, error);
},
});
}
@@ -301,12 +363,9 @@ export class EntryPageComponent implements OnInit {
dialogRef.afterClosed().subscribe((result) => {
// ? dialog closed
if (result == null) {
return console.log('Seen dialog was cancelled');
}
if (result == null) return;
// ? dialog confimed
console.log('Episode seen dialog closed with:', result);
// 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.`);
@@ -317,7 +376,7 @@ export class EntryPageComponent implements OnInit {
this.episodeSeenService.updateEpisodeSeen(episodeSeen).subscribe({
next: () => {
console.log(`Episode seen updated.`);
// console.log(`Episode seen updated.`);
this.getEpisodesSeenHistory();
// TODO show notification
},
@@ -332,39 +391,56 @@ export class EntryPageComponent implements OnInit {
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);
const dialogRef = this.dialog.open(ConfirmModalComponent, {
width: '400px',
data: {
title: 'Confirmation',
message: `Are you sure you want to delete this?`,
type: 'danger',
},
});
dialogRef.afterClosed().subscribe((result) => {
if (result) {
console.log(result);
if (result.confirmed != true) 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`);
}
@@ -406,23 +482,18 @@ export class EntryPageComponent implements OnInit {
this.showAll = !this.showAll;
}
enlargePoster() {
window.open(
'https://image.tmdb.org/t/p/original' + this.entry.poster_path,
'_blank'
);
}
// * getters
get status() {
return this.entry.status;
}
get createdDate() {
return this.datePipe.transform(this.entry.createdAt, 'dd MMMM, yyyy');
}
get releaseDate() {
return this.datePipe.transform(this.entry.release_date, 'MMM dd, yyyy');
}
get year() {
return this.datePipe.transform(this.entry.release_date, 'yyyy');
}
get runtime() {
return this.entry.runtime;
}
@@ -431,21 +502,6 @@ export class EntryPageComponent implements OnInit {
return this.entry.genres.join(', ');
}
get userRating(): string {
if (!this.entry.entryRating.user_rating) return '0';
return this.entry.entryRating.user_rating.toFixed(1);
}
get tmdbRating(): string {
if (!this.entry.entryRating.tmdb_rating) return '0';
return this.entry.entryRating.tmdb_rating.toFixed(1);
}
get imdbRating(): string {
if (!this.entry.entryRating.imdb_rating) return '0';
return this.entry.entryRating.imdb_rating.toFixed(1);
}
get poster(): string {
if (this.entry.poster_path == null)
return 'assets/images/default/default-poster.png';