diff --git a/feature-libs/cart/base/components/add-to-cart/add-to-cart.component.html b/feature-libs/cart/base/components/add-to-cart/add-to-cart.component.html index cea53d5837f..91b559d8ff0 100644 --- a/feature-libs/cart/base/components/add-to-cart/add-to-cart.component.html +++ b/feature-libs/cart/base/components/add-to-cart/add-to-cart.component.html @@ -23,9 +23,14 @@ + {{ + disableReason + }} + - {{ - (displayPickupLocation - ? 'pickupOptions.changeStore' - : 'pickupOptions.selectStore' - ) | cxTranslate - }} -

- - - - -
- - -
-
- - -
-
+ + +Free return + + + {{ displayPickupLocation }} | + + + + + diff --git a/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.component.ts b/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.component.ts index a3a310639c3..8edcd28950f 100644 --- a/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.component.ts +++ b/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.component.ts @@ -5,19 +5,34 @@ */ import { + AfterViewInit, + ChangeDetectorRef, Component, ElementRef, EventEmitter, inject, Input, OnChanges, + OnDestroy, Output, ViewChild, + TemplateRef, } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; import { FeatureConfigService, useFeatureStyles } from '@spartacus/core'; import { PickupOption } from '@spartacus/pickup-in-store/root'; +import { TabComponent } from 'projects/storefrontlib/cms-components/content/tab/tab.component'; +import { + TAB_MODE, + Tab, + TabConfig, +} from 'projects/storefrontlib/cms-components/content/tab/tab.model'; +import { Subscription, take } from 'rxjs'; +enum PickupOptionsTabs { + 'DELIVERY', + 'PICKUP', +} /** * The presentational component of a pair of radio buttons for pickup options for a product. */ @@ -25,7 +40,10 @@ import { PickupOption } from '@spartacus/pickup-in-store/root'; selector: 'cx-pickup-options', templateUrl: './pickup-options.component.html', }) -export class PickupOptionsComponent implements OnChanges { +export class PickupOptionsComponent + implements OnChanges, AfterViewInit, OnDestroy +{ + protected subscription = new Subscription(); /** The selected option, either `'pickup'` or `'delivery'`. */ @Input() selectedOption: PickupOption; /** The location to display in the pickup option. */ @@ -54,7 +72,17 @@ export class PickupOptionsComponent implements OnChanges { pickupOptionsForm = new FormGroup({ pickupOption: new FormControl(null), }); + tabs: Tab[]; + tabConfig: TabConfig = { + label: 'pickupOptions.legend', + openTabs: [PickupOptionsTabs.DELIVERY], + }; + + @ViewChild('deliveryTabPanel') deliveryTabPanel: TemplateRef; + @ViewChild('pickupTabPanel') pickupTabPanel: TemplateRef; + @ViewChild(TabComponent) tabComponent: TabComponent | undefined; + protected cdr = inject(ChangeDetectorRef); constructor() { useFeatureStyles('a11yDeliveryMethodFieldset'); } @@ -65,6 +93,46 @@ export class PickupOptionsComponent implements OnChanges { } this.pickupOptionsForm.markAllAsTouched(); this.pickupOptionsForm.get('pickupOption')?.setValue(this.selectedOption); + this.onSelectedOptionChange(); + } + + protected onSelectedOptionChange() { + if (!this.tabComponent) return; + this.tabComponent.openTabs$.pipe(take(1)).subscribe((openTabs) => { + const openedTab = openTabs[0]; + const shouldBeOpened = + this.selectedOption === 'delivery' + ? PickupOptionsTabs.DELIVERY + : PickupOptionsTabs.PICKUP; + if (openedTab !== shouldBeOpened) { + this.tabComponent?.select(shouldBeOpened, TAB_MODE.TAB); + } + }); + } + + ngAfterViewInit() { + this.tabs = [ + { + header: 'Ship it', + content: this.deliveryTabPanel, + id: PickupOptionsTabs.DELIVERY, + }, + { + header: 'Free Pickup In Store', + content: this.pickupTabPanel, + id: PickupOptionsTabs.PICKUP, + }, + ]; + this.cdr.detectChanges(); + this.subscription.add( + this.tabComponent?.openTabs$.subscribe((openTabs) => { + // open tabs should have one tab opened for mode "TAB" + const openedTab = openTabs[0]; + const selectedOption = + openedTab === PickupOptionsTabs.DELIVERY ? 'delivery' : 'pickup'; + this.onPickupOptionChange(selectedOption); + }) + ); } /** Emit a new selected option. */ @@ -89,4 +157,8 @@ export class PickupOptionsComponent implements OnChanges { // Return false to stop `onPickupOptionChange` being called after this return false; } + + ngOnDestroy(): void { + this.subscription.unsubscribe(); + } } diff --git a/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.module.ts b/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.module.ts index 5ce387548a8..297cd96c20c 100644 --- a/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.module.ts +++ b/feature-libs/pickup-in-store/components/presentational/pickup-options/pickup-options.module.ts @@ -9,6 +9,7 @@ import { NgModule } from '@angular/core'; import { ReactiveFormsModule } from '@angular/forms'; import { FeaturesConfigModule, I18nModule } from '@spartacus/core'; import { PickupOptionsComponent } from './pickup-options.component'; +import { TabModule } from 'projects/storefrontlib/cms-components/content/tab/tab.module'; @NgModule({ imports: [ @@ -16,6 +17,7 @@ import { PickupOptionsComponent } from './pickup-options.component'; I18nModule, ReactiveFormsModule, FeaturesConfigModule, + TabModule, ], declarations: [PickupOptionsComponent], exports: [PickupOptionsComponent],