feat: enhance product attributes and event date handling for improved user experience and data management
This commit is contained in:
@@ -24,13 +24,24 @@ export interface ProductAttributeOption {
|
|||||||
metadata: Record<string, unknown> | null;
|
metadata: Record<string, unknown> | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ProductAttributeType =
|
||||||
|
| 'string'
|
||||||
|
| 'number'
|
||||||
|
| 'boolean'
|
||||||
|
| 'select'
|
||||||
|
| 'multiselect'
|
||||||
|
| 'color'
|
||||||
|
| 'image'
|
||||||
|
| 'event_date';
|
||||||
|
|
||||||
export interface ProductAttribute {
|
export interface ProductAttribute {
|
||||||
id: number;
|
id: number;
|
||||||
codigo: string;
|
codigo: string;
|
||||||
nombre: string;
|
nombre: string;
|
||||||
is_required: boolean;
|
is_required: boolean;
|
||||||
|
allow_multi_select?: boolean;
|
||||||
metadata_schema: Record<string, unknown> | null;
|
metadata_schema: Record<string, unknown> | null;
|
||||||
type: string;
|
type: ProductAttributeType;
|
||||||
options: ProductAttributeOption[];
|
options: ProductAttributeOption[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,23 +49,17 @@ export type InventoryPolicy = 'tracked' | 'unlimited';
|
|||||||
|
|
||||||
export interface CatalogItemVariant {
|
export interface CatalogItemVariant {
|
||||||
id: number;
|
id: number;
|
||||||
|
descripcion?: string | null;
|
||||||
|
precio?: string;
|
||||||
event_date_id?: number | null;
|
event_date_id?: number | null;
|
||||||
|
event_date_ids?: number[];
|
||||||
|
event_dates?: string[];
|
||||||
stock_tecnico: number | null;
|
stock_tecnico: number | null;
|
||||||
minimum_use_date?: string | null;
|
minimum_use_date?: string | null;
|
||||||
maximum_use_date?: string | null;
|
maximum_use_date?: string | null;
|
||||||
effective_minimum_use_date?: string | null;
|
effective_minimum_use_date?: string | null;
|
||||||
effective_maximum_use_date?: string | null;
|
effective_maximum_use_date?: string | null;
|
||||||
values: Record<string, string>;
|
values: Record<string, string | string[]>;
|
||||||
}
|
|
||||||
|
|
||||||
export interface CatalogItemEventDate {
|
|
||||||
id: number;
|
|
||||||
variant_id: number;
|
|
||||||
date: string;
|
|
||||||
starts_at: string;
|
|
||||||
ends_at: string;
|
|
||||||
label: string;
|
|
||||||
stock_tecnico: number | null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SelectedCatalogItemVariant extends CatalogItemVariant {
|
export interface SelectedCatalogItemVariant extends CatalogItemVariant {
|
||||||
@@ -74,12 +79,12 @@ export interface CatalogItemDetail {
|
|||||||
category: string | null;
|
category: string | null;
|
||||||
brand: string | null;
|
brand: string | null;
|
||||||
inventory_policy: InventoryPolicy;
|
inventory_policy: InventoryPolicy;
|
||||||
|
max_units_per_user?: number | null;
|
||||||
has_tickets: boolean;
|
has_tickets: boolean;
|
||||||
minimum_use_date: string | null;
|
minimum_use_date: string | null;
|
||||||
maximum_use_date: string | null;
|
maximum_use_date: string | null;
|
||||||
attributes: ProductAttribute[];
|
attributes: ProductAttribute[];
|
||||||
variants: CatalogItemVariant[];
|
variants: CatalogItemVariant[];
|
||||||
event_dates?: CatalogItemEventDate[];
|
|
||||||
selected_variant?: SelectedCatalogItemVariant;
|
selected_variant?: SelectedCatalogItemVariant;
|
||||||
stock_tecnico?: number | null;
|
stock_tecnico?: number | null;
|
||||||
images?: string[];
|
images?: string[];
|
||||||
@@ -90,8 +95,10 @@ export type CatalogGroupLayout = 'paginated' | 'simple' | 'simple_vertical' | 'c
|
|||||||
|
|
||||||
export interface CatalogFeaturedItemVariant {
|
export interface CatalogFeaturedItemVariant {
|
||||||
id: number;
|
id: number;
|
||||||
|
descripcion?: string | null;
|
||||||
|
precio?: string;
|
||||||
stock_tecnico: number | null;
|
stock_tecnico: number | null;
|
||||||
values: Record<string, string>;
|
values: Record<string, string | string[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CatalogFeaturedItem {
|
export interface CatalogFeaturedItem {
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ export interface PurchaseStatusResponse {
|
|||||||
|
|
||||||
export interface PurchaseSummaryResponse extends PurchaseStatusResponse {
|
export interface PurchaseSummaryResponse extends PurchaseStatusResponse {
|
||||||
id: number;
|
id: number;
|
||||||
event_id: number | null;
|
|
||||||
created_at: string | null;
|
created_at: string | null;
|
||||||
total: string;
|
total: string;
|
||||||
}
|
}
|
||||||
@@ -59,7 +58,6 @@ export interface PurchaseDetailResponse extends PurchaseStatusResponse {
|
|||||||
id: number;
|
id: number;
|
||||||
cart_id: number | null;
|
cart_id: number | null;
|
||||||
tenant_codigo: string;
|
tenant_codigo: string;
|
||||||
event_id: number | null;
|
|
||||||
user_id: number;
|
user_id: number;
|
||||||
created_at: string | null;
|
created_at: string | null;
|
||||||
payment_method: string | null;
|
payment_method: string | null;
|
||||||
|
|||||||
@@ -54,10 +54,9 @@ export interface ActiveEventDate {
|
|||||||
time_end: string;
|
time_end: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ActiveEvent {
|
export interface TenantEvent {
|
||||||
id: number;
|
title: string;
|
||||||
name: string;
|
location: string;
|
||||||
address: string;
|
|
||||||
dates: ActiveEventDate[];
|
dates: ActiveEventDate[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,8 +107,7 @@ export interface Tenant {
|
|||||||
website_type_code?: string | null;
|
website_type_code?: string | null;
|
||||||
website_type?: WebsiteType | null;
|
website_type?: WebsiteType | null;
|
||||||
extras?: WebsiteExtras;
|
extras?: WebsiteExtras;
|
||||||
active_event_id?: number | null;
|
event?: TenantEvent | null;
|
||||||
active_event?: ActiveEvent | null;
|
|
||||||
selected_bank_account_id?: number | null;
|
selected_bank_account_id?: number | null;
|
||||||
selected_bank_account?: BankAccount | null;
|
selected_bank_account?: BankAccount | null;
|
||||||
search_product_layout?: 'row' | 'column_with_image' | 'column_with_cart';
|
search_product_layout?: 'row' | 'column_with_image' | 'column_with_cart';
|
||||||
|
|||||||
17
src/app/core/services/validity-time.interface.ts
Normal file
17
src/app/core/services/validity-time.interface.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
export interface TimeWindowValidityTime {
|
||||||
|
id: number;
|
||||||
|
type: 'time_window';
|
||||||
|
is_valid: boolean;
|
||||||
|
start_time?: string;
|
||||||
|
end_time?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FixedWindowValidityTime {
|
||||||
|
id: number;
|
||||||
|
type: 'fixed_window';
|
||||||
|
is_valid: boolean;
|
||||||
|
fixed_starts_at?: string;
|
||||||
|
fixed_expires_at?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ValidityTime = TimeWindowValidityTime | FixedWindowValidityTime;
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<div class="attribute-selector">
|
<div class="attribute-selector">
|
||||||
@for (attribute of attributes(); track attribute.id) {
|
@for (attribute of attributes(); track attribute.codigo) {
|
||||||
<div class="attribute-selector__row">
|
<div class="attribute-selector__row">
|
||||||
<span class="attribute-selector__label">{{ attribute.nombre }}:</span>
|
<span class="attribute-selector__label">{{ attribute.nombre }}:</span>
|
||||||
|
|
||||||
@@ -10,12 +10,14 @@
|
|||||||
type="button"
|
type="button"
|
||||||
class="attribute-selector__swatch"
|
class="attribute-selector__swatch"
|
||||||
[class.attribute-selector__swatch--selected]="hasSelectedOption(attribute, option)"
|
[class.attribute-selector__swatch--selected]="hasSelectedOption(attribute, option)"
|
||||||
[class.attribute-selector__swatch--disabled]="!availableOptions()[attribute.id][option.id]"
|
[class.attribute-selector__swatch--disabled]="
|
||||||
|
!availableOptions()[attribute.codigo][option.id]
|
||||||
|
"
|
||||||
[style.background-color]="getOptionSwatchColor(option)"
|
[style.background-color]="getOptionSwatchColor(option)"
|
||||||
[attr.aria-label]="option.label"
|
[attr.aria-label]="option.label"
|
||||||
[attr.aria-pressed]="hasSelectedOption(attribute, option)"
|
[attr.aria-pressed]="hasSelectedOption(attribute, option)"
|
||||||
[title]="option.label"
|
[title]="option.label"
|
||||||
[disabled]="!availableOptions()[attribute.id][option.id]"
|
[disabled]="!availableOptions()[attribute.codigo][option.id]"
|
||||||
(click)="selectAttributeOption(attribute, option)"
|
(click)="selectAttributeOption(attribute, option)"
|
||||||
>
|
>
|
||||||
<span class="visually-hidden">{{ option.label }}</span>
|
<span class="visually-hidden">{{ option.label }}</span>
|
||||||
@@ -24,10 +26,14 @@
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
class="attribute-selector__text-option"
|
class="attribute-selector__text-option"
|
||||||
[class.attribute-selector__text-option--selected]="hasSelectedOption(attribute, option)"
|
[class.attribute-selector__text-option--selected]="
|
||||||
[class.attribute-selector__text-option--disabled]="!availableOptions()[attribute.id][option.id]"
|
hasSelectedOption(attribute, option)
|
||||||
|
"
|
||||||
|
[class.attribute-selector__text-option--disabled]="
|
||||||
|
!availableOptions()[attribute.codigo][option.id]
|
||||||
|
"
|
||||||
[attr.aria-pressed]="hasSelectedOption(attribute, option)"
|
[attr.aria-pressed]="hasSelectedOption(attribute, option)"
|
||||||
[disabled]="!availableOptions()[attribute.id][option.id]"
|
[disabled]="!availableOptions()[attribute.codigo][option.id]"
|
||||||
(click)="selectAttributeOption(attribute, option)"
|
(click)="selectAttributeOption(attribute, option)"
|
||||||
>
|
>
|
||||||
{{ option.label }}
|
{{ option.label }}
|
||||||
|
|||||||
@@ -68,4 +68,46 @@ describe('ProductAttributeSelectorComponent', () => {
|
|||||||
expect(buttons[0].disabled).toBe(true);
|
expect(buttons[0].disabled).toBe(true);
|
||||||
expect(buttons[1].disabled).toBe(false);
|
expect(buttons[1].disabled).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('allows multiple event dates and emits the exact matching variant', () => {
|
||||||
|
const fixture = TestBed.createComponent(ProductAttributeSelectorComponent);
|
||||||
|
const emittedIds: Array<number | null> = [];
|
||||||
|
fixture.componentInstance.variantChange.subscribe((variant) =>
|
||||||
|
emittedIds.push(variant?.id ?? null),
|
||||||
|
);
|
||||||
|
fixture.componentRef.setInput('attributes', [
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
codigo: 'event_date',
|
||||||
|
nombre: 'Fecha',
|
||||||
|
is_required: true,
|
||||||
|
allow_multi_select: true,
|
||||||
|
metadata_schema: null,
|
||||||
|
type: 'event_date',
|
||||||
|
options: [
|
||||||
|
{ id: 101, value: '1', label: '09/10/2026', sort_order: 0, metadata: null },
|
||||||
|
{ id: 102, value: '2', label: '10/10/2026', sort_order: 1, metadata: null },
|
||||||
|
],
|
||||||
|
} satisfies ProductAttribute,
|
||||||
|
]);
|
||||||
|
fixture.componentRef.setInput('inventoryPolicy', 'unlimited');
|
||||||
|
fixture.componentRef.setInput('variants', [
|
||||||
|
{ id: 1, stock_tecnico: null, values: { event_date: '1' } },
|
||||||
|
{ id: 2, stock_tecnico: null, values: { event_date: '2' } },
|
||||||
|
{ id: 3, stock_tecnico: null, values: { event_date: ['1', '2'] } },
|
||||||
|
]);
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
const buttons = Array.from(
|
||||||
|
fixture.nativeElement.querySelectorAll('.attribute-selector__text-option'),
|
||||||
|
) as HTMLButtonElement[];
|
||||||
|
buttons[0].click();
|
||||||
|
fixture.detectChanges();
|
||||||
|
buttons[1].click();
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
expect(buttons[0].getAttribute('aria-pressed')).toBe('true');
|
||||||
|
expect(buttons[1].getAttribute('aria-pressed')).toBe('true');
|
||||||
|
expect(emittedIds).toContain(3);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -32,48 +32,65 @@ export class ProductAttributeSelectorComponent {
|
|||||||
|
|
||||||
public variantChange = output<CatalogItemVariant | null>();
|
public variantChange = output<CatalogItemVariant | null>();
|
||||||
|
|
||||||
protected readonly selectedAttributeOptions = signal<Record<number, number>>({});
|
protected readonly selectedAttributeOptions = signal<Record<string, number[]>>({});
|
||||||
|
|
||||||
protected readonly availableOptions = computed(() => {
|
protected readonly availableOptions = computed(() => {
|
||||||
const selections = this.selectedAttributeOptions();
|
const selections = this.selectedAttributeOptions();
|
||||||
const variants = this.variants();
|
const variants = this.variants();
|
||||||
const attributes = this.attributes();
|
const attributes = this.attributes();
|
||||||
|
|
||||||
const availability: Record<number, Record<number, boolean>> = {};
|
const availability: Record<string, Record<number, boolean>> = {};
|
||||||
|
|
||||||
for (const attribute of attributes) {
|
for (const attribute of attributes) {
|
||||||
availability[attribute.id] = {};
|
availability[attribute.codigo] = {};
|
||||||
for (const option of attribute.options) {
|
for (const option of attribute.options) {
|
||||||
const optionNormalized = this.normalizeText(option.value || option.label);
|
const optionNormalized = this.normalizeText(option.value || option.label);
|
||||||
|
const selectedForAttribute = selections[attribute.codigo] ?? [];
|
||||||
|
|
||||||
|
if (
|
||||||
|
!attribute.allow_multi_select &&
|
||||||
|
selectedForAttribute.length >= 1 &&
|
||||||
|
!selectedForAttribute.includes(option.id)
|
||||||
|
) {
|
||||||
|
availability[attribute.codigo][option.id] = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const isAvailable = variants.some((variant) => {
|
const isAvailable = variants.some((variant) => {
|
||||||
if (!this.isVariantAvailable(variant)) return false;
|
if (!this.isVariantAvailable(variant)) return false;
|
||||||
|
|
||||||
const variantAttrValue = this.getDefaultVariantAttributeValue(attribute, variant.values);
|
const variantAttrValues = this.getVariantAttributeValues(attribute, variant.values);
|
||||||
if (!variantAttrValue || this.normalizeText(variantAttrValue) !== optionNormalized) {
|
const desiredOptionIds = selectedForAttribute.includes(option.id)
|
||||||
|
? selectedForAttribute
|
||||||
|
: [...selectedForAttribute, option.id];
|
||||||
|
const desiredValues = desiredOptionIds
|
||||||
|
.map((id) => attribute.options.find((candidate) => candidate.id === id))
|
||||||
|
.filter((candidate): candidate is ProductAttributeOption => candidate !== undefined)
|
||||||
|
.map((candidate) => this.normalizeText(candidate.value || candidate.label));
|
||||||
|
|
||||||
|
if (
|
||||||
|
!variantAttrValues.includes(optionNormalized) ||
|
||||||
|
!desiredValues.every((value) => variantAttrValues.includes(value))
|
||||||
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const otherAttr of attributes) {
|
for (const otherAttr of attributes) {
|
||||||
if (otherAttr.id === attribute.id) continue;
|
if (otherAttr.codigo === attribute.codigo) continue;
|
||||||
const selectedOptionId = selections[otherAttr.id];
|
const selectedOptionIds = selections[otherAttr.codigo] ?? [];
|
||||||
if (selectedOptionId !== undefined) {
|
const selectedValues = selectedOptionIds
|
||||||
const selectedOption = otherAttr.options.find((o) => o.id === selectedOptionId);
|
.map((id) => otherAttr.options.find((candidate) => candidate.id === id))
|
||||||
if (selectedOption) {
|
.filter((candidate): candidate is ProductAttributeOption => candidate !== undefined)
|
||||||
const selectedNormalized = this.normalizeText(
|
.map((candidate) => this.normalizeText(candidate.value || candidate.label));
|
||||||
selectedOption.value || selectedOption.label,
|
const variantValues = this.getVariantAttributeValues(otherAttr, variant.values);
|
||||||
);
|
if (!selectedValues.every((value) => variantValues.includes(value))) {
|
||||||
const vAttrValue = this.getDefaultVariantAttributeValue(otherAttr, variant.values);
|
return false;
|
||||||
if (!vAttrValue || this.normalizeText(vAttrValue) !== selectedNormalized) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
availability[attribute.id][option.id] = isAvailable;
|
availability[attribute.codigo][option.id] = isAvailable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,17 +126,28 @@ export class ProductAttributeSelectorComponent {
|
|||||||
attribute: ProductAttribute,
|
attribute: ProductAttribute,
|
||||||
option: ProductAttributeOption,
|
option: ProductAttributeOption,
|
||||||
): boolean {
|
): boolean {
|
||||||
return this.selectedAttributeOptions()[attribute.id] === option.id;
|
return (this.selectedAttributeOptions()[attribute.codigo] ?? []).includes(option.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected selectAttributeOption(
|
protected selectAttributeOption(
|
||||||
attribute: ProductAttribute,
|
attribute: ProductAttribute,
|
||||||
option: ProductAttributeOption,
|
option: ProductAttributeOption,
|
||||||
): void {
|
): void {
|
||||||
this.selectedAttributeOptions.update((current) => ({
|
this.selectedAttributeOptions.update((current) => {
|
||||||
...current,
|
const selected = current[attribute.codigo] ?? [];
|
||||||
[attribute.id]: option.id,
|
const isMultiple = attribute.allow_multi_select ?? false;
|
||||||
}));
|
let next: number[];
|
||||||
|
|
||||||
|
if (!isMultiple) {
|
||||||
|
next = [option.id];
|
||||||
|
} else if (selected.includes(option.id)) {
|
||||||
|
next = selected.filter((id) => id !== option.id);
|
||||||
|
} else {
|
||||||
|
next = [...selected, option.id];
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...current, [attribute.codigo]: next };
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected isColorOption(option: ProductAttributeOption): boolean {
|
protected isColorOption(option: ProductAttributeOption): boolean {
|
||||||
@@ -134,51 +162,44 @@ export class ProductAttributeSelectorComponent {
|
|||||||
attributes: ProductAttribute[],
|
attributes: ProductAttribute[],
|
||||||
defaultVariant: CatalogItemVariant | null,
|
defaultVariant: CatalogItemVariant | null,
|
||||||
): void {
|
): void {
|
||||||
const selections: Record<number, number> = {};
|
const selections: Record<string, number[]> = {};
|
||||||
|
|
||||||
for (const attribute of attributes) {
|
for (const attribute of attributes) {
|
||||||
const optionId = this.findDefaultOptionId(attribute, defaultVariant);
|
const optionIds = this.findDefaultOptionIds(attribute, defaultVariant);
|
||||||
if (optionId !== null) {
|
if (optionIds.length > 0) {
|
||||||
selections[attribute.id] = optionId;
|
selections[attribute.codigo] = optionIds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.selectedAttributeOptions.set(selections);
|
this.selectedAttributeOptions.set(selections);
|
||||||
}
|
}
|
||||||
|
|
||||||
private findDefaultOptionId(
|
private findDefaultOptionIds(
|
||||||
attribute: ProductAttribute,
|
attribute: ProductAttribute,
|
||||||
variant: CatalogItemVariant | null,
|
variant: CatalogItemVariant | null,
|
||||||
): number | null {
|
): number[] {
|
||||||
if (!variant) {
|
if (!variant) {
|
||||||
return null;
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultValue = this.getDefaultVariantAttributeValue(attribute, variant.values);
|
const defaultValues = this.getVariantAttributeValues(attribute, variant.values);
|
||||||
if (!defaultValue) {
|
if (defaultValues.length === 0) {
|
||||||
return null;
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizedValue = this.normalizeText(defaultValue);
|
return attribute.options
|
||||||
const matchByValue = attribute.options.find(
|
.filter(
|
||||||
(option) => this.normalizeText(option.value) === normalizedValue,
|
(option) =>
|
||||||
);
|
defaultValues.includes(this.normalizeText(option.value)) ||
|
||||||
|
defaultValues.includes(this.normalizeText(option.label)),
|
||||||
if (matchByValue) {
|
)
|
||||||
return matchByValue.id;
|
.map((option) => option.id);
|
||||||
}
|
|
||||||
|
|
||||||
const matchByLabel = attribute.options.find(
|
|
||||||
(option) => this.normalizeText(option.label) === normalizedValue,
|
|
||||||
);
|
|
||||||
|
|
||||||
return matchByLabel?.id ?? null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private getDefaultVariantAttributeValue(
|
private getVariantAttributeValues(
|
||||||
attribute: ProductAttribute,
|
attribute: ProductAttribute,
|
||||||
variantAttributes: Record<string, string>,
|
variantAttributes: Record<string, string | string[]>,
|
||||||
): string | null {
|
): string[] {
|
||||||
const normalizedCodigo = this.normalizeText(attribute.codigo);
|
const normalizedCodigo = this.normalizeText(attribute.codigo);
|
||||||
const normalizedNombre = this.normalizeText(attribute.nombre);
|
const normalizedNombre = this.normalizeText(attribute.nombre);
|
||||||
|
|
||||||
@@ -186,11 +207,11 @@ export class ProductAttributeSelectorComponent {
|
|||||||
const normalizedKey = this.normalizeText(key);
|
const normalizedKey = this.normalizeText(key);
|
||||||
|
|
||||||
if (normalizedKey === normalizedCodigo || normalizedKey === normalizedNombre) {
|
if (normalizedKey === normalizedCodigo || normalizedKey === normalizedNombre) {
|
||||||
return value;
|
return (Array.isArray(value) ? value : [value]).map((item) => this.normalizeText(item));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
private normalizeText(value: string | null | undefined): string {
|
private normalizeText(value: string | null | undefined): string {
|
||||||
@@ -234,7 +255,7 @@ export class ProductAttributeSelectorComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private emitMatchingVariant(
|
private emitMatchingVariant(
|
||||||
selections: Record<number, number>,
|
selections: Record<string, number[]>,
|
||||||
variants: CatalogItemVariant[],
|
variants: CatalogItemVariant[],
|
||||||
attributes: ProductAttribute[],
|
attributes: ProductAttribute[],
|
||||||
): void {
|
): void {
|
||||||
@@ -243,18 +264,22 @@ export class ProductAttributeSelectorComponent {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const selectedValuesById: Record<number, string> = {};
|
const selectedValuesByCode: Record<string, string[]> = {};
|
||||||
let allSelected = true;
|
let allSelected = true;
|
||||||
for (const attr of attributes) {
|
for (const attr of attributes) {
|
||||||
const selectedOptionId = selections[attr.id];
|
const selectedOptionIds = selections[attr.codigo] ?? [];
|
||||||
if (selectedOptionId === undefined) {
|
if (
|
||||||
|
selectedOptionIds.length === 0 ||
|
||||||
|
(!attr.allow_multi_select && selectedOptionIds.length !== 1)
|
||||||
|
) {
|
||||||
allSelected = false;
|
allSelected = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const option = attr.options.find((o) => o.id === selectedOptionId);
|
selectedValuesByCode[attr.codigo] = selectedOptionIds
|
||||||
if (option) {
|
.map((id) => attr.options.find((option) => option.id === id))
|
||||||
selectedValuesById[attr.id] = this.normalizeText(option.value || option.label);
|
.filter((option): option is ProductAttributeOption => option !== undefined)
|
||||||
}
|
.map((option) => this.normalizeText(option.value || option.label))
|
||||||
|
.sort();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!allSelected) {
|
if (!allSelected) {
|
||||||
@@ -264,10 +289,12 @@ export class ProductAttributeSelectorComponent {
|
|||||||
|
|
||||||
const matchingVariant = variants.find((variant) => {
|
const matchingVariant = variants.find((variant) => {
|
||||||
return attributes.every((attr) => {
|
return attributes.every((attr) => {
|
||||||
const selectedValue = selectedValuesById[attr.id];
|
const selectedValues = selectedValuesByCode[attr.codigo];
|
||||||
const variantValue = this.getDefaultVariantAttributeValue(attr, variant.values);
|
const variantValues = this.getVariantAttributeValues(attr, variant.values).sort();
|
||||||
if (!variantValue) return false;
|
return (
|
||||||
return this.normalizeText(variantValue) === selectedValue;
|
selectedValues.length === variantValues.length &&
|
||||||
|
selectedValues.every((value, index) => value === variantValues[index])
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
|
|
||||||
<div class="product-detail__price-group">
|
<div class="product-detail__price-group">
|
||||||
<span class="product-detail__price-current">
|
<span class="product-detail__price-current">
|
||||||
{{ getFormattedPrice(prod.precio) }}
|
{{ getFormattedPrice(effectivePrice()) }}
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
@if (oldPrice(); as oldPrice) {
|
@if (oldPrice(); as oldPrice) {
|
||||||
@@ -35,31 +35,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@if (hasEventDates()) {
|
|
||||||
<div class="product-detail__divider"></div>
|
|
||||||
|
|
||||||
<section class="product-detail__section product-detail__section--event-date">
|
|
||||||
<label class="form-label" for="event-date-selector">Fecha</label>
|
|
||||||
<select
|
|
||||||
id="event-date-selector"
|
|
||||||
class="form-select"
|
|
||||||
[value]="selectedVariant()?.id ?? ''"
|
|
||||||
(change)="onEventDateChange($event)"
|
|
||||||
>
|
|
||||||
@for (eventDate of eventDates(); track eventDate.id) {
|
|
||||||
<option
|
|
||||||
[value]="eventDate.variant_id"
|
|
||||||
[disabled]="
|
|
||||||
prod.inventory_policy !== 'unlimited' && (eventDate.stock_tecnico ?? 0) <= 0
|
|
||||||
"
|
|
||||||
>
|
|
||||||
{{ eventDate.label }}
|
|
||||||
</option>
|
|
||||||
}
|
|
||||||
</select>
|
|
||||||
</section>
|
|
||||||
}
|
|
||||||
|
|
||||||
@if (hasRenderableAttributes()) {
|
@if (hasRenderableAttributes()) {
|
||||||
<div class="product-detail__divider"></div>
|
<div class="product-detail__divider"></div>
|
||||||
|
|
||||||
@@ -124,7 +99,7 @@
|
|||||||
[class.product-detail__description-body--expanded]="descriptionExpanded()"
|
[class.product-detail__description-body--expanded]="descriptionExpanded()"
|
||||||
[style.max-height.px]="descriptionExpanded() ? descriptionMaxHeight() || null : null"
|
[style.max-height.px]="descriptionExpanded() ? descriptionMaxHeight() || null : null"
|
||||||
>
|
>
|
||||||
{{ prod.descripcion }}
|
{{ effectiveDescription() }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@if (showDescriptionToggle()) {
|
@if (showDescriptionToggle()) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import {
|
|||||||
describe('ProductDetailPageComponent', () => {
|
describe('ProductDetailPageComponent', () => {
|
||||||
const mockProduct: CatalogItemDetail = {
|
const mockProduct: CatalogItemDetail = {
|
||||||
id: 1,
|
id: 1,
|
||||||
|
type: 'product',
|
||||||
category_id: 10,
|
category_id: 10,
|
||||||
brand_id: null,
|
brand_id: null,
|
||||||
slug: 'auriculares-bluetooth',
|
slug: 'auriculares-bluetooth',
|
||||||
@@ -327,33 +328,39 @@ describe('ProductDetailPageComponent', () => {
|
|||||||
expect((swatches[0] as HTMLElement).style.backgroundColor).not.toBe('');
|
expect((swatches[0] as HTMLElement).style.backgroundColor).not.toBe('');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('renders event dates and resolves the selected date through its variant', async () => {
|
it('renders event dates as a dynamic attribute and resolves their variants', async () => {
|
||||||
const detailProduct: CatalogItemDetail = {
|
const detailProduct: CatalogItemDetail = {
|
||||||
...mockProduct,
|
...mockProduct,
|
||||||
purpose: 'entry',
|
purpose: 'entry',
|
||||||
has_tickets: true,
|
has_tickets: true,
|
||||||
variants: [
|
variants: [
|
||||||
{ id: 101, event_date_id: 20, stock_tecnico: 10, values: {} },
|
{ id: 101, event_date_id: 20, stock_tecnico: 10, values: { event_date: '20' } },
|
||||||
{ id: 102, event_date_id: 21, stock_tecnico: 10, values: {} },
|
{ id: 102, event_date_id: 21, stock_tecnico: 10, values: { event_date: '21' } },
|
||||||
],
|
],
|
||||||
event_dates: [
|
attributes: [
|
||||||
{
|
{
|
||||||
id: 20,
|
id: 99,
|
||||||
variant_id: 101,
|
codigo: 'event_date',
|
||||||
date: '2026-10-09',
|
nombre: 'Fecha',
|
||||||
starts_at: '2026-10-09T10:00:00.000000Z',
|
is_required: true,
|
||||||
ends_at: '2026-10-09T20:00:00.000000Z',
|
metadata_schema: null,
|
||||||
label: 'Viernes 9 de octubre - 10:00 a 20:00',
|
type: 'event_date',
|
||||||
stock_tecnico: 10,
|
options: [
|
||||||
},
|
{
|
||||||
{
|
id: 20,
|
||||||
id: 21,
|
value: '20',
|
||||||
variant_id: 102,
|
label: '09/10/2026 · 10:00 a 20:00',
|
||||||
date: '2026-10-10',
|
sort_order: 0,
|
||||||
starts_at: '2026-10-10T10:00:00.000000Z',
|
metadata: null,
|
||||||
ends_at: '2026-10-10T20:00:00.000000Z',
|
},
|
||||||
label: 'Sábado 10 de octubre - 10:00 a 20:00',
|
{
|
||||||
stock_tecnico: 10,
|
id: 21,
|
||||||
|
value: '21',
|
||||||
|
label: '10/10/2026 · 10:00 a 20:00',
|
||||||
|
sort_order: 1,
|
||||||
|
metadata: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
selected_variant: {
|
selected_variant: {
|
||||||
@@ -373,12 +380,13 @@ describe('ProductDetailPageComponent', () => {
|
|||||||
const fixture = TestBed.createComponent(ProductDetailPageComponent);
|
const fixture = TestBed.createComponent(ProductDetailPageComponent);
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
const select = fixture.nativeElement.querySelector('#event-date-selector') as HTMLSelectElement;
|
const options = fixture.nativeElement.querySelectorAll(
|
||||||
expect(select.options).toHaveLength(2);
|
'.attribute-selector__text-option',
|
||||||
expect(select.value).toBe('101');
|
) as NodeListOf<HTMLButtonElement>;
|
||||||
|
expect(options).toHaveLength(2);
|
||||||
|
expect(options[0].classList.contains('attribute-selector__text-option--selected')).toBe(true);
|
||||||
|
|
||||||
select.value = '102';
|
options[1].click();
|
||||||
select.dispatchEvent(new Event('change'));
|
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
expect(catalogServiceStub.getCatalogItem).toHaveBeenCalledWith(1, 102);
|
expect(catalogServiceStub.getCatalogItem).toHaveBeenCalledWith(1, 102);
|
||||||
@@ -621,6 +629,39 @@ describe('ProductDetailPageComponent', () => {
|
|||||||
expect(fixture.componentInstance['quantity']()).toBe(2);
|
expect(fixture.componentInstance['quantity']()).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('caps an unlimited variant at the per-user purchase limit', async () => {
|
||||||
|
const unlimitedVariant = {
|
||||||
|
id: 322,
|
||||||
|
stock_tecnico: null,
|
||||||
|
values: {},
|
||||||
|
};
|
||||||
|
resolveProduct({
|
||||||
|
...mockProduct,
|
||||||
|
inventory_policy: 'unlimited',
|
||||||
|
max_units_per_user: 2,
|
||||||
|
selected_variant: {
|
||||||
|
id: 322,
|
||||||
|
stock_tecnico: null,
|
||||||
|
images: [],
|
||||||
|
values: { event_date: '20' },
|
||||||
|
},
|
||||||
|
variants: [unlimitedVariant],
|
||||||
|
});
|
||||||
|
|
||||||
|
await configureTestingModule();
|
||||||
|
const fixture = TestBed.createComponent(ProductDetailPageComponent);
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
const increaseButton = fixture.nativeElement.querySelector(
|
||||||
|
'app-quantity-selector button:last-child',
|
||||||
|
) as HTMLButtonElement;
|
||||||
|
increaseButton.click();
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
expect(fixture.componentInstance['quantity']()).toBe(2);
|
||||||
|
expect(increaseButton.disabled).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
it('disables purchase actions for tracked variants without stock', async () => {
|
it('disables purchase actions for tracked variants without stock', async () => {
|
||||||
const trackedVariant = {
|
const trackedVariant = {
|
||||||
id: 654,
|
id: 654,
|
||||||
|
|||||||
@@ -86,13 +86,28 @@ export class ProductDetailPageComponent implements OnInit, OnDestroy {
|
|||||||
protected readonly creatingDirectPurchase = signal(false);
|
protected readonly creatingDirectPurchase = signal(false);
|
||||||
protected readonly error = signal<string | null>(null);
|
protected readonly error = signal<string | null>(null);
|
||||||
protected readonly selectedVariant = signal<CatalogItemVariant | null>(null);
|
protected readonly selectedVariant = signal<CatalogItemVariant | null>(null);
|
||||||
|
protected readonly effectiveDescription = computed(
|
||||||
|
() => this.selectedVariant()?.descripcion ?? this.product()?.descripcion ?? '',
|
||||||
|
);
|
||||||
|
protected readonly effectivePrice = computed(
|
||||||
|
() => this.selectedVariant()?.precio ?? this.product()?.precio,
|
||||||
|
);
|
||||||
protected readonly quantity = signal(1);
|
protected readonly quantity = signal(1);
|
||||||
protected readonly selectedVariantMax = computed<number | null>(() => {
|
protected readonly selectedVariantMax = computed<number | null>(() => {
|
||||||
const prod = this.product();
|
const prod = this.product();
|
||||||
const variant = this.selectedVariant();
|
const variant = this.selectedVariant();
|
||||||
if (variant) return variant.stock_tecnico;
|
if (!prod) return 0;
|
||||||
if (prod && prod.variants.length === 0) return prod.stock_tecnico ?? null;
|
|
||||||
return 0;
|
const stockLimit = variant
|
||||||
|
? variant.stock_tecnico
|
||||||
|
: prod.variants.length === 0
|
||||||
|
? (prod.stock_tecnico ?? null)
|
||||||
|
: 0;
|
||||||
|
const userLimit = prod.max_units_per_user ?? null;
|
||||||
|
|
||||||
|
if (stockLimit === null) return userLimit;
|
||||||
|
if (userLimit === null) return stockLimit;
|
||||||
|
return Math.min(stockLimit, userLimit);
|
||||||
});
|
});
|
||||||
protected readonly selectedVariantAvailable = computed(() => {
|
protected readonly selectedVariantAvailable = computed(() => {
|
||||||
const prod = this.product();
|
const prod = this.product();
|
||||||
@@ -109,14 +124,8 @@ export class ProductDetailPageComponent implements OnInit, OnDestroy {
|
|||||||
protected readonly descriptionMaxHeight = signal(0);
|
protected readonly descriptionMaxHeight = signal(0);
|
||||||
protected readonly descriptionHasOverflow = signal(false);
|
protected readonly descriptionHasOverflow = signal(false);
|
||||||
protected readonly renderableAttributes = computed(() =>
|
protected readonly renderableAttributes = computed(() =>
|
||||||
(this.product()?.attributes ?? []).filter(
|
(this.product()?.attributes ?? []).filter((attribute) => attribute.options.length > 0),
|
||||||
(attribute) =>
|
|
||||||
attribute.options.length > 0 &&
|
|
||||||
!(this.product()?.purpose === 'entry' && attribute.codigo === 'fecha'),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
protected readonly eventDates = computed(() => this.product()?.event_dates ?? []);
|
|
||||||
protected readonly hasEventDates = computed(() => this.eventDates().length > 0);
|
|
||||||
protected readonly hasRenderableAttributes = computed(
|
protected readonly hasRenderableAttributes = computed(
|
||||||
() => this.renderableAttributes().length > 0,
|
() => this.renderableAttributes().length > 0,
|
||||||
);
|
);
|
||||||
@@ -130,6 +139,7 @@ export class ProductDetailPageComponent implements OnInit, OnDestroy {
|
|||||||
this.carouselHost();
|
this.carouselHost();
|
||||||
this.descriptionBody();
|
this.descriptionBody();
|
||||||
this.product();
|
this.product();
|
||||||
|
this.selectedVariant();
|
||||||
this.descriptionExpanded();
|
this.descriptionExpanded();
|
||||||
|
|
||||||
if (this.isBrowser) {
|
if (this.isBrowser) {
|
||||||
@@ -237,8 +247,11 @@ export class ProductDetailPageComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.selectedVariant.set(variant);
|
this.selectedVariant.set(variant);
|
||||||
if (variant && variant.stock_tecnico !== null && this.quantity() > variant.stock_tecnico) {
|
this.descriptionExpanded.set(false);
|
||||||
this.quantity.set(Math.max(1, variant.stock_tecnico));
|
this.descriptionHasOverflow.set(false);
|
||||||
|
const maximum = this.selectedVariantMax();
|
||||||
|
if (maximum !== null && this.quantity() > maximum) {
|
||||||
|
this.quantity.set(Math.max(1, maximum));
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentProduct = this.product();
|
const currentProduct = this.product();
|
||||||
@@ -247,12 +260,6 @@ export class ProductDetailPageComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected onEventDateChange(event: Event): void {
|
|
||||||
const variantId = Number((event.target as HTMLSelectElement).value);
|
|
||||||
const variant = this.product()?.variants.find((item) => item.id === variantId) ?? null;
|
|
||||||
this.onVariantChange(variant);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected addToCart(): void {
|
protected addToCart(): void {
|
||||||
const currentProduct = this.product();
|
const currentProduct = this.product();
|
||||||
const variant = this.selectedVariant();
|
const variant = this.selectedVariant();
|
||||||
|
|||||||
@@ -195,11 +195,9 @@ describe('StoreHomePageComponent', () => {
|
|||||||
background_image_id: 'https://example.com/hero.jpg',
|
background_image_id: 'https://example.com/hero.jpg',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
tenant.active_event_id = 10;
|
tenant.event = {
|
||||||
tenant.active_event = {
|
title: 'Fiesta Fútbol Infantil',
|
||||||
id: 10,
|
location: 'Rosario, Santa Fe',
|
||||||
name: 'Fiesta Fútbol Infantil',
|
|
||||||
address: 'Rosario, Santa Fe',
|
|
||||||
dates: [
|
dates: [
|
||||||
{
|
{
|
||||||
id: 20,
|
id: 20,
|
||||||
@@ -331,11 +329,9 @@ describe('StoreHomePageComponent', () => {
|
|||||||
|
|
||||||
it('renders only the active event data when the tenant does not have the hero extra', async () => {
|
it('renders only the active event data when the tenant does not have the hero extra', async () => {
|
||||||
const tenant = createTenant();
|
const tenant = createTenant();
|
||||||
tenant.active_event_id = 12;
|
tenant.event = {
|
||||||
tenant.active_event = {
|
title: 'Fiesta Fútbol Infantil',
|
||||||
id: 12,
|
location: 'Sunchales, Santa Fe',
|
||||||
name: 'Fiesta Fútbol Infantil',
|
|
||||||
address: 'Sunchales, Santa Fe',
|
|
||||||
dates: [
|
dates: [
|
||||||
{
|
{
|
||||||
id: 25,
|
id: 25,
|
||||||
|
|||||||
@@ -68,14 +68,14 @@ export class StoreHomePageComponent implements OnInit, OnDestroy {
|
|||||||
protected readonly eventConfig = computed(() => {
|
protected readonly eventConfig = computed(() => {
|
||||||
const tenant = this.tenant();
|
const tenant = this.tenant();
|
||||||
const contact = tenant?.social_media ?? [];
|
const contact = tenant?.social_media ?? [];
|
||||||
const activeEvent = tenant?.active_event;
|
const event = tenant?.event;
|
||||||
|
|
||||||
if (activeEvent) {
|
if (event) {
|
||||||
return {
|
return {
|
||||||
id: activeEvent.id,
|
id: tenant.id,
|
||||||
title: activeEvent.name,
|
title: event.title,
|
||||||
location: activeEvent.address,
|
location: event.location,
|
||||||
dates: activeEvent.dates.map((eventDate) => ({
|
dates: event.dates.map((eventDate) => ({
|
||||||
id: eventDate.id,
|
id: eventDate.id,
|
||||||
date: eventDate.date,
|
date: eventDate.date,
|
||||||
start_time: eventDate.time_start,
|
start_time: eventDate.time_start,
|
||||||
|
|||||||
@@ -109,6 +109,8 @@ export class ProductListComponent {
|
|||||||
return (item.variants ?? []).map((variant) => ({
|
return (item.variants ?? []).map((variant) => ({
|
||||||
value: variant.id,
|
value: variant.id,
|
||||||
label: Object.values(variant.values).join(' / ') || `Variante ${variant.id}`,
|
label: Object.values(variant.values).join(' / ') || `Variante ${variant.id}`,
|
||||||
|
descripcion: variant.descripcion,
|
||||||
|
precio: variant.precio,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
<h3 class="product-row-card__title text-uppercase mb-1 m-0">
|
<h3 class="product-row-card__title text-uppercase mb-1 m-0">
|
||||||
{{ title() }}
|
{{ title() }}
|
||||||
</h3>
|
</h3>
|
||||||
@if (description()) {
|
@if (effectiveDescription()) {
|
||||||
<p class="product-row-card__description m-0 mt-1">
|
<p class="product-row-card__description m-0 mt-1">
|
||||||
{{ description() }}
|
{{ effectiveDescription() }}
|
||||||
</p>
|
</p>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import { QuantitySelectorComponent } from '../quantity-selector/quantity-selecto
|
|||||||
export interface Variant {
|
export interface Variant {
|
||||||
label: string;
|
label: string;
|
||||||
value: any;
|
value: any;
|
||||||
|
descripcion?: string | null;
|
||||||
|
precio?: string | number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@@ -53,11 +55,20 @@ export class ProductRowCardComponent {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formatted string for price: "$ 10.000"
|
protected readonly selectedVariantData = computed(() =>
|
||||||
readonly formattedPrice = computed(() => {
|
this.variants().find((variant) => Object.is(variant.value, this.selectedVariant())),
|
||||||
return this.formatCurrency(this.price());
|
);
|
||||||
|
protected readonly effectiveDescription = computed(
|
||||||
|
() => this.selectedVariantData()?.descripcion ?? this.description(),
|
||||||
|
);
|
||||||
|
protected readonly effectivePrice = computed(() => {
|
||||||
|
const variantPrice = Number(this.selectedVariantData()?.precio);
|
||||||
|
|
||||||
|
return Number.isFinite(variantPrice) ? variantPrice : this.price();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
readonly formattedPrice = computed(() => this.formatCurrency(this.effectivePrice()));
|
||||||
|
|
||||||
protected onAddToCart(): void {
|
protected onAddToCart(): void {
|
||||||
this.addToCart.emit({
|
this.addToCart.emit({
|
||||||
quantity: this.quantity(),
|
quantity: this.quantity(),
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
<div class="product-vertical-with-cart-card__content">
|
<div class="product-vertical-with-cart-card__content">
|
||||||
<h3 class="product-vertical-with-cart-card__title">{{ title() }}</h3>
|
<h3 class="product-vertical-with-cart-card__title">{{ title() }}</h3>
|
||||||
|
|
||||||
@if (description()) {
|
@if (effectiveDescription()) {
|
||||||
<p class="product-vertical-with-cart-card__description">{{ description() }}</p>
|
<p class="product-vertical-with-cart-card__description">{{ effectiveDescription() }}</p>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -177,4 +177,45 @@ describe('ProductVerticalWithCartCardComponent', () => {
|
|||||||
|
|
||||||
expect(buySpy).toHaveBeenCalledWith({ quantity: 1, variant: 31 });
|
expect(buySpy).toHaveBeenCalledWith({ quantity: 1, variant: 31 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('prioritizes the selected variant description and price', async () => {
|
||||||
|
const fixture = await createComponent('Descripción general');
|
||||||
|
fixture.componentRef.setInput('variants', [
|
||||||
|
{
|
||||||
|
id: 40,
|
||||||
|
descripcion: 'Almuerzo en comedor',
|
||||||
|
precio: '10000.00',
|
||||||
|
values: { servicio: 'Comedor' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 41,
|
||||||
|
descripcion: 'Almuerzo en vianda',
|
||||||
|
precio: '8000.00',
|
||||||
|
values: { servicio: 'Vianda' },
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
const element = fixture.nativeElement as HTMLElement;
|
||||||
|
expect(
|
||||||
|
element.querySelector('.product-vertical-with-cart-card__description')?.textContent,
|
||||||
|
).toContain('Almuerzo en comedor');
|
||||||
|
expect(
|
||||||
|
element.querySelector('.product-vertical-with-cart-card__price')?.textContent?.trim(),
|
||||||
|
).toBe('$ 10.000');
|
||||||
|
|
||||||
|
const select = element.querySelector(
|
||||||
|
'.product-vertical-with-cart-card__variant-select',
|
||||||
|
) as HTMLSelectElement;
|
||||||
|
select.value = select.options[1].value;
|
||||||
|
select.dispatchEvent(new Event('change'));
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
expect(
|
||||||
|
element.querySelector('.product-vertical-with-cart-card__description')?.textContent,
|
||||||
|
).toContain('Almuerzo en vianda');
|
||||||
|
expect(
|
||||||
|
element.querySelector('.product-vertical-with-cart-card__price')?.textContent?.trim(),
|
||||||
|
).toBe('$ 8.000');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ import { QuantitySelectorComponent } from '../quantity-selector/quantity-selecto
|
|||||||
|
|
||||||
export interface VerticalCartVariant {
|
export interface VerticalCartVariant {
|
||||||
id: number;
|
id: number;
|
||||||
|
descripcion?: string | null;
|
||||||
|
precio?: string | number;
|
||||||
values: Record<string, string>;
|
values: Record<string, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +48,18 @@ export class ProductVerticalWithCartCardComponent {
|
|||||||
|
|
||||||
protected readonly selectedValues = signal<Record<string, string>>({});
|
protected readonly selectedValues = signal<Record<string, string>>({});
|
||||||
|
|
||||||
protected readonly formattedPrice = computed(() => this.formatCurrency(this.price()));
|
protected readonly selectedVariantData = computed(() =>
|
||||||
|
this.variants().find((variant) => variant.id === this.selectedVariant()),
|
||||||
|
);
|
||||||
|
protected readonly effectiveDescription = computed(
|
||||||
|
() => this.selectedVariantData()?.descripcion ?? this.description(),
|
||||||
|
);
|
||||||
|
protected readonly effectivePrice = computed(() => {
|
||||||
|
const variantPrice = Number(this.selectedVariantData()?.precio);
|
||||||
|
|
||||||
|
return Number.isFinite(variantPrice) ? variantPrice : this.price();
|
||||||
|
});
|
||||||
|
protected readonly formattedPrice = computed(() => this.formatCurrency(this.effectivePrice()));
|
||||||
protected readonly variantSelectors = computed<VariantSelector[]>(() => {
|
protected readonly variantSelectors = computed<VariantSelector[]>(() => {
|
||||||
const variants = this.variants();
|
const variants = this.variants();
|
||||||
const keys = Array.from(new Set(variants.flatMap((variant) => Object.keys(variant.values))));
|
const keys = Array.from(new Set(variants.flatMap((variant) => Object.keys(variant.values))));
|
||||||
|
|||||||
Reference in New Issue
Block a user