Files
marketplaces/src/app/pages/home/home.component.ts

76 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-01-18 18:57:06 +04:00
import { Component, OnInit, signal, computed, ChangeDetectionStrategy } from '@angular/core';
import { CommonModule } from '@angular/common';
2026-02-14 00:45:17 +04:00
import { Router, RouterLink } from '@angular/router';
2026-01-18 18:57:06 +04:00
import { ApiService } from '../../services';
import { Category } from '../../models';
import { environment } from '../../../environments/environment';
import { ItemsCarouselComponent } from '../../components/items-carousel/items-carousel.component';
@Component({
selector: 'app-home',
standalone: true,
imports: [CommonModule, RouterLink, ItemsCarouselComponent],
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class HomeComponent implements OnInit {
brandName = environment.brandFullName;
isnovo = environment.theme === 'novo';
categories = signal<Category[]>([]);
loading = signal(true);
error = signal<string | null>(null);
// Memoized computed values for performance
topLevelCategories = computed(() => {
return this.categories().filter(cat => cat.parentID === 0);
});
// Cache subcategories by parent ID
private subcategoriesCache = computed(() => {
const cache = new Map<number, Category[]>();
this.categories().forEach(cat => {
if (cat.parentID !== 0) {
if (!cache.has(cat.parentID)) {
cache.set(cat.parentID, []);
}
cache.get(cat.parentID)!.push(cat);
}
});
return cache;
});
2026-02-14 00:45:17 +04:00
constructor(private apiService: ApiService, private router: Router) {}
2026-01-18 18:57:06 +04:00
ngOnInit(): void {
this.loadCategories();
}
loadCategories(): void {
this.loading.set(true);
this.apiService.getCategories().subscribe({
next: (categories) => {
this.categories.set(categories);
this.loading.set(false);
},
error: (err) => {
this.error.set('Failed to load categories');
this.loading.set(false);
console.error('Error loading categories:', err);
}
});
}
getTopLevelCategories(): Category[] {
return this.topLevelCategories();
}
getSubCategories(parentID: number): Category[] {
return this.subcategoriesCache().get(parentID) || [];
}
2026-02-14 00:45:17 +04:00
navigateToSearch(): void {
this.router.navigate(['/search']);
}
2026-01-18 18:57:06 +04:00
}