Files
marketplaces/src/app/components/product-card/product-card.component.ts

105 lines
3.5 KiB
TypeScript
Raw Normal View History

2026-07-05 01:05:48 +04:00
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { DecimalPipe } from '@angular/common';
import { RouterLink } from '@angular/router';
2026-07-05 01:36:21 +04:00
import { Product } from '../../core/products/models/product-domain.model';
2026-07-05 01:05:48 +04:00
import { LangRoutePipe } from '../../pipes/lang-route.pipe';
import { TranslatePipe } from '../../i18n/translate.pipe';
2026-07-05 01:05:48 +04:00
import { getBadgeClass, getDiscountedPrice, getMainImage } from '../../utils/item.utils';
const STOCK_LABEL_KEYS: Record<string, string> = {
high: 'catalog.stockHigh',
medium: 'catalog.stockMedium',
low: 'catalog.stockLow',
out: 'catalog.stockOut'
};
2026-07-05 01:05:48 +04:00
export type ProductCardAppearance = 'standard' | 'compact';
@Component({
selector: 'app-product-card',
standalone: true,
imports: [DecimalPipe, RouterLink, LangRoutePipe, TranslatePipe],
2026-07-05 01:05:48 +04:00
templateUrl: './product-card.component.html',
styleUrls: ['./product-card.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ProductCardComponent {
2026-07-05 01:36:21 +04:00
@Input({ required: true }) item!: Product;
2026-07-05 01:05:48 +04:00
@Input() title = '';
@Input() description = '';
@Input() addToCartLabel = '';
2026-07-05 01:05:48 +04:00
@Input() appearance: ProductCardAppearance = 'standard';
@Input() showDescription = false;
@Input() showStock = true;
@Input() showRating = true;
@Input() showDiscountBadge = true;
@Input() showFavoritePlaceholder = false;
@Input() showComparePlaceholder = false;
@Input() showQuickViewPlaceholder = false;
@Input() showFavoriteAction = false;
@Input() showCompareAction = false;
@Input() showShareAction = false;
@Input() isFavorite = false;
@Input() isCompared = false;
2026-07-05 01:05:48 +04:00
@Output() addToCart = new EventEmitter<{ itemID: number; event: Event }>();
@Output() preview = new EventEmitter<number>();
2026-07-05 01:36:21 +04:00
@Output() selected = new EventEmitter<{ itemID: number; event: Event }>();
@Output() favoritePlaceholder = new EventEmitter<number>();
@Output() comparePlaceholder = new EventEmitter<number>();
@Output() quickViewPlaceholder = new EventEmitter<number>();
@Output() favoriteToggled = new EventEmitter<number>();
@Output() compareToggled = new EventEmitter<number>();
@Output() shareRequested = new EventEmitter<number>();
2026-07-05 01:05:48 +04:00
readonly getMainImage = getMainImage;
readonly getDiscountedPrice = getDiscountedPrice;
readonly getBadgeClass = getBadgeClass;
onAddToCart(event: Event): void {
this.addToCart.emit({ itemID: this.item.itemID, event });
}
2026-07-05 01:36:21 +04:00
onSelected(event: Event): void {
this.selected.emit({ itemID: this.item.itemID, event });
}
onFavoritePlaceholder(event: Event): void {
event.preventDefault();
event.stopPropagation();
this.favoritePlaceholder.emit(this.item.itemID);
this.favoriteToggled.emit(this.item.itemID);
}
onComparePlaceholder(event: Event): void {
event.preventDefault();
event.stopPropagation();
this.comparePlaceholder.emit(this.item.itemID);
this.compareToggled.emit(this.item.itemID);
}
onQuickViewPlaceholder(event: Event): void {
event.preventDefault();
event.stopPropagation();
this.quickViewPlaceholder.emit(this.item.itemID);
}
onShare(event: Event): void {
event.preventDefault();
event.stopPropagation();
this.shareRequested.emit(this.item.itemID);
}
stockLabelKey(remainings: string): string {
return STOCK_LABEL_KEYS[remainings.toLowerCase()] ?? STOCK_LABEL_KEYS['high'];
}
showFavoriteControl(): boolean {
return this.showFavoriteAction || this.showFavoritePlaceholder;
}
showCompareControl(): boolean {
return this.showCompareAction || this.showComparePlaceholder;
}
2026-07-05 01:05:48 +04:00
}