147 lines
3.8 KiB
TypeScript
147 lines
3.8 KiB
TypeScript
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
|
import { UserCollectionsModalComponent } from '../../modals/list-selection-modal/user-collections-modal.component';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import {
|
|
trigger,
|
|
transition,
|
|
style,
|
|
animate,
|
|
state,
|
|
} from '@angular/animations';
|
|
import { Entry } from '../../../models/entry.model';
|
|
import { LibraryService } from '../../../../core/services/library.service';
|
|
import { Router } from '@angular/router';
|
|
|
|
@Component({
|
|
selector: 'app-main-card',
|
|
templateUrl: './main-card.component.html',
|
|
styleUrls: ['./main-card.component.css'],
|
|
animations: [
|
|
trigger('fadeInOut', [
|
|
transition(':enter', [
|
|
style({ opacity: 0, transform: 'translateY(-50px)' }),
|
|
animate(
|
|
'300ms ease-out',
|
|
style({ opacity: 1, transform: 'translateY(0)' })
|
|
),
|
|
]),
|
|
transition(':leave', [
|
|
animate(
|
|
'300ms ease-in',
|
|
style({ opacity: 0, transform: 'translateY(-50px)' })
|
|
),
|
|
]),
|
|
]),
|
|
],
|
|
})
|
|
export class MainCardComponent implements OnInit {
|
|
@Output() removedEvent = new EventEmitter();
|
|
@Input() entry!: Entry;
|
|
|
|
constructor(
|
|
private libraryService: LibraryService,
|
|
public dialog: MatDialog,
|
|
private router: Router
|
|
) {}
|
|
|
|
ngOnInit(): void {
|
|
this.libraryService.isInLibrary(this.entry.entryId.tmdb_id).subscribe({
|
|
next: (e) => {
|
|
if (e != null) {
|
|
e.isInLibrary = true;
|
|
this.entry = e;
|
|
}
|
|
},
|
|
error: (error) => {},
|
|
});
|
|
}
|
|
|
|
get releaseYear(): number {
|
|
return new Date(this.entry.release_date).getFullYear();
|
|
}
|
|
|
|
get formattedRate(): string {
|
|
return this.entry.entryRating.tmdb_rating.toFixed(1);
|
|
}
|
|
|
|
addEntry(tmdb_id: string, is_tvserie: boolean) {
|
|
console.log('Adding Entry To Library.');
|
|
let obs = this.libraryService.addEntry(tmdb_id, is_tvserie).subscribe({
|
|
next: (entry) => {
|
|
if (entry != null) {
|
|
console.log(`Entry added to library`, entry);
|
|
entry.isInLibrary = true;
|
|
this.entry = entry;
|
|
obs.unsubscribe();
|
|
}
|
|
},
|
|
error: (error) => {
|
|
console.error('Error adding entry:', error);
|
|
},
|
|
});
|
|
}
|
|
|
|
removeEntry(id: number) {
|
|
console.log('Deleting Entry From Library.');
|
|
this.libraryService.deleteEntry(id).subscribe({
|
|
next: (response) => {
|
|
console.log('Entry successfully deleted.', response);
|
|
// ? emit to dynamic entry list
|
|
this.entry.id = 0;
|
|
this.entry.isInLibrary = false;
|
|
this.removedEvent.emit(this.entry.id);
|
|
// todo entry removed notification
|
|
},
|
|
error: (error) => {
|
|
console.error('Error deleting entry:', 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);
|
|
});
|
|
}
|
|
}
|
|
|
|
cardClick() {
|
|
if (this.entry.isInLibrary)
|
|
this.router.navigate([`/entries/${this.entry.id}`]);
|
|
}
|
|
|
|
getTmdbLink() {
|
|
let type = this.entry.is_tvseries ? 'tv' : 'movie';
|
|
return `https://www.themoviedb.org/${type}/${this.entry.entryId.tmdb_id}`;
|
|
}
|
|
|
|
isCertainRoute(): boolean {
|
|
return (
|
|
this.router.isActive('/search', true) ||
|
|
this.router.isActive('/popular', true) ||
|
|
this.router.isActive('/trending', true)
|
|
);
|
|
}
|
|
|
|
get postPath() {
|
|
if (this.entry.poster_path == null)
|
|
return 'assets/images/default/default-poster.png';
|
|
|
|
return `https://image.tmdb.org/t/p/w154` + this.entry.poster_path;
|
|
}
|
|
|
|
get runtime() {
|
|
if (this.entry.runtime == null) return '';
|
|
return this.entry.runtime;
|
|
}
|
|
}
|