improvments are done
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { Component, OnInit, signal, computed, ChangeDetectionStrategy } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Router, RouterLink } from '@angular/router';
|
||||
import { ApiService } from '../../services';
|
||||
import { Category } from '../../models';
|
||||
@@ -8,8 +7,7 @@ import { ItemsCarouselComponent } from '../../components/items-carousel/items-ca
|
||||
|
||||
@Component({
|
||||
selector: 'app-home',
|
||||
standalone: true,
|
||||
imports: [CommonModule, RouterLink, ItemsCarouselComponent],
|
||||
imports: [RouterLink, ItemsCarouselComponent],
|
||||
templateUrl: './home.component.html',
|
||||
styleUrls: ['./home.component.scss'],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush
|
||||
@@ -27,6 +25,13 @@ export class HomeComponent implements OnInit {
|
||||
return this.categories().filter(cat => cat.parentID === 0);
|
||||
});
|
||||
|
||||
// Memoized item count lookup
|
||||
private itemCountMap = computed(() => {
|
||||
const map = new Map<number, number>();
|
||||
this.categories().forEach(cat => map.set(cat.categoryID, cat.itemCount || 0));
|
||||
return map;
|
||||
});
|
||||
|
||||
// Cache subcategories by parent ID
|
||||
private subcategoriesCache = computed(() => {
|
||||
const cache = new Map<number, Category[]>();
|
||||
@@ -64,12 +69,7 @@ export class HomeComponent implements OnInit {
|
||||
}
|
||||
|
||||
getItemCount(categoryID: number): number {
|
||||
const cat = this.categories().find(c => c.categoryID === categoryID);
|
||||
return cat?.itemCount || 0;
|
||||
}
|
||||
|
||||
getTopLevelCategories(): Category[] {
|
||||
return this.topLevelCategories();
|
||||
return this.itemCountMap().get(categoryID) || 0;
|
||||
}
|
||||
|
||||
getSubCategories(parentID: number): Category[] {
|
||||
|
||||
Reference in New Issue
Block a user