feat(ticket-selector): implement ticket selector component with layout and functionality
This commit is contained in:
@@ -27,6 +27,17 @@
|
||||
(addToCart)="emitColumnCart(item, $event)"
|
||||
/>
|
||||
}
|
||||
@case ('ticket_selector') {
|
||||
<app-product-ticket-selector
|
||||
[title]="item.nombre"
|
||||
[description]="item.descripcion ?? ''"
|
||||
[price]="price(item)"
|
||||
[imageUrl]="loadImages() ? (item.image ?? null) : null"
|
||||
[variants]="variantsFor(item)"
|
||||
[disabled]="loading()"
|
||||
(buy)="emitTicketBuy(item, $event)"
|
||||
/>
|
||||
}
|
||||
@default {
|
||||
<app-product-column-with-image
|
||||
[imageUrl]="loadImages() ? (item.image ?? null) : null"
|
||||
@@ -54,8 +65,11 @@
|
||||
class="product-list"
|
||||
[class.product-list--row]="effectiveLayout() === 'row'"
|
||||
[class.product-list--column]="effectiveLayout() !== 'row'"
|
||||
[class.product-list--adaptive]="groupLayout() !== 'simple_vertical'"
|
||||
[class.product-list--adaptive]="
|
||||
groupLayout() !== 'simple_vertical' && groupLayout() !== 'single'
|
||||
"
|
||||
[class.product-list--simple-vertical]="groupLayout() === 'simple_vertical'"
|
||||
[class.product-list--single]="groupLayout() === 'single'"
|
||||
>
|
||||
@for (item of itemData(); track item.id; let index = $index) {
|
||||
<ng-container
|
||||
|
||||
@@ -27,6 +27,10 @@
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
|
||||
&--single {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
|
||||
&__item {
|
||||
width: 100%;
|
||||
min-width: 0;
|
||||
|
||||
@@ -13,6 +13,7 @@ describe('ProductListComponent', () => {
|
||||
const items: ProductListItem[] = [
|
||||
{
|
||||
id: 1,
|
||||
type: 'product',
|
||||
nombre: 'Producto uno',
|
||||
descripcion: 'Primera descripcion',
|
||||
precio: '100.00',
|
||||
@@ -21,6 +22,7 @@ describe('ProductListComponent', () => {
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
type: 'product',
|
||||
nombre: 'Producto dos',
|
||||
descripcion: 'Segunda descripcion',
|
||||
precio: 200,
|
||||
@@ -216,6 +218,40 @@ describe('ProductListComponent', () => {
|
||||
expect(element.querySelectorAll('app-product-column-with-image')).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('renders a single ticket selector and emits its selected variant', async () => {
|
||||
const ticket: ProductListItem = {
|
||||
...items[0],
|
||||
variants: [
|
||||
{
|
||||
id: 401,
|
||||
precio: '10000.00',
|
||||
stock_tecnico: 1,
|
||||
values: { tipo: 'VIP', sector: 'A', fila: '3', asiento: '12' },
|
||||
},
|
||||
],
|
||||
};
|
||||
const fixture = await render('ticket_selector', [ticket], 'single');
|
||||
const buySpy = vi.fn();
|
||||
fixture.componentInstance.buy.subscribe(buySpy);
|
||||
await fixture.whenStable();
|
||||
fixture.detectChanges();
|
||||
const element = fixture.nativeElement as HTMLElement;
|
||||
|
||||
expect(element.querySelector('.product-list--single')).not.toBeNull();
|
||||
expect(element.querySelectorAll('app-product-ticket-selector')).toHaveLength(1);
|
||||
expect(element.querySelectorAll('.variant-selector__select')).toHaveLength(4);
|
||||
|
||||
(element.querySelector('.ticket-selector__actions .btn-primary') as HTMLButtonElement).click();
|
||||
|
||||
expect(buySpy).toHaveBeenCalledWith({
|
||||
product: ticket,
|
||||
quantity: 1,
|
||||
variant: 401,
|
||||
variantIds: [401],
|
||||
directPurchase: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('renders carousel groups with the reusable carousel', async () => {
|
||||
const fixture = await render('column_with_image', items, 'carousel');
|
||||
const element = fixture.nativeElement as HTMLElement;
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
Variant as RowVariant,
|
||||
} from '../product-row-card/product-row-card.component';
|
||||
import { ProductVerticalWithCartCardComponent } from '../product-vertical-with-cart-card/product-vertical-with-cart-card.component';
|
||||
import { ProductTicketSelectorComponent } from '../product-ticket-selector/product-ticket-selector.component';
|
||||
|
||||
export type ProductListLayout = CatalogProductLayout;
|
||||
export type ProductListVariant = CatalogFeaturedItemVariant;
|
||||
@@ -41,6 +42,7 @@ export interface ProductListBuyEvent {
|
||||
product: ProductListItem;
|
||||
quantity: number;
|
||||
variant?: number | null;
|
||||
variantIds?: number[];
|
||||
directPurchase: boolean;
|
||||
}
|
||||
|
||||
@@ -53,6 +55,7 @@ export interface ProductListBuyEvent {
|
||||
PaginatorComponent,
|
||||
ProductRowCardComponent,
|
||||
ProductVerticalWithCartCardComponent,
|
||||
ProductTicketSelectorComponent,
|
||||
],
|
||||
templateUrl: './product-list.component.html',
|
||||
styleUrl: './product-list.component.scss',
|
||||
@@ -110,6 +113,7 @@ export class ProductListComponent {
|
||||
value: variant.id,
|
||||
descripcion: variant.descripcion,
|
||||
precio: variant.precio,
|
||||
stock_tecnico: variant.stock_tecnico,
|
||||
values: variant.values,
|
||||
}));
|
||||
}
|
||||
@@ -159,4 +163,14 @@ export class ProductListComponent {
|
||||
protected emitProductDetailBuy(product: ProductListItem): void {
|
||||
this.buy.emit({ product, quantity: 1, variant: null, directPurchase: false });
|
||||
}
|
||||
|
||||
protected emitTicketBuy(product: ProductListItem, variantIds: number[]): void {
|
||||
this.buy.emit({
|
||||
product,
|
||||
quantity: variantIds.length,
|
||||
variant: variantIds[0] ?? null,
|
||||
variantIds,
|
||||
directPurchase: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ export interface Variant extends VariantSelectorVariant {
|
||||
label?: string;
|
||||
descripcion?: string | null;
|
||||
precio?: string | number;
|
||||
stock_tecnico?: number | null;
|
||||
}
|
||||
|
||||
@Component({
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
<article class="ticket-selector">
|
||||
<header class="ticket-selector__header">
|
||||
<div>
|
||||
<h3 class="ticket-selector__title">{{ title() }}</h3>
|
||||
@if (description()) {
|
||||
<p class="ticket-selector__description">{{ description() }}</p>
|
||||
}
|
||||
</div>
|
||||
<span class="ticket-selector__price text-primary">
|
||||
@if (priceRange().hasRange) {
|
||||
de <strong>{{ priceRange().minimum }}</strong> a
|
||||
<strong>{{ priceRange().maximum }}</strong>
|
||||
} @else {
|
||||
<strong>{{ priceRange().minimum }}</strong>
|
||||
}
|
||||
</span>
|
||||
</header>
|
||||
|
||||
<div class="ticket-selector__selection">
|
||||
<span class="ticket-selector__label">Seleccioná Entrada/s:</span>
|
||||
|
||||
<div class="ticket-selector__rows">
|
||||
@for (row of rows(); track row.id) {
|
||||
<div class="ticket-selector__row">
|
||||
<app-variant-selector
|
||||
class="ticket-selector__fields"
|
||||
[variants]="variantsForRow(row.id)"
|
||||
[disabled]="disabled()"
|
||||
[selectedVariant]="row.variantId"
|
||||
(selectedVariantChange)="updateRow(row.id, $event)"
|
||||
/>
|
||||
<app-icon-button
|
||||
variant="trash"
|
||||
ariaLabel="Eliminar entrada"
|
||||
[disabled]="disabled()"
|
||||
(clicked)="removeRow(row.id)"
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<app-button
|
||||
hostClass="ticket-selector__add-row"
|
||||
buttonClass="ticket-selector__add-row-button"
|
||||
variant="secondary"
|
||||
[disabled]="disabled() || !canAddRow()"
|
||||
(click)="addRow()"
|
||||
>
|
||||
+ Agregar entrada
|
||||
</app-button>
|
||||
|
||||
@if (selectableVariants().length === 0) {
|
||||
<p class="ticket-selector__empty">No hay entradas disponibles.</p>
|
||||
}
|
||||
|
||||
<div class="ticket-selector__actions">
|
||||
<app-button variant="primary" [disabled]="disabled() || !hasSelection()" (click)="onBuy()">
|
||||
Comprar
|
||||
</app-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (imageUrl(); as image) {
|
||||
<img class="ticket-selector__map" [src]="image" [alt]="'Plano de ubicaciones de ' + title()" />
|
||||
}
|
||||
</article>
|
||||
@@ -0,0 +1,141 @@
|
||||
:host {
|
||||
display: block;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.ticket-selector {
|
||||
width: 100%;
|
||||
|
||||
&__header {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
gap: 1rem;
|
||||
padding-bottom: 1.25rem;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
&__title {
|
||||
margin: 0;
|
||||
color: #666;
|
||||
font-size: 22px;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.4px;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
&__description {
|
||||
margin: 0.35rem 0 0;
|
||||
color: #777;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
&__price {
|
||||
flex: 0 0 auto;
|
||||
font-size: 16px;
|
||||
font-weight: 400;
|
||||
|
||||
strong {
|
||||
font-weight: 700;
|
||||
}
|
||||
}
|
||||
|
||||
&__selection {
|
||||
padding-top: 1rem;
|
||||
}
|
||||
|
||||
&__label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
color: #555;
|
||||
font-size: 15px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
&__rows {
|
||||
display: grid;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
&__row {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) auto;
|
||||
align-items: end;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
&__fields {
|
||||
min-width: 0;
|
||||
|
||||
::ng-deep .variant-selector {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, minmax(120px, 1fr));
|
||||
gap: 0.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
&__add-row {
|
||||
display: block;
|
||||
width: 100%;
|
||||
margin-top: 0.5rem;
|
||||
|
||||
::ng-deep .ticket-selector__add-row-button {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
&__empty {
|
||||
margin: 0;
|
||||
color: #777;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
&__actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 0.75rem;
|
||||
margin-top: 0.75rem;
|
||||
|
||||
app-button {
|
||||
min-width: 170px;
|
||||
}
|
||||
}
|
||||
|
||||
&__map {
|
||||
display: block;
|
||||
width: min(100%, 720px);
|
||||
max-height: 680px;
|
||||
margin: 3rem auto 0;
|
||||
object-fit: contain;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 767.98px) {
|
||||
.ticket-selector {
|
||||
&__header {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
&__fields {
|
||||
::ng-deep .variant-selector {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
&__actions {
|
||||
flex-direction: column-reverse;
|
||||
|
||||
app-button {
|
||||
width: 100%;
|
||||
|
||||
::ng-deep .btn {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&__map {
|
||||
margin-top: 2rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import { ChangeDetectionStrategy, Component, computed, input, output, signal } from '@angular/core';
|
||||
|
||||
import { ButtonComponent } from '../button/button.component';
|
||||
import { IconButtonComponent } from '../icon-button/icon-button.component';
|
||||
import {
|
||||
VariantSelectorComponent,
|
||||
VariantSelectorVariant,
|
||||
} from '../variant-selector/variant-selector.component';
|
||||
|
||||
export interface TicketSelectorVariant extends VariantSelectorVariant {
|
||||
precio?: string | number;
|
||||
stock_tecnico?: number | null;
|
||||
}
|
||||
|
||||
interface TicketSelectionRow {
|
||||
id: number;
|
||||
variantId: number | null;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-product-ticket-selector',
|
||||
imports: [ButtonComponent, IconButtonComponent, VariantSelectorComponent],
|
||||
templateUrl: './product-ticket-selector.component.html',
|
||||
styleUrl: './product-ticket-selector.component.scss',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class ProductTicketSelectorComponent {
|
||||
readonly title = input.required<string>();
|
||||
readonly description = input<string>('');
|
||||
readonly price = input<number>(0);
|
||||
readonly imageUrl = input<string | null>(null);
|
||||
readonly variants = input<TicketSelectorVariant[]>([]);
|
||||
readonly disabled = input(false);
|
||||
|
||||
readonly buy = output<number[]>();
|
||||
|
||||
protected readonly rows = signal<TicketSelectionRow[]>([{ id: 1, variantId: null }]);
|
||||
private nextRowId = 2;
|
||||
|
||||
protected readonly selectableVariants = computed<TicketSelectorVariant[]>(() =>
|
||||
this.variants().filter((variant) => variant.stock_tecnico == null || variant.stock_tecnico > 0),
|
||||
);
|
||||
protected readonly hasSelection = computed(
|
||||
() => this.rows().length > 0 && this.rows().every((row) => row.variantId !== null),
|
||||
);
|
||||
protected readonly canAddRow = computed(
|
||||
() => this.rows().length < this.selectableVariants().length,
|
||||
);
|
||||
protected readonly priceRange = computed(() => {
|
||||
const prices = this.variants()
|
||||
.filter((variant) => variant.stock_tecnico == null || variant.stock_tecnico > 0)
|
||||
.map((variant) => Number(variant.precio ?? this.price()))
|
||||
.filter(Number.isFinite);
|
||||
|
||||
if (prices.length === 0) {
|
||||
const price = this.formatCurrency(this.price());
|
||||
return { minimum: price, maximum: price, hasRange: false };
|
||||
}
|
||||
|
||||
const minimum = Math.min(...prices);
|
||||
const maximum = Math.max(...prices);
|
||||
return {
|
||||
minimum: this.formatCurrency(minimum),
|
||||
maximum: this.formatCurrency(maximum),
|
||||
hasRange: minimum !== maximum,
|
||||
};
|
||||
});
|
||||
|
||||
protected onBuy(): void {
|
||||
const variantIds = this.rows().flatMap((row) =>
|
||||
row.variantId === null ? [] : [row.variantId],
|
||||
);
|
||||
|
||||
if (variantIds.length === this.rows().length && variantIds.length > 0) {
|
||||
this.buy.emit(variantIds);
|
||||
}
|
||||
}
|
||||
|
||||
protected addRow(): void {
|
||||
if (!this.canAddRow()) return;
|
||||
this.rows.update((rows) => [...rows, { id: this.nextRowId++, variantId: null }]);
|
||||
}
|
||||
|
||||
protected removeRow(rowId: number): void {
|
||||
this.rows.update((rows) => rows.filter((row) => row.id !== rowId));
|
||||
}
|
||||
|
||||
protected updateRow(rowId: number, selectedVariant: unknown): void {
|
||||
const variantId = typeof selectedVariant === 'number' ? selectedVariant : null;
|
||||
this.rows.update((rows) => rows.map((row) => (row.id === rowId ? { ...row, variantId } : row)));
|
||||
}
|
||||
|
||||
protected variantsForRow(rowId: number): VariantSelectorVariant[] {
|
||||
const selectedByOtherRows = new Set(
|
||||
this.rows()
|
||||
.filter((row) => row.id !== rowId && row.variantId !== null)
|
||||
.map((row) => row.variantId),
|
||||
);
|
||||
|
||||
return this.selectableVariants().filter(
|
||||
(variant) => !selectedByOtherRows.has(variant.value as number),
|
||||
);
|
||||
}
|
||||
|
||||
private formatCurrency(value: number): string {
|
||||
return new Intl.NumberFormat('es-AR', {
|
||||
style: 'currency',
|
||||
currency: 'ARS',
|
||||
maximumFractionDigits: 0,
|
||||
}).format(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user