add: update modal for entry seen

This commit is contained in:
X4Lo committed 2024-05-19 06:55:47 +01:00
1 parent d6c3a974a1
commit bffc1227c3
8 files changed
+208 -81

No files matched your search

@@ -18,6 +18,7 @@ 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';
import { EntrySeen } from '../../../shared/models/entrySeen.model';
@Component({
selector: 'app-entry-page',
@@ -125,51 +126,6 @@ export class EntryPageComponent implements OnInit {
this.getEpisodesSeenHistory();
}
openEntrySeenModal() {
if (!this.entry) return;
const dialogRef = this.dialog.open(SeenModalComponent, {
width: '360px',
});
dialogRef.afterClosed().subscribe((result) => {
if (result) {
// console.log('Seen dialog closed with:', result);
if (result.watchDate == '') {
// TODO show no was passed date notification
return;
}
let watchDate = result.watchDate;
let watchedWith = result.watchedWith.split('\n').join(', ');
this.entrySeenService
.createEntrySeen(this.entry.id.toString(), watchDate, watchedWith)
.subscribe({
next: (entrySeen) => {
if (entrySeen) {
// TODO show confirm notification
// console.log(`EntrySeen Added`, entrySeen);
this.entry.entrySeen.push(entrySeen);
if (this.entry.watchlist) {
this.libraryService.refrechEntries().subscribe();
}
}
},
error: (error) => {
console.error('Error adding entrySeen:', error);
},
});
// TODO show notification
} else {
// console.log('Seen dialog was cancelled');
}
});
}
onRatingChange(event: Event) {
this.entryRatingsService
.updateEntryRatings(this.entry.entryRating)
@@ -187,6 +143,88 @@ export class EntryPageComponent implements OnInit {
});
}
// ! ENTRY SEEN
openEntrySeenModal(entrySeen: EntrySeen = {} as EntrySeen) {
if (!this.entry) return;
let oldData = {} as { watchDate: Date; watchedWith: string };
if (entrySeen.id) {
oldData.watchDate = entrySeen.date_seen;
oldData.watchedWith = entrySeen.seen_with;
}
const dialogRef = this.dialog.open(SeenModalComponent, {
width: '360px',
data: oldData.watchDate ? oldData : {},
});
dialogRef.afterClosed().subscribe((result) => {
if (result) {
if (result.watchDate == '') {
// TODO show no was passed date notification
return;
}
let watchDate = result.watchDate;
let watchedWith = result.watchedWith.split('\n').join(', ');
if (entrySeen.id) {
const es = {
id: entrySeen.id,
date_seen: watchDate,
seen_with: watchedWith,
} as EntrySeen;
// * Update
this.entrySeenService.updateEntrySeen(es).subscribe({
next: (updatedEntrySeen) => {
if (updatedEntrySeen) {
// TODO show confirm notification
// ? replace the entry-seen object with the updated one
for (let i = 0; i < this.entry.entrySeen.length; i++) {
if (this.entry.entrySeen[i].id == updatedEntrySeen.id)
this.entry.entrySeen[i] = updatedEntrySeen;
}
}
},
error: (error) => {
console.error('Error adding entrySeen:', error);
},
});
} else {
// * Create
this.entrySeenService
.createEntrySeen(this.entry.id.toString(), watchDate, watchedWith)
.subscribe({
next: (entrySeen) => {
if (entrySeen) {
// TODO show confirm notification
this.entry.entrySeen.push(entrySeen);
// ? if the entry was in watch list creating and entry-seen would have removed it
// ? from the watchlist, so now we need to refrech libraryService in order to
// ? update the watchlist page
if (this.entry.watchlist) {
this.libraryService.refrechEntries().subscribe();
}
}
},
error: (error) => {
console.error('Error adding entrySeen:', error);
},
});
}
// TODO show notification
} else {
// console.log('Seen dialog was cancelled');
}
});
}
editEntrySeen(entrySeen: EntrySeen) {
this.openEntrySeenModal(entrySeen);
}
removeEntrySeen(id: number) {
const dialogRef = this.dialog.open(ConfirmModalComponent, {
width: '400px',