Revert "feat: enhance variant attribute handling across components for improved selection and display"
This reverts commit 56f602d876.
This commit is contained in:
@@ -28,8 +28,8 @@
|
||||
[ngModel]="selectedValues()[selector.key]"
|
||||
(ngModelChange)="onVariantValueChange(selector.key, $event)"
|
||||
>
|
||||
@for (option of selector.options; track option.key) {
|
||||
<option [ngValue]="option.value">{{ option.label }}</option>
|
||||
@for (option of selector.options; track option) {
|
||||
<option [ngValue]="option">{{ option }}</option>
|
||||
}
|
||||
</select>
|
||||
}
|
||||
|
||||
@@ -136,24 +136,6 @@ describe('ProductVerticalWithCartCardComponent', () => {
|
||||
expect(selectors?.querySelector('app-quantity-selector')).toBeNull();
|
||||
});
|
||||
|
||||
it('uses the variant value for selection and renders its label', async () => {
|
||||
const fixture = await createComponent();
|
||||
fixture.componentRef.setInput('variants', [
|
||||
{
|
||||
id: 10,
|
||||
values: { event_date: { value: '2', label: '10/10/2026' } },
|
||||
},
|
||||
]);
|
||||
fixture.detectChanges();
|
||||
|
||||
const option = fixture.nativeElement.querySelector(
|
||||
'.product-vertical-with-cart-card__variant-select option',
|
||||
) as HTMLOptionElement;
|
||||
|
||||
expect(option.textContent?.trim()).toBe('10/10/2026');
|
||||
expect((fixture.componentInstance as any).selectedValues()).toEqual({ event_date: '2' });
|
||||
});
|
||||
|
||||
it('places all variant selectors together below price and quantity', async () => {
|
||||
const fixture = await createComponent();
|
||||
fixture.componentRef.setInput('variants', [
|
||||
|
||||
@@ -13,27 +13,18 @@ import { FormsModule } from '@angular/forms';
|
||||
|
||||
import { ButtonComponent } from '../button/button.component';
|
||||
import { QuantitySelectorComponent } from '../quantity-selector/quantity-selector.component';
|
||||
import { VariantAttributeValue } from '../../../core/services/catalog/catalog.interface';
|
||||
|
||||
type VariantSelectionValue = string | string[];
|
||||
|
||||
export interface VerticalCartVariant {
|
||||
id: number;
|
||||
descripcion?: string | null;
|
||||
precio?: string | number;
|
||||
values: Record<string, VariantAttributeValue>;
|
||||
}
|
||||
|
||||
interface VariantSelectorOption {
|
||||
key: string;
|
||||
value: VariantSelectionValue;
|
||||
label: string;
|
||||
values: Record<string, string>;
|
||||
}
|
||||
|
||||
interface VariantSelector {
|
||||
key: string;
|
||||
label: string;
|
||||
options: VariantSelectorOption[];
|
||||
options: string[];
|
||||
}
|
||||
|
||||
@Component({
|
||||
@@ -55,7 +46,7 @@ export class ProductVerticalWithCartCardComponent {
|
||||
readonly buy = output<{ quantity: number; variant: number | null }>();
|
||||
readonly addToCart = output<{ quantity: number; variant: number | null }>();
|
||||
|
||||
protected readonly selectedValues = signal<Record<string, VariantSelectionValue>>({});
|
||||
protected readonly selectedValues = signal<Record<string, string>>({});
|
||||
|
||||
protected readonly selectedVariantData = computed(() =>
|
||||
this.variants().find((variant) => variant.id === this.selectedVariant()),
|
||||
@@ -76,7 +67,13 @@ export class ProductVerticalWithCartCardComponent {
|
||||
return keys.map((key) => ({
|
||||
key,
|
||||
label: this.formatVariantLabel(key),
|
||||
options: this.optionsFor(variants, key),
|
||||
options: Array.from(
|
||||
new Set(
|
||||
variants
|
||||
.map((variant) => variant.values[key])
|
||||
.filter((value): value is string => Boolean(value)),
|
||||
),
|
||||
),
|
||||
}));
|
||||
});
|
||||
|
||||
@@ -95,21 +92,17 @@ export class ProductVerticalWithCartCardComponent {
|
||||
|
||||
const selected =
|
||||
variants.find((variant) => variant.id === this.selectedVariant()) ?? variants[0];
|
||||
this.selectedValues.set(this.selectionValues(selected.values));
|
||||
this.selectedValues.set({ ...selected.values });
|
||||
this.selectedVariant.set(selected.id);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected onVariantValueChange(key: string, value: VariantSelectionValue): void {
|
||||
protected onVariantValueChange(key: string, value: string): void {
|
||||
const values = { ...this.selectedValues(), [key]: value };
|
||||
const selectors = this.variantSelectors();
|
||||
const matchingVariant = this.variants().find((variant) =>
|
||||
selectors.every(
|
||||
(selector) =>
|
||||
this.valueKey(this.selectionValue(variant.values[selector.key])) ===
|
||||
this.valueKey(values[selector.key]),
|
||||
),
|
||||
selectors.every((selector) => variant.values[selector.key] === values[selector.key]),
|
||||
);
|
||||
|
||||
this.selectedValues.set(values);
|
||||
@@ -124,55 +117,6 @@ export class ProductVerticalWithCartCardComponent {
|
||||
this.buy.emit({ quantity: this.quantity(), variant: this.selectedVariant() });
|
||||
}
|
||||
|
||||
private optionsFor(variants: VerticalCartVariant[], key: string): VariantSelectorOption[] {
|
||||
const options = new Map<string, VariantSelectorOption>();
|
||||
|
||||
for (const variant of variants) {
|
||||
const attributeValue = variant.values[key];
|
||||
if (attributeValue === undefined) continue;
|
||||
|
||||
const value = this.selectionValue(attributeValue);
|
||||
const optionKey = this.valueKey(value);
|
||||
if (!options.has(optionKey)) {
|
||||
options.set(optionKey, {
|
||||
key: optionKey,
|
||||
value,
|
||||
label: this.selectionLabel(attributeValue),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(options.values());
|
||||
}
|
||||
|
||||
private selectionValues(
|
||||
values: Record<string, VariantAttributeValue>,
|
||||
): Record<string, VariantSelectionValue> {
|
||||
return Object.fromEntries(
|
||||
Object.entries(values).map(([key, value]) => [key, this.selectionValue(value)]),
|
||||
);
|
||||
}
|
||||
|
||||
private selectionValue(value: VariantAttributeValue): VariantSelectionValue {
|
||||
if (typeof value === 'string') return value;
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => (typeof item === 'string' ? item : item.value));
|
||||
}
|
||||
return value.value;
|
||||
}
|
||||
|
||||
private selectionLabel(value: VariantAttributeValue): string {
|
||||
if (typeof value === 'string') return value;
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => (typeof item === 'string' ? item : item.label)).join(', ');
|
||||
}
|
||||
return value.label;
|
||||
}
|
||||
|
||||
private valueKey(value: VariantSelectionValue | undefined): string {
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
private formatVariantLabel(key: string): string {
|
||||
const label = key.replace(/[_-]+/g, ' ');
|
||||
return label.charAt(0).toUpperCase() + label.slice(1);
|
||||
|
||||
Reference in New Issue
Block a user