F: implemented load on scroll for collections page

This commit is contained in:
X4Lo committed 2025-02-16 15:16:25 +01:00
1 parent ec9e21dd0f
commit 2d7bf91543
2 files changed
+47 -9

No files matched your search

@@ -42,7 +42,7 @@
<div class="card-body p-0">
<div class="collections-container">
<app-collection-card *ngFor="let collection of collectionsToShow" [collection]="collection"
<app-collection-card *ngFor="let collection of collectionsToDisplay" [collection]="collection"
@animateDestroy></app-collection-card>
</div>
@@ -1,4 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Collection } from '../../../../shared/models/collection.model';
import { CollectionService } from '../../../../core/services/collection.service';
@@ -11,6 +11,8 @@ import {
} from '@angular/animations';
import { Entry } from '../../../../shared/models/entry.model';
import { ConfigService } from '../../../../core/services/config.service';
import { ScrollService } from '../../../../core/services/scroll.service';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-collections-page',
@@ -23,31 +25,48 @@ import { ConfigService } from '../../../../core/services/config.service';
]),
],
})
export class CollectionsPageComponent implements OnInit {
export class CollectionsPageComponent implements OnInit, OnDestroy {
searchControl = new FormControl('');
showAddButton: boolean = false;
collectionsCount: number = 0;
collections: Collection[] = [];
collectionsToShow: Collection[] = [];
collectionsFiltered: Collection[] = [];
collectionsToDisplay: Collection[] = [];
collectionType: string = 'All';
lockActions: boolean = false;
private subscriptions: Subscription[] = [];
constructor(
private collectionsService: CollectionService,
private scrollService: ScrollService,
private configService: ConfigService
) {}
ngOnInit() {
this.getCollections();
this.searchControl.valueChanges.subscribe(() => {
let searchSub = this.searchControl.valueChanges.subscribe(() => {
this.applyFilter();
this.loadNext();
});
this.subscriptions.push(searchSub);
let scrollSub = this.scrollService.scrollEnd$.subscribe(() => {
this.loadNext();
});
this.subscriptions.push(scrollSub);
}
ngOnDestroy(): void {
// Unsubscribe to avoid memory leaks
this.subscriptions.forEach((sub) => sub.unsubscribe());
this.subscriptions = [];
}
getCollections() {
this.collectionsService.getCollections().subscribe({
let fetchSub = this.collectionsService.getCollections().subscribe({
next: (result) => {
// console.log(`Loaded ${l.length} lists.`);
this.collections = result.map((c) => {
@@ -61,20 +80,25 @@ export class CollectionsPageComponent implements OnInit {
return c;
});
this.collectionsToShow = this.collections;
this.collectionsToDisplay = this.collections;
this.applyFilter();
this.loadNext();
},
error: (error) => {
console.log("Counldn't get lists from backend.");
},
});
this.subscriptions.push(fetchSub);
}
onCollectionTypeChange(event: any): void {
this.applyFilter();
this.loadNext();
}
applyFilter() {
this.collectionsToDisplay = [];
// * type filter
let collections = this.collections;
if (this.collectionType != 'All') {
@@ -84,15 +108,29 @@ export class CollectionsPageComponent implements OnInit {
);
}
// * name filter
const filterValue = this.searchControl.value;
if (filterValue == null) return;
const normalizedFilter = filterValue.toLowerCase().trim();
this.collectionsToShow = collections.filter((collection) =>
this.collectionsFiltered = collections.filter((collection) =>
collection.name.toLowerCase().trim().includes(normalizedFilter)
);
this.collectionsCount = this.collectionsToShow.length;
this.collectionsCount = this.collectionsFiltered.length;
}
loadNext(): void {
const nextItems = this.collectionsFiltered.slice(
this.collectionsToDisplay.length,
Math.min(
this.collectionsToDisplay.length + 30,
this.collectionsFiltered.length
)
);
if (nextItems.length > 0) {
this.collectionsToDisplay = [...this.collectionsToDisplay, ...nextItems];
}
}
createCollection() {