home page

This commit is contained in:
sdarbinyan
2026-02-14 01:28:08 +04:00
parent 88ac37ebc4
commit 751ad48489
14 changed files with 522 additions and 155 deletions

View File

@@ -1,6 +1,7 @@
import { Component, OnInit, signal, computed, ChangeDetectionStrategy } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Router, RouterLink } from '@angular/router';
import { forkJoin } from 'rxjs';
import { ApiService } from '../../services';
import { Category } from '../../models';
import { environment } from '../../../environments/environment';
@@ -18,6 +19,7 @@ export class HomeComponent implements OnInit {
brandName = environment.brandFullName;
isnovo = environment.theme === 'novo';
categories = signal<Category[]>([]);
itemCounts = signal<Map<number, number>>(new Map());
loading = signal(true);
error = signal<string | null>(null);
@@ -52,6 +54,7 @@ export class HomeComponent implements OnInit {
next: (categories) => {
this.categories.set(categories);
this.loading.set(false);
this.loadItemCounts();
},
error: (err) => {
this.error.set('Failed to load categories');
@@ -61,6 +64,31 @@ export class HomeComponent implements OnInit {
});
}
private loadItemCounts(): void {
const topLevel = this.topLevelCategories();
if (topLevel.length === 0) return;
const requests: Record<string, ReturnType<ApiService['getCategoryItems']>> = {};
topLevel.forEach(cat => {
requests[cat.categoryID.toString()] = this.apiService.getCategoryItems(cat.categoryID, 1000);
});
forkJoin(requests).subscribe({
next: (results) => {
const counts = new Map<number, number>();
Object.entries(results).forEach(([id, items]) => {
counts.set(Number(id), items.length);
});
this.itemCounts.set(counts);
},
error: (err) => console.error('Error loading item counts:', err)
});
}
getItemCount(categoryID: number): number {
return this.itemCounts().get(categoryID) || 0;
}
getTopLevelCategories(): Category[] {
return this.topLevelCategories();
}
@@ -72,4 +100,28 @@ export class HomeComponent implements OnInit {
navigateToSearch(): void {
this.router.navigate(['/search']);
}
scrollToCatalog(): void {
const target = document.getElementById('catalog');
if (!target) return;
const targetY = target.getBoundingClientRect().top + window.scrollY;
const startY = window.scrollY;
const distance = targetY - startY;
const duration = 1200;
let start: number | null = null;
const easeInOutCubic = (t: number) =>
t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
const step = (timestamp: number) => {
if (!start) start = timestamp;
const elapsed = timestamp - start;
const progress = Math.min(elapsed / duration, 1);
window.scrollTo(0, startY + distance * easeInOutCubic(progress));
if (progress < 1) requestAnimationFrame(step);
};
requestAnimationFrame(step);
}
}