Files
Showy/frontend/src/app/features/collections/user-collections/userCollections-page/userCollections-page.component.ts
T

182 lines
5.4 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { UserCollection } from '../../../../shared/models/userCollection.model';
import { CollectionService } from '../../../../core/services/userCollection.service';
import {
animate,
state,
style,
transition,
trigger,
} from '@angular/animations';
import { Entry } from '../../../../shared/models/entry.model';
import { environment } from '../../../../../environments/environment.prod';
@Component({
selector: 'app-userCollections-page',
templateUrl: './userCollections-page.component.html',
styleUrls: ['./userCollections-page.component.css'],
animations: [
trigger('animateDestroy', [
state('void', style({ opacity: '0' })),
transition('* => void', animate('300ms ease')),
]),
],
})
export class UserCollectionsPageComponent implements OnInit {
searchControl = new FormControl('');
showAddButton: boolean = false;
collectionsCount: number = 0;
collections: UserCollection[] = [];
collectionsToShow: UserCollection[] = [];
collectionType: string = 'All';
lockActions: boolean = false;
constructor(private collectionsService: CollectionService) {}
ngOnInit() {
this.getCollections();
this.searchControl.valueChanges.subscribe(() => {
this.applyFilter();
});
}
getCollections() {
this.collectionsService.getCollections().subscribe({
next: (result) => {
// console.log(`Loaded ${l.length} lists.`);
this.collections = result.map((c) => {
c.mediasCount = c.medias.length;
c.entriesCount = c.entries.length;
c.selectedPoster = this.getCollectionPosterPath(c);
c.medias = [];
c.entries = [];
return c;
});
this.collectionsToShow = this.collections;
this.applyFilter();
},
error: (error) => {
console.log("Counldn't get lists from backend.");
},
});
}
onCollectionTypeChange(event: any): void {
this.applyFilter();
}
applyFilter() {
// * type filter
let collections = this.collections;
if (this.collectionType != 'All') {
const type = this.collectionType == 'Custom' ? true : false;
collections = this.collections.filter(
(collection) => collection.isUserCollection == type
);
}
const filterValue = this.searchControl.value;
if (filterValue == null) return;
const normalizedFilter = filterValue.toLowerCase().trim();
this.collectionsToShow = collections.filter((collection) =>
collection.name.toLowerCase().trim().includes(normalizedFilter)
);
this.collectionsCount = this.collectionsToShow.length;
}
createCollection() {
this.lockActions = true;
// TODO add create collection from the collections list page
const listName = this.searchControl.value;
let myNewList = { name: listName } as UserCollection;
this.collectionsService.createCollection(myNewList).subscribe({
next: (collection) => {
this.collections.push(collection);
this.applyFilter();
},
error: (error) => {
// TODO show error notification
console.log(`Error creating new collection: `, error);
},
complete: () => {
this.lockActions = false;
},
});
}
doShowCreate() {
return (
this.searchControl.value &&
!this.collections.some(
(c) =>
c.name.trim().toLowerCase() ==
this.searchControl.value?.trim().toLowerCase()
)
);
}
getCollectionPosterPath(collection: UserCollection) {
if (collection.medias && collection.medias.length != 0) {
const selectedPosters = collection.medias.filter(
(m) => m.type == 'poster' && m.isSelected
);
let selectedPoster;
if (selectedPosters.length != 0) {
selectedPoster = selectedPosters[0].path;
} else {
selectedPoster = collection.medias[0].path;
}
return (
environment.API_URL +
`/media/collections/posters/preview/${selectedPoster}`
);
} else if (collection.entries && collection.entries.length != 0) {
// * use the poster of the first entry in the collection
// * if the collection is empty
return this.getEntryPosterPath(collection.entries[0]);
} else if (collection.posterPath) {
// * use first remote tmdb poster
// * if collection has none and has no entry
return `https://www.themoviedb.org/w420/${collection.posterPath}`;
} else {
// * use default poster
// * if collection has totaly no poster
return 'assets/images/default/default-poster.png';
}
}
getCollectionBakcgroundPath(collection: UserCollection) {
return `https://www.themoviedb.org/w1280/${collection.backdropPath}`;
}
getEntryPosterPath(entry: Entry) {
// * Is in library: use a local poster
if (entry.medias) {
const selectedPosters = entry.medias.filter(
(m) => m.type == 'poster' && m.isSelected
);
if (selectedPosters.length != 0) {
const name = selectedPosters[0].path;
return environment.API_URL + `/media/entries/posters/preview/${name}`;
}
}
// * No local poster available, Use TMDB poster
if (entry.poster_path)
return `https://image.tmdb.org/t/p/w300` + entry.poster_path;
// * No poster found, use default poster image as a last stand
return 'assets/images/default/default-poster.png';
}
}