Files
Showy/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.ts
T
2024-05-19 17:03:38 +01:00

134 lines
3.5 KiB
TypeScript

import { Component, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import {
trigger,
state,
style,
transition,
animate,
} from '@angular/animations';
import { formatDate } from '@angular/common';
import flatpickr from 'flatpickr';
import { Season } from '../../../models/season.model';
import { SeasonService } from '../../../../core/services/season.service';
import { Episode } from '../../../models/episode.models';
import { EpisodeSeenService } from '../../../../core/services/episode-seen.service';
@Component({
selector: 'app-episode-seen-modal',
templateUrl: './episode-seen-modal.component.html',
styleUrl: './episode-seen-modal.component.css',
animations: [
trigger('fadeInOut', [
state('in', style({ opacity: 1 })),
transition(':enter', [style({ opacity: 0 }), animate(600)]),
transition(':leave', [animate(600, style({ opacity: 0 }))]),
]),
trigger('scaleInOut', [
transition(':enter', [
state('in', style({ opacity: 1 })),
style({ transform: 'scale(0)' }),
animate('200ms ease-in-out', style({ transform: 'scale(1)' })),
]),
transition(':leave', [
animate('200ms ease-in-out', style({ transform: 'scale(0)' })),
]),
]),
],
})
export class EpisodeSeenModalComponent {
public entryId: number = 0;
public watchDate: string = '';
public watchedWith: string = '';
public seasons: Season[] = [];
public selectedSeason: Season | null = {} as Season;
public selectedEpisode: Episode | null = {} as Episode;
constructor(
public dialogRef: MatDialogRef<EpisodeSeenModalComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private seasonsService: SeasonService,
private episodeSeenService: EpisodeSeenService
) {}
ngOnInit(): void {
if (!this.data.entryId) return;
this.entryId = this.data.entryId;
this.seasonsService
.getSeasonsWithEpisodesByEntryId(this.data.entryId)
.subscribe({
next: (seasons) => {
this.seasons = seasons;
},
error: (error) => {
console.log(`Error gettings seasons: `, error);
},
});
flatpickr('#myDatepicker', {
inline: true,
defaultDate: this.watchDate,
altInput: true,
allowInput: false,
altFormat: 'F j, Y',
dateFormat: 'Y-m-d',
});
}
onSeasonChange(event: any): void {
this.selectedEpisode = null;
}
onConfirm(): void {
if (!this.selectedSeason) {
return;
}
if (!this.selectedEpisode) {
return;
}
if (!this.watchDate) {
return;
}
const date_seen = new Date(this.watchDate);
const seen_with = this.watchedWith.split('\n').join(', ');
this.episodeSeenService
.createEpisodeSeen(
this.entryId,
this.selectedEpisode?.id,
date_seen,
seen_with
)
.subscribe({
next: (episodeSeen) => {
if (episodeSeen) {
return this.dialogRef.close({
created: true,
// ? returning the season number so it can be used to refrech the-
// ? season card progress bar in the entry page
season_number: this.selectedSeason?.season_number,
});
}
},
error: (error) => {
console.log(`Episode seen create error: `, error);
// TODO show notification
},
});
this.dialogRef.close({
created: false,
});
}
onClose(): void {
this.dialogRef.close();
}
}