From 2d7bf91543d13b0369a90e91b36a6c442613ec64 Mon Sep 17 00:00:00 2001 From: X4Lo Date: Sun, 16 Feb 2025 15:16:25 +0100 Subject: [PATCH] F: implemented load on scroll for collections page --- .../collections-page.component.html | 2 +- .../collections-page.component.ts | 54 ++++++++++++++++--- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/frontend/src/app/features/collections/collections/collections-page/collections-page.component.html b/frontend/src/app/features/collections/collections/collections-page/collections-page.component.html index dbdc41a..a72acc9 100644 --- a/frontend/src/app/features/collections/collections/collections-page/collections-page.component.html +++ b/frontend/src/app/features/collections/collections/collections-page/collections-page.component.html @@ -42,7 +42,7 @@
-
diff --git a/frontend/src/app/features/collections/collections/collections-page/collections-page.component.ts b/frontend/src/app/features/collections/collections/collections-page/collections-page.component.ts index 63cb099..4343b39 100644 --- a/frontend/src/app/features/collections/collections/collections-page/collections-page.component.ts +++ b/frontend/src/app/features/collections/collections/collections-page/collections-page.component.ts @@ -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() {