add entry page feature

This commit is contained in:
X4Lo committed 2024-04-16 19:18:24 +01:00
1 parent 9fb4202640
commit e1223171b0
8 files changed
+525 -73

No files matched your search

@@ -1,10 +1,245 @@
import { Component } from '@angular/core';
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { trigger, state, style, transition, animate } from '@angular/animations';
import { ActivatedRoute, Router } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { Entry } from '../../../shared/models/entry.model';
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 { EntrySeenService } from '../../../core/services/entry-seen.service';
import { EntryRatingsService } from '../../../core/services/entry-ratings.service';
import { TmdbService } from '../../../core/services/tmdb.service';
import { LibraryService } from '../../../core/services/library.service';
import { EntryService } from '../../../core/services/entry.service';
@Component({
selector: 'app-entry-page',
templateUrl: './entry-page.component.html',
styleUrls: ['./entry-page.component.css']
styleUrls: ['./entry-page.component.css'],
encapsulation: ViewEncapsulation.None,
animations: [
trigger('fadeInOut', [
transition(':enter', [ // void => *
style({ opacity: 0 }),
animate('0.5s ease-in', style({ opacity: 1 }))
]),
transition(':leave', [ // * => void
animate('0.5s ease-out', style({ opacity: 0 }))
])
])
],
})
export class EntryPageComponent {
export class EntryPageComponent implements OnInit {
entry: Entry = {} as Entry;
public showAll = false;
public initialCount = 2;
constructor(
private route: ActivatedRoute,
private router: Router,
public dialog: MatDialog,
private tmdbService: TmdbService,
private libraryService: LibraryService,
private entryService: EntryService,
private entrySeenService: EntrySeenService,
private entryRatingsService: EntryRatingsService,
private datePipe: DatePipe
) {}
ngOnInit(): void {
this.route.data.subscribe(
(data) => {
this.entry = data['entry'] as Entry;
this.entry.seasons = this.entry.seasons.sort((a, b) => a.name.localeCompare(b.name));
console.log(this.entry);
if (!this.entry) {
this.router.navigate(['/error'], {
queryParams: { error: 'Entry not found' },
});
}
},
(error) => {
console.error('Error when trying to access route data', error);
this.router.navigate(['/error'], {
queryParams: { error: 'Error fetching data' },
});
}
);
}
openDialog() {
if (!this.entry) return;
const dialogRef = this.dialog.open(SeenModalComponent, {
width: '400px',
});
console.log('Seen dialog opened');
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 watchedWith = result.watchedWith.split('\n').join(', ');
this.entrySeenService
.createEntrySeen(
this.entry.id.toString(),
result.watchDate,
watchedWith
)
.subscribe({
next: (entrySeen) => {
if (entrySeen) {
// TODO show confirm notification
console.log(`EntrySeen Added`, entrySeen);
this.entry.entrySeen.push(entrySeen);
}
},
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)
.subscribe({
next: (entryRatings) => {
if (entryRatings) {
// TODO show notification
this.entry.entryRating = entryRatings;
}
},
error: (error) => {
// TODO show notification
console.error('Error updating entryRatings:', error);
},
});
}
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);
},
});
}
showAddToListModal() {
if (this.entry) {
const dialogRef = this.dialog.open(UserCollectionsModalComponent, {
width: '400px',
height: '55%',
data: {
entryId: this.entry.id,
},
});
dialogRef.afterClosed().subscribe((result) => {
console.log('Modal Closed: ', result);
});
}
}
refrechEntry() {
this.entryService.refrechEntryById(this.entry.id).subscribe({
next: (entry) => {
this.entry = entry;
console.log(`Entry refreched: `, entry);
// TODO show updated notification
},
error: (error) => {
// TODO show error notification
console.error('Error refreching movie info:', error);
},
});
}
toggleShowAllSeasons() {
this.showAll = !this.showAll;
}
// * 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;
}
get genres(): string {
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';
return `https://image.tmdb.org/t/p/w500` + this.entry.poster_path;
}
get banner(): string {
if (this.entry.banner_path == null)
return 'assets/images/default/default-poster.png';
return `https://image.tmdb.org/t/p/w1280` + this.entry.banner_path;
}
get nextEpisode() {
if (!this.entry.is_tvseries) return 'This is a movie';
return 'sooon';
}
}