Files
Showy/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.ts
T

122 lines
3.7 KiB
TypeScript

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Entry } from '../../../../shared/models/entry.model';
import { trigger, transition, style, animate } from '@angular/animations';
import { CollectionService } from '../../../../core/services/userCollection.service';
import { Router } from '@angular/router';
import { ConfirmModalComponent } from '../../../../shared/components/modals/confirm-modal/confirm-modal.component';
import { environment } from '../../../../../environments/environment.prod';
@Component({
selector: 'app-userCollection-grid-card',
templateUrl: './userCollection-grid-card.component.html',
styleUrls: ['./userCollection-grid-card.component.css'],
animations: [
trigger('fadeInOut', [
transition(':enter', [
style({ opacity: 0 }),
animate('0.5s ease-out', style({ opacity: 1 })),
]),
transition(':leave', [animate('0.5s ease-in', style({ opacity: 0 }))]),
]),
],
})
export class UserCollectionGridCardComponent implements OnInit {
@Input() entry!: Entry;
@Input() collectionId: string = '';
@Output() refrechEvent = new EventEmitter();
constructor(
private userCollectionService: CollectionService,
public dialog: MatDialog,
private router: Router
) {}
ngOnInit(): void {}
get releaseYear(): number {
return new Date(this.entry.release_date).getFullYear();
}
get formattedRate(): string {
return this.entry.entryRating.tmdb_rating.toFixed(1);
}
getTmdbLink() {
let type = this.entry.is_tvseries ? 'tv' : 'movie';
return `https://www.themoviedb.org/${type}/${this.entry.entryId.tmdb_id}`;
}
get posterPath() {
// * Not in library, use TMDB poster
// if (!this.entry.isInLibrary)
// return `https://image.tmdb.org/t/p/w300` + this.entry.poster_path;
// * Is in library: use a local poster
if (this.entry.medias) {
const selectedPosters = this.entry.medias.filter(
(m) => m.type == 'poster' && m.isSelected
);
if (
selectedPosters.length &&
selectedPosters[0] &&
selectedPosters[0].path
) {
const name = selectedPosters[0].path;
return environment.API_URL + `/media/entries/posters/preview/${name}`;
}
}
// * No local poster available, Use TMDB poster
if (this.entry.poster_path)
return `https://image.tmdb.org/t/p/w300` + this.entry.poster_path;
// * No poster found, use default poster image as a last stand
return 'assets/images/default/default-poster.png';
}
removeEntryFromList() {
const dialogRef = this.dialog.open(ConfirmModalComponent, {
width: '400px',
data: {
title: 'Confirmation',
message: `Are you sure you want to remove ${this.entry.title} from this collection?`,
type: 'danger',
},
});
dialogRef.afterClosed().subscribe((result) => {
if (result) {
if (result.confirmed != true) return;
this.userCollectionService
.removeEntryFromCollection(this.collectionId, this.entry.id)
.subscribe({
next: (response) => {
// TODO confirm notification
console.log('Entry successfully removed from list.', response);
this.entry.id = 0;
this.entry.isInLibrary = false;
},
error: (error) => {
// TODO error notification
console.error('Error removing entry from list:', error);
},
});
}
});
}
// ? EVENTS
cardClick() {
this.router.navigate([`/entries/${this.entry.id}`]);
}
// ? EVENT EMITTER
emitRefrechEvent() {
this.refrechEvent.emit();
}
}