add fetch entry collection + change to seen modals & improvements

This commit is contained in:
X4Lo committed 2024-09-10 01:19:17 +01:00
1 parent 776506b232
commit 41666cc6a3
7 files changed
+171 -82

No files matched your search

@@ -29,7 +29,7 @@
<div class="col-md-12">
<label class="form-label">Date Watched</label>
<input type="text" class="form-control rounded-0" name="watch-date" [(ngModel)]="watchDate" id="myDatepicker">
<input type="date" class="form-control rounded-0" name="watch-date" [(ngModel)]="watchDate" id="myDatepicker">
</div>
<div class="col-md-12">
@@ -9,11 +9,11 @@ import {
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';
import { EpisodeSeenDto } from '../../../models/dtos/episode-seen.dto';
@Component({
selector: 'app-episode-seen-modal',
@@ -41,6 +41,7 @@ export class EpisodeSeenModalComponent {
public entryId: number = 0;
public watchDate: string = '';
public watchedWith: string = '';
public episodeSeen: EpisodeSeenDto = {} as EpisodeSeenDto;
public seasons: Season[] = [];
public selectedSeason: Season | null = {} as Season;
@@ -54,28 +55,84 @@ export class EpisodeSeenModalComponent {
) {}
ngOnInit(): void {
if (!this.data.entryId) return;
if (!this.data) return;
this.entryId = this.data.entryId;
if (this.data.episodeSeen) {
this.episodeSeen = this.data.episodeSeen;
this.watchDate = formatDate(
new Date(this.data.episodeSeen.date_seen),
'yyyy-MM-dd',
'en-US'
);
this.watchedWith = this.data.episodeSeen.seen_with;
} else {
this.watchDate = formatDate(new Date(), 'yyyy-MM-dd', 'en-US');
// ? these season and episode number are from the latest watched episode
// ? used to determine the next episode to watch and automaticaly select it
this.episodeSeen.season_number = this.data.season_number;
this.episodeSeen.episode_number = this.data.episode_number;
}
this.seasonsService
.getSeasonsWithEpisodesByEntryId(this.data.entryId)
.subscribe({
next: (seasons) => {
this.seasons = seasons;
// * on update mode
if (this.data.episodeSeen) {
this.seasons.forEach((s) => {
if (s.season_number == this.episodeSeen.season_number) {
this.selectedSeason = s;
s.episodes.forEach((e) => {
if (e.episode_number == this.episodeSeen.episode_number) {
this.selectedEpisode = e;
}
});
}
});
} else {
// * create mode
let season = this.seasons.find(
(s) => s.season_number == this.episodeSeen.season_number
);
let episode = season?.episodes.find(
(e) => e.episode_number == this.episodeSeen.episode_number + 1
);
if (episode) {
this.selectedSeason = season!;
this.selectedEpisode = episode;
} else {
season = this.seasons.find(
(s) => s.season_number == this.episodeSeen.season_number + 1
);
if (season) {
episode = season?.episodes.find((e) => e.episode_number == 1);
if (episode) {
this.selectedSeason = season!;
this.selectedEpisode = episode!;
}
}
}
}
},
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',
});
// flatpickr('#myDatepicker', {
// inline: true,
// defaultDate: this.watchDate,
// altInput: true,
// allowInput: false,
// altFormat: 'F j, Y',
// dateFormat: 'Y-m-d',
// });
}
onSeasonChange(event: any): void {
@@ -83,48 +140,55 @@ export class EpisodeSeenModalComponent {
}
onConfirm(): void {
if (!this.selectedSeason) {
return;
}
if (!this.selectedEpisode) {
return;
}
if (!this.watchDate) {
if (!this.selectedSeason || !this.selectedEpisode || !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({
this.episodeSeen.date_seen = date_seen;
this.episodeSeen.seen_with = seen_with;
if (this.episodeSeen.id) {
this.episodeSeenService.updateEpisodeSeen(this.episodeSeen).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,
updated: true,
});
}
},
error: (error) => {
console.log(`Episode seen create error: `, error);
console.log(`Episode seen update error: `, error);
// TODO show notification
},
});
this.dialogRef.close({
created: false,
});
} else {
// ? create
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
},
});
}
}
onClose(): void {
@@ -1,18 +1,19 @@
<div class="card border-top border-3 border-primary rounded-0" style="margin-bottom: 0 !important;">
<div class="modal-header card-header py-3 px-4" >
<div class="modal-header card-header py-3 px-4">
<h5 class="mb-0 text-primary">Watch Details</h5>
<button type="button" class="btn-close" (click)="onClose()" aria-label="Close"></button>
</div>
<div class="card-body p-4">
<div class="card-body p-4 pt-1">
<form class="row g-3">
<div class="col-md-12">
<label class="form-label">Seen With</label>
<textarea class="form-control rounded-0" [(ngModel)]="watchedWith" [ngModelOptions]="{ standalone: true }" placeholder="Enter names" rows="3"></textarea>
<textarea class="form-control rounded-0" [(ngModel)]="watchedWith" [ngModelOptions]="{ standalone: true }"
placeholder="Enter names" rows="3"></textarea>
</div>
<div class="col-md-12">
<label class="form-label">Date Watched</label>
<input type="text" class="form-control rounded-0" name="watch-date" [(ngModel)]="watchDate" id="myDatepicker">
<input type="date" class="form-control rounded-0" name="watch-date" [(ngModel)]="watchDate" id="myDatepicker">
</div>
<div class="col-md-12">
@@ -48,18 +48,22 @@ export class SeenModalComponent implements OnInit {
this.watchDate = formatDate(new Date(), 'yyyy-MM-dd', 'en-US');
} else {
// ? if data is passed, populate fields
this.watchDate = formatDate(new Date(this.data.watchDate), 'yyyy-MM-dd', 'en-US');
this.watchDate = formatDate(
new Date(this.data.watchDate),
'yyyy-MM-dd',
'en-US'
);
this.watchedWith = this.data.watchedWith.split(', ').join('\n');
}
flatpickr('#myDatepicker', {
inline: true,
defaultDate: this.watchDate,
altInput: true,
allowInput: false,
altFormat: 'F j, Y',
dateFormat: 'Y-m-d',
});
// flatpickr('#myDatepicker', {
// inline: true,
// defaultDate: this.watchDate,
// altInput: true,
// allowInput: false,
// altFormat: 'F j, Y',
// dateFormat: 'Y-m-d',
// });
}
onConfirm(): void {