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