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

65 lines
1.8 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';
@Component({
selector: 'app-userCollections-page',
templateUrl: './userCollections-page.component.html',
styleUrls: ['./userCollections-page.component.css'],
})
export class UserCollectionsPageComponent implements OnInit {
searchControl = new FormControl('');
showAddButton: boolean = false;
listsCount: number = 0;
lists: UserCollection[] = [];
listsToShow: UserCollection[] = [];
constructor(private listService: UserCollectionService) {}
ngOnInit() {
this.getLists();
this.searchControl.valueChanges.subscribe(() => {
this.applyFilter();
});
}
getLists() {
this.listService.getLists().subscribe({
next: (l) => {
console.log(`Loaded ${l.length} lists.`);
this.lists = l;
this.listsToShow = this.lists;
this.applyFilter();
},
error: (error) => {
console.log("Counldn't get lists from backend.");
},
});
}
applyFilter() {
let filterValue = this.searchControl.value;
if (filterValue == null) {
this.listsToShow = [...this.lists];
return;
}
const normalizedFilter = filterValue.toLowerCase().trim();
this.listsToShow = this.lists.filter((collection) =>
collection.name.toLowerCase().trim().includes(normalizedFilter)
);
this.listsCount = this.listsToShow.length;
}
createList() {
// const listName = this.searchControl.value; // string
// let myNewList = { name: listName } as List;
// this.listService.createList(myNewList).subscribe(async (list) => {
// await this.getLists();
// });
}
}