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

@@ -10,6 +10,8 @@ import {
} from '@angular/animations';
import { Episode } from '../../models/episode.models';
import { EpisodeSeenService } from '../../../core/services/episode-seen.service';
import { MatDialog } from '@angular/material/dialog';
import { SeenModalComponent } from '../../components/modals/seen-modal/seen-modal.component';
@Component({
selector: 'app-season-main-card',
@@ -37,11 +39,11 @@ export class SeasonMainCardComponent implements OnInit {
constructor(
private seasonService: SeasonService,
private episodeSeenService: EpisodeSeenService
private episodeSeenService: EpisodeSeenService,
public dialog: MatDialog
) {}
ngOnInit(): void {
// TODO: GET SEASON EPISODES
this.seasonService.getSeasonEpisodes(this.season.id).subscribe({
next: (episodes) => {
console.log(`Loaded Season Episodes: `, episodes);
@@ -83,29 +85,77 @@ export class SeasonMainCardComponent implements OnInit {
return count;
}
episodeMarkSeenClick() {
async episodeMarkSeenClick() {
if (this.isLocked) return;
this.isLocked = true;
// starts looping from the last available episode
for (let i = this.season.episode_count; i >= 0; i--) {
let selectedEpisode = {} as Episode;
let selectedEpisodeIndex = -1;
// * starts looping from the last available episode
if (
this.season.episodes[this.season.episodes.length - 1].date_first_seen ==
null
) {
// ? untils it finds that the previous episode episode is marked as seen
// ? > in that case it chooses the current one to mark
if (i == 0 || this.season.episodes[i - 1].date_first_seen != null) {
let currentEpisode = this.season.episodes[i];
// todo: show episode seen modal
for (let i = this.season.episodes.length - 1; i >= 0; i--) {
if (i == 0 || this.season.episodes[i - 1].date_first_seen != null) {
selectedEpisodeIndex = i;
selectedEpisode = this.season.episodes[i];
break;
}
}
} else {
// ? in case the last episode is already marked as seen but
// ? there are other before it that are not, then start from
// ? episode 1 and look for any that are not seen
for (let i = 0; i < this.season.episodes.length; i++) {
if (this.season.episodes[i].date_first_seen == null) {
selectedEpisodeIndex = i;
selectedEpisode = this.season.episodes[i];
break;
}
}
}
// + end loocking for episode
if (selectedEpisode == null) return;
if (selectedEpisode.date_first_seen != null) return;
// ? show the episode seen modal
const dialogRef = this.dialog.open(SeenModalComponent, {
width: '400px',
});
console.log('Episode seen dialog opened');
await dialogRef.afterClosed().subscribe((result) => {
if (result) {
console.log('Episode seen dialog closed with:', result);
if (result.watchDate == '') {
// TODO show no date was passed notification
this.isLocked = false;
return;
}
let watchDate = result.watchDate;
let watchedWith = result.watchedWith.split('\n').join(', ');
this.episodeSeenService
.createEpisodeSeen(
this.season.entry_id,
currentEpisode.id,
new Date(),
'Me, My Self & I'
selectedEpisode.id,
watchDate,
watchedWith
)
.subscribe({
next: (episodeSeen) => {
console.log(`Episode marked as seen`, episodeSeen);
this.season.episodes[i].date_first_seen = new Date();
this.season.episodes[i].date_last_seen = new Date();
// TODO get actualy date_first_seen & date_last_seen for episode seen (not just place holders)
this.season.episodes[selectedEpisodeIndex].date_first_seen =
new Date();
this.season.episodes[selectedEpisodeIndex].date_last_seen =
new Date();
this.episodeWatchedEvent.emit();
// TODO show notification
},
@@ -117,8 +167,10 @@ export class SeasonMainCardComponent implements OnInit {
this.isLocked = false;
},
});
break;
} else {
console.log('Seen dialog was cancelled');
this.isLocked = false;
}
}
});
}
}
@@ -13,6 +13,7 @@ import {
transition,
animate,
} from '@angular/animations';
import { formatDate } from '@angular/common';
@Component({
selector: 'app-seen-modal',
@@ -36,7 +37,7 @@ import {
]),
],
})
export class SeenModalComponent {
export class SeenModalComponent implements OnInit {
public watchDate: string = '';
public watchedWith: string = '';
@@ -45,6 +46,15 @@ export class SeenModalComponent {
@Inject(MAT_DIALOG_DATA) public data: any
) {}
ngOnInit(): void {
if (this.data == null) {
this.watchDate = formatDate(new Date(), 'yyyy-MM-dd', 'en-US');
return; // ? no initial data was passed
}
this.watchDate = this.data.watchDate.toString().substring(0, 10);
this.watchedWith = this.data.watchedWith.split(', ').join('\n');
}
onConfirm(): void {
this.dialogRef.close({
watchDate: this.watchDate,