2026-02-26 21:54:21 +04:00
|
|
|
|
import { Component, OnInit, OnDestroy, signal, ChangeDetectionStrategy, inject } from '@angular/core';
|
2026-02-19 01:23:25 +04:00
|
|
|
|
import { DecimalPipe } from '@angular/common';
|
2026-01-18 18:57:06 +04:00
|
|
|
|
import { FormsModule } from '@angular/forms';
|
|
|
|
|
|
import { ActivatedRoute, RouterLink } from '@angular/router';
|
2026-02-26 21:54:21 +04:00
|
|
|
|
import { ApiService, CartService, TelegramService, SeoService } from '../../services';
|
2026-01-18 18:57:06 +04:00
|
|
|
|
import { Item } from '../../models';
|
|
|
|
|
|
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
|
|
|
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
|
|
import { environment } from '../../../environments/environment';
|
2026-02-26 21:54:21 +04:00
|
|
|
|
import { SecurityContext } from '@angular/core';
|
|
|
|
|
|
import { getDiscountedPrice } from '../../utils/item.utils';
|
2026-01-18 18:57:06 +04:00
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
|
selector: 'app-item-detail',
|
2026-02-19 01:23:25 +04:00
|
|
|
|
imports: [DecimalPipe, RouterLink, FormsModule],
|
2026-01-18 18:57:06 +04:00
|
|
|
|
templateUrl: './item-detail.component.html',
|
|
|
|
|
|
styleUrls: ['./item-detail.component.scss'],
|
|
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
|
|
|
|
})
|
|
|
|
|
|
export class ItemDetailComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
item = signal<Item | null>(null);
|
|
|
|
|
|
selectedPhotoIndex = signal(0);
|
|
|
|
|
|
loading = signal(true);
|
|
|
|
|
|
error = signal<string | null>(null);
|
|
|
|
|
|
isnovo = environment.theme === 'novo';
|
|
|
|
|
|
|
|
|
|
|
|
newReview = {
|
|
|
|
|
|
rating: 0,
|
|
|
|
|
|
comment: '',
|
|
|
|
|
|
anonymous: false
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
reviewSubmitStatus = signal<'idle' | 'loading' | 'success' | 'error'>('idle');
|
|
|
|
|
|
|
|
|
|
|
|
private routeSubscription?: Subscription;
|
2026-02-26 21:54:21 +04:00
|
|
|
|
private reviewResetTimeout?: ReturnType<typeof setTimeout>;
|
|
|
|
|
|
private reviewErrorTimeout?: ReturnType<typeof setTimeout>;
|
|
|
|
|
|
private reloadTimeout?: ReturnType<typeof setTimeout>;
|
|
|
|
|
|
|
|
|
|
|
|
private seoService = inject(SeoService);
|
2026-01-18 18:57:06 +04:00
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
|
|
private apiService: ApiService,
|
|
|
|
|
|
private cartService: CartService,
|
|
|
|
|
|
private telegramService: TelegramService,
|
|
|
|
|
|
private sanitizer: DomSanitizer
|
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
|
this.routeSubscription = this.route.params.subscribe(params => {
|
|
|
|
|
|
const id = parseInt(params['id'], 10);
|
|
|
|
|
|
this.loadItem(id);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
|
this.routeSubscription?.unsubscribe();
|
2026-02-26 21:54:21 +04:00
|
|
|
|
if (this.reviewResetTimeout) clearTimeout(this.reviewResetTimeout);
|
|
|
|
|
|
if (this.reviewErrorTimeout) clearTimeout(this.reviewErrorTimeout);
|
|
|
|
|
|
if (this.reloadTimeout) clearTimeout(this.reloadTimeout);
|
|
|
|
|
|
this.seoService.resetToDefaults();
|
2026-01-18 18:57:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
loadItem(itemID: number): void {
|
|
|
|
|
|
this.loading.set(true);
|
|
|
|
|
|
|
|
|
|
|
|
this.apiService.getItem(itemID).subscribe({
|
|
|
|
|
|
next: (item) => {
|
|
|
|
|
|
this.item.set(item);
|
2026-02-26 21:54:21 +04:00
|
|
|
|
this.seoService.setItemMeta(item);
|
2026-01-18 18:57:06 +04:00
|
|
|
|
this.loading.set(false);
|
|
|
|
|
|
},
|
|
|
|
|
|
error: (err) => {
|
|
|
|
|
|
this.error.set('Failed to load item');
|
|
|
|
|
|
this.loading.set(false);
|
|
|
|
|
|
console.error('Error loading item:', err);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
selectPhoto(index: number): void {
|
|
|
|
|
|
this.selectedPhotoIndex.set(index);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addToCart(): void {
|
|
|
|
|
|
const currentItem = this.item();
|
|
|
|
|
|
if (currentItem) {
|
|
|
|
|
|
this.cartService.addItem(currentItem.itemID);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getDiscountedPrice(): number {
|
|
|
|
|
|
const currentItem = this.item();
|
|
|
|
|
|
if (!currentItem) return 0;
|
2026-02-26 21:54:21 +04:00
|
|
|
|
return getDiscountedPrice(currentItem);
|
2026-01-18 18:57:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getSafeHtml(html: string): SafeHtml {
|
2026-02-26 21:54:21 +04:00
|
|
|
|
return this.sanitizer.sanitize(SecurityContext.HTML, html) || '';
|
2026-01-18 18:57:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getRatingStars(rating: number): string {
|
|
|
|
|
|
const fullStars = Math.floor(rating);
|
|
|
|
|
|
const hasHalfStar = rating % 1 >= 0.5;
|
2026-02-26 21:54:21 +04:00
|
|
|
|
let stars = '★'.repeat(fullStars);
|
|
|
|
|
|
if (hasHalfStar) stars += '☆';
|
2026-01-18 18:57:06 +04:00
|
|
|
|
return stars;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
formatDate(timestamp: string): string {
|
|
|
|
|
|
const date = new Date(timestamp);
|
|
|
|
|
|
const now = new Date();
|
|
|
|
|
|
const diffMs = now.getTime() - date.getTime();
|
|
|
|
|
|
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
|
|
|
|
|
|
2026-02-26 21:54:21 +04:00
|
|
|
|
if (diffDays === 0) return 'Сегодня';
|
|
|
|
|
|
if (diffDays === 1) return 'Вчера';
|
|
|
|
|
|
if (diffDays < 7) return `${diffDays} дн. назад`;
|
|
|
|
|
|
if (diffDays < 30) return `${Math.floor(diffDays / 7)} нед. назад`;
|
2026-01-18 18:57:06 +04:00
|
|
|
|
|
|
|
|
|
|
return date.toLocaleDateString('ru-RU', {
|
|
|
|
|
|
day: 'numeric',
|
|
|
|
|
|
month: 'long',
|
|
|
|
|
|
year: 'numeric'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setRating(rating: number): void {
|
|
|
|
|
|
this.newReview.rating = rating;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getUserDisplayName(): string | null {
|
|
|
|
|
|
if (!this.telegramService.isTelegramApp()) {
|
2026-02-26 21:54:21 +04:00
|
|
|
|
return 'Пользователь';
|
2026-01-18 18:57:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
return this.telegramService.getDisplayName();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
submitReview(): void {
|
|
|
|
|
|
if (!this.newReview.rating || !this.newReview.comment.trim()) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const currentItem = this.item();
|
|
|
|
|
|
if (!currentItem) return;
|
|
|
|
|
|
|
|
|
|
|
|
this.reviewSubmitStatus.set('loading');
|
|
|
|
|
|
|
|
|
|
|
|
const reviewData = {
|
|
|
|
|
|
itemID: currentItem.itemID,
|
|
|
|
|
|
rating: this.newReview.rating,
|
|
|
|
|
|
comment: this.newReview.comment.trim(),
|
|
|
|
|
|
username: this.newReview.anonymous ? null : this.getUserDisplayName(),
|
|
|
|
|
|
userId: this.telegramService.getUserId(),
|
|
|
|
|
|
timestamp: new Date().toISOString()
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.apiService.submitReview(reviewData).subscribe({
|
|
|
|
|
|
next: (response) => {
|
|
|
|
|
|
this.reviewSubmitStatus.set('success');
|
|
|
|
|
|
this.newReview = { rating: 0, comment: '', anonymous: false };
|
|
|
|
|
|
|
2026-02-26 21:54:21 +04:00
|
|
|
|
// Сброс состояния через 3 секунды
|
|
|
|
|
|
this.reviewResetTimeout = setTimeout(() => {
|
2026-01-18 18:57:06 +04:00
|
|
|
|
this.reviewSubmitStatus.set('idle');
|
|
|
|
|
|
}, 3000);
|
|
|
|
|
|
|
2026-02-26 21:54:21 +04:00
|
|
|
|
// Перезагрузить данные товара после отправки отзыва
|
|
|
|
|
|
this.reloadTimeout = setTimeout(() => {
|
2026-01-18 18:57:06 +04:00
|
|
|
|
this.loadItem(currentItem.itemID);
|
|
|
|
|
|
}, 500);
|
|
|
|
|
|
},
|
|
|
|
|
|
error: (err) => {
|
|
|
|
|
|
console.error('Error submitting review:', err);
|
|
|
|
|
|
this.reviewSubmitStatus.set('error');
|
|
|
|
|
|
|
2026-02-26 21:54:21 +04:00
|
|
|
|
// Сброс состояния об ошибке через 5 секунд
|
|
|
|
|
|
this.reviewErrorTimeout = setTimeout(() => {
|
2026-01-18 18:57:06 +04:00
|
|
|
|
this.reviewSubmitStatus.set('idle');
|
|
|
|
|
|
}, 5000);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|