Files
Showy/frontend/src/app/features/collections/user-collection-info/collection-table-line/collection-table-line.component.ts
T

134 lines
3.8 KiB
TypeScript

import {
Component,
EventEmitter,
Input,
OnInit,
Output,
ViewEncapsulation,
} from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { LibraryService } from '../../../../core/services/library.service';
import { Entry } from '../../../../shared/models/entry.model';
import { trigger, transition, style, animate } from '@angular/animations';
import { UserCollectionService } 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: 'tr [app-collection-table-line]',
templateUrl: './collection-table-line.component.html',
styleUrl: './collection-table-line.component.css',
encapsulation: ViewEncapsulation.None,
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 CollectionTableLineComponent implements OnInit {
@Input() entry!: Entry;
@Input() listId: string = '';
@Output() refrechEvent = new EventEmitter();
constructor(
private libraryService: LibraryService,
private userCollectionService: UserCollectionService,
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}`;
}
getPostPath() {
if (this.entry.poster_path == null) return 'https://pic.pnnet.dev/256x256';
return `https://image.tmdb.org/t/p/w500` + this.entry.poster_path;
}
getLogoPath() {
// * Is in library: use a local logo
if (this.entry.medias) {
const selectedLogos = this.entry.medias.filter(
(m) => m.type == 'logo' && m.isSelected
);
if (
selectedLogos.length &&
selectedLogos[0] &&
selectedLogos[0].path
) {
const name = selectedLogos[0].path.split('.')[0];
const extention = selectedLogos[0].path.split('.')[1];
return environment.API_URL + `/media/entries/logos/original/${name}.${extention}`;
}
}
// * No poster found, use default poster image as a last stand
return 'assets/images/default/default-logo.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.listId, 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() {
if (this.entry.isInLibrary)
this.router.navigate([`/entries/${this.entry.id}`]);
}
// ? EVENT EMITTER
emitRefrechEvent() {
this.refrechEvent.emit();
}
}