Skip to content

Commit

Permalink
fix(atomic/commerce): fix breadbox labels / values (#4178)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeaudoincoveo authored Jul 11, 2024
1 parent 095ab4b commit 6ab1e3b
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {NumberValue, Schema} from '@coveo/bueno';
import {
BreadcrumbManagerState,
BreadcrumbManager,
FacetGenerator,
FacetGeneratorState,
ProductListing,
Search,
buildProductListing,
Expand All @@ -17,6 +15,7 @@ import {
} from '@coveo/headless/commerce';
import {Component, h, State, Element, Prop} from '@stencil/core';
import {FocusTargetController} from '../../../utils/accessibility-utils';
import {parseDate} from '../../../utils/date-utils';
import {getFieldValueCaption} from '../../../utils/field-utils';
import {
InitializableComponent,
Expand All @@ -30,6 +29,8 @@ import {BreadcrumbContent} from '../../common/breadbox/breadcrumb-content';
import {BreadcrumbShowLess} from '../../common/breadbox/breadcrumb-show-less';
import {BreadcrumbShowMore} from '../../common/breadbox/breadcrumb-show-more';
import {Breadcrumb as BreadboxBreadcrumb} from '../../common/breadbox/breadcrumb-types';
import {formatHumanReadable} from '../../common/facets/numeric-facet/formatter';
import {defaultNumberFormatter} from '../../common/formats/format-common';
import {Hidden} from '../../common/hidden';
import {CommerceBindings} from '../atomic-commerce-interface/atomic-commerce-interface';

Expand Down Expand Up @@ -73,7 +74,6 @@ export class AtomicCommerceBreadbox
private numberOfBreadcrumbs = 0;
private numberOfCollapsedBreadcrumbs = 0;
private firstExpandedBreadcrumbIndex?: number;
facetGenerator!: FacetGenerator;
breadcrumbManager!: BreadcrumbManager;

@Element() private host!: HTMLElement;
Expand All @@ -83,9 +83,6 @@ export class AtomicCommerceBreadbox
@BindStateToController('breadcrumbManager')
@State()
private breadcrumbManagerState!: BreadcrumbManagerState;
@BindStateToController('facetGenerator')
@State()
public facetGeneratorState!: FacetGeneratorState[];
@State() public error!: Error;
@State() private isCollapsed = true;

Expand Down Expand Up @@ -121,7 +118,6 @@ export class AtomicCommerceBreadbox
}

this.breadcrumbManager = this.searchOrListing.breadcrumbManager();
this.facetGenerator = this.searchOrListing.facetGenerator();

if (window.ResizeObserver) {
this.resizeObserver = new ResizeObserver(() => this.adaptBreadcrumbs());
Expand Down Expand Up @@ -249,15 +245,25 @@ export class AtomicCommerceBreadbox
switch (type) {
case 'numericalRange':
return [
this.bindings.store.state.numericFacets[field].format(
value.value as NumericFacetValue
),
formatHumanReadable({
facetValue: value.value as NumericFacetValue,
logger: this.bindings.engine.logger,
i18n: this.bindings.i18n,
field: field,
manualRanges: [],
formatter: defaultNumberFormatter,
}),
];
case 'dateRange':
return [
this.bindings.store.state.dateFacets[field].format(
value.value as DateFacetValue
),
this.bindings.i18n.t('to', {
start: parseDate((value.value as DateFacetValue).start).format(
'YYYY-MM-DD'
),
end: parseDate((value.value as DateFacetValue).end).format(
'YYYY-MM-DD'
),
}),
];
case 'hierarchical':
return (value.value as CategoryFacetValue).path.map(
Expand All @@ -276,33 +282,10 @@ export class AtomicCommerceBreadbox
};

private buildBreadcrumb(breadcrumb: Breadcrumb<AnyFacetValue>) {
let facetStateName:
| 'facets'
| 'categoryFacets'
| 'numericFacets'
| 'dateFacets';

switch (breadcrumb.type) {
case 'hierarchical':
facetStateName = 'categoryFacets';
break;
case 'numericalRange':
facetStateName = 'numericFacets';
break;
case 'dateRange':
facetStateName = 'dateFacets';
break;
default:
facetStateName = 'facets';
}

return breadcrumb.values.map((value: BreadcrumbValue<AnyFacetValue>) => {
return {
facetId: breadcrumb.facetId,
label:
this.bindings.store.state[facetStateName][
breadcrumb.facetId
]?.label(),
label: breadcrumb.facetDisplayName,
deselect: value.deselect,
formattedValue: this.valueForFacetType(
breadcrumb.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export interface AtomicStoreData extends AtomicCommonStoreData {

export interface AtomicCommerceStore
extends AtomicCommonStore<AtomicStoreData> {
getAllFacets(): FacetInfoMap;
isMobile(): boolean;
}

Expand Down Expand Up @@ -70,15 +69,6 @@ export function createAtomicCommerceStore(
return {
...commonStore,

getAllFacets() {
return {
...commonStore.state.facets,
...commonStore.state.dateFacets,
...commonStore.state.categoryFacets,
...commonStore.state.numericFacets,
};
},

isMobile() {
return !window.matchMedia(
makeDesktopQuery(commonStore.state.mobileBreakpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ export class AtomicCategoryFacet implements InitializableComponent<Bindings> {
*/
@Prop({reflect: true}) field?: string;

@BindStateToController('facet')
@State()
public facetState!: CategoryFacetState;

Expand All @@ -124,27 +123,34 @@ export class AtomicCategoryFacet implements InitializableComponent<Bindings> {
private showMoreFocus?: FocusTargetController;
private headerFocus?: FocusTargetController;
private activeValueFocus?: FocusTargetController;
private unsubscribeFacetController!: () => void;

@AriaLiveRegion('facet-search')
protected facetSearchAriaMessage!: string;

public initialize() {
if (!this.facetState) {
if (!this.facet) {
return;
}

this.unsubscribeFacetController = this.facet.subscribe(
() => (this.facetState = this.facet.state)
);

announceFacetSearchResultsWithAriaLive(
this.facet,
this.displayName,
(msg) => (this.facetSearchAriaMessage = msg),
this.bindings.i18n
);

const facetInfo: FacetInfo = {
label: () => this.bindings.i18n.t(this.displayName),
facetId: this.facetState.facetId,
element: this.host,
isHidden: () => this.isHidden,
};
this.bindings.store.registerFacet('categoryFacets', facetInfo);

initializePopover(this.host, {
...facetInfo,
hasValues: () => !!this.facetState.values.length,
Expand Down Expand Up @@ -182,6 +188,7 @@ export class AtomicCategoryFacet implements InitializableComponent<Bindings> {
if (this.host.isConnected) {
return;
}
this.unsubscribeFacetController();
}

private get isHidden() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export class AtomicCommerceFacet implements InitializableComponent<Bindings> {
*/
@Prop({reflect: true}) field?: string;

@BindStateToController('facet')
@State()
public facetState!: RegularFacetState;

Expand All @@ -124,17 +123,24 @@ export class AtomicCommerceFacet implements InitializableComponent<Bindings> {
private showLessFocus?: FocusTargetController;
private showMoreFocus?: FocusTargetController;
private headerFocus?: FocusTargetController;
private unsubscribeFacetController!: () => void;

@AriaLiveRegion('facet-search')
protected facetSearchAriaMessage!: string;

public initialize() {
if (!this.facetState) {
if (!this.facet) {
return;
}
this.unsubscribeFacetController = this.facet.subscribe(
() => (this.facetState = this.facet.state)
);
this.initAriaLive();
this.initPopover();
this.registerFacet();
}

public disconnectedCallback(): void {
this.unsubscribeFacetController();
}

public componentShouldUpdate(
Expand Down Expand Up @@ -336,10 +342,6 @@ export class AtomicCommerceFacet implements InitializableComponent<Bindings> {
return !this.facetState.values.length;
}

private registerFacet() {
this.bindings.store.registerFacet('facets', this.facetInfo);
}

private initPopover() {
initializePopover(this.host, {
...this.facetInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {FacetInfo} from '../../../common/facets/facet-common-store';
import {FacetContainer} from '../../../common/facets/facet-container/facet-container';
import {FacetGuard} from '../../../common/facets/facet-guard';
import {FacetHeader} from '../../../common/facets/facet-header/facet-header';
import {formatHumanReadable} from '../../../common/facets/numeric-facet/formatter';
import {NumericFacetValueLink} from '../../../common/facets/numeric-facet/value-link';
import {NumericFacetValuesContainer} from '../../../common/facets/numeric-facet/values-container';
import {initializePopover} from '../../../common/facets/popover/popover-type';
Expand Down Expand Up @@ -47,7 +46,6 @@ export class AtomicCommerceNumericFacet

@State() private range?: Range;

@BindStateToController('facet')
@State()
public facetState!: NumericFacetState;

Expand Down Expand Up @@ -86,35 +84,33 @@ export class AtomicCommerceNumericFacet
return this.headerFocus;
}

private unsubscribeFacetController!: () => void;

public initialize() {
if (!this.facetState) {
if (!this.facet) {
return;
}

this.unsubscribeFacetController = this.facet.subscribe(
() => (this.facetState = this.facet.state)
);

this.registerFacetToStore();
}

public disconnectedCallback() {
this.unsubscribeFacetController();
}

private registerFacetToStore() {
const {facetId, field} = this.facetState;
const {facetId} = this.facetState;
const facetInfo: FacetInfo = {
label: () => this.bindings.i18n.t(this.displayName),
facetId: facetId,
element: this.host,
isHidden: () => this.isHidden,
};

this.bindings.store.registerFacet('numericFacets', {
...facetInfo,
format: (value) =>
formatHumanReadable({
facetValue: value,
logger: this.bindings.engine.logger,
i18n: this.bindings.i18n,
field: field,
manualRanges: this.manualRanges,
formatter: this.formatter,
}),
});

initializePopover(this.host, {
...facetInfo,
hasValues: () => this.hasValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export class AtomicCommerceTimeframeFacet
*/
@Prop({reflect: true}) field?: string;

@BindStateToController('facet')
@State()
public facetState!: DateFacetState;

Expand All @@ -87,10 +86,17 @@ export class AtomicCommerceTimeframeFacet
return this.headerFocus;
}

private unsubscribeFacetController!: () => void;

public initialize() {
if (!this.facetState) {
if (!this.facet) {
return;
}

this.unsubscribeFacetController = this.facet.subscribe(
() => (this.facetState = this.facet.state)
);

this.registerFacetToStore();
}

Expand Down Expand Up @@ -162,6 +168,7 @@ export class AtomicCommerceTimeframeFacet
if (this.host.isConnected) {
return;
}
this.unsubscribeFacetController();
}

private get isHidden() {
Expand All @@ -181,11 +188,6 @@ export class AtomicCommerceTimeframeFacet
isHidden: () => this.isHidden,
};

this.bindings.store.registerFacet('dateFacets', {
...facetInfo,
format: (value) => this.formatFacetValue(value),
});

initializePopover(this.host, {
...facetInfo,
hasValues: () => this.hasValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ <h1>Cart</h1>
<atomic-layout-section section="main">
<atomic-commerce-recommendation-list
id="cart_recommendations"
slot-id="52870f54-4edc-4767-b8b5-f126ff2de9bf"
slot-id="8a0ba6de-cecc-473c-a364-dab408d3cd54"
products-per-page="3"
>
<atomic-product-template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ <h1>HOMEPAGE</h1>
<atomic-layout-section section="main">
<atomic-commerce-recommendation-list
id="popular_bought"
slot-id="3216339b-f776-4cb1-8a21-018c8ec0f08c"
slot-id="52870f54-4edc-4767-b8b5-f126ff2de9bf"
products-per-page="3"
>
<atomic-product-template>
Expand Down Expand Up @@ -70,9 +70,15 @@ <h1>HOMEPAGE</h1>
</template>
</atomic-product-template>
</atomic-commerce-recommendation-list>
</atomic-layout-section>
</atomic-commerce-layout>
</atomic-commerce-recommendation-interface>
<atomic-commerce-recommendation-interface>
<atomic-commerce-layout>
<atomic-layout-section section="main">
<atomic-commerce-recommendation-list
id="popular_viewed"
slot-id="36ed24b6-d7e0-4c32-8815-c456cc1928e7"
slot-id="abccdea4-7d8d-4d56-b593-20267083f88f"
products-per-page="3"
>
</atomic-commerce-recommendation-list>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ <h1>Pants</h1>
></atomic-layout-section>
<atomic-layout-section section="main">
<atomic-layout-section section="status">
<atomic-commerce-breadbox></atomic-commerce-breadbox>
<atomic-commerce-query-summary></atomic-commerce-query-summary>
<atomic-commerce-sort-dropdown></atomic-commerce-sort-dropdown>
<atomic-commerce-refine-toggle></atomic-commerce-refine-toggle>
Expand Down
Loading

0 comments on commit 6ab1e3b

Please sign in to comment.