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

107 lines
3.0 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { UserCollection } from '../../../../shared/models/userCollection.model';
import { UserCollectionService } from '../../../../core/services/userCollection.service';
import {
animate,
state,
style,
transition,
trigger,
} from '@angular/animations';
@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: UserCollectionService) {}
ngOnInit() {
this.getCollections();
this.searchControl.valueChanges.subscribe(() => {
this.applyFilter();
});
}
getCollections() {
this.collectionsService.getCollections().subscribe({
next: (l) => {
// console.log(`Loaded ${l.length} lists.`);
this.collections = l;
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());
}
}