-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(headless): add support for breadcrumb manager in commerce ssr (#…
…4601) https://coveord.atlassian.net/browse/KIT-3464 This PR adds the breadcrumb manager definer function and a sample for it.
- Loading branch information
1 parent
0c526de
commit 851257f
Showing
9 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
.../src/controllers/commerce/core/breadcrumb-manager/headless-core-breadcrumb-manager.ssr.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {ensureAtLeastOneSolutionType} from '../../../../app/commerce-ssr-engine/common.js'; | ||
import { | ||
ControllerDefinitionOption, | ||
SolutionType, | ||
SubControllerDefinitionWithoutProps, | ||
} from '../../../../app/commerce-ssr-engine/types/common.js'; | ||
import {buildProductListing} from '../../product-listing/headless-product-listing.js'; | ||
import {buildSearch} from '../../search/headless-search.js'; | ||
import { | ||
BreadcrumbManager, | ||
BreadcrumbManagerState, | ||
} from './headless-core-breadcrumb-manager.js'; | ||
|
||
export type {BreadcrumbManager, BreadcrumbManagerState}; | ||
|
||
/** | ||
* Defines a `BreadcrumbManager` controller instance. | ||
* | ||
* @returns The `BreadcrumbManager` controller definition. | ||
* | ||
* @internal | ||
*/ | ||
export function defineBreadcrumbManager< | ||
TOptions extends ControllerDefinitionOption | undefined, | ||
>(options?: TOptions) { | ||
ensureAtLeastOneSolutionType(options); | ||
return { | ||
...options, | ||
build: (engine, solutionType) => | ||
solutionType === SolutionType.listing | ||
? buildProductListing(engine).breadcrumbManager() | ||
: buildSearch(engine).breadcrumbManager(), | ||
} as SubControllerDefinitionWithoutProps<BreadcrumbManager, TOptions>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/samples/headless-ssr-commerce/app/_components/breadcrumb-manager.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
BreadcrumbManagerState, | ||
NumericFacetValue, | ||
DateFacetValue, | ||
CategoryFacetValue, | ||
BreadcrumbManager as HeadlessBreadcrumbManager, | ||
RegularFacetValue, | ||
} from '@coveo/headless/ssr-commerce'; | ||
import {useEffect, useState} from 'react'; | ||
|
||
interface BreadcrumbManagerProps { | ||
staticState: BreadcrumbManagerState; | ||
controller?: HeadlessBreadcrumbManager; | ||
} | ||
|
||
export default function BreadcrumbManager(props: BreadcrumbManagerProps) { | ||
const {staticState, controller} = props; | ||
|
||
const [state, setState] = useState(staticState); | ||
|
||
useEffect(() => { | ||
controller?.subscribe(() => setState(controller.state)); | ||
}, [controller]); | ||
|
||
const renderBreadcrumbValue = ( | ||
value: | ||
| CategoryFacetValue | ||
| RegularFacetValue | ||
| NumericFacetValue | ||
| DateFacetValue, | ||
type: string | ||
) => { | ||
switch (type) { | ||
case 'hierarchical': | ||
return (value as CategoryFacetValue).path.join(' > '); | ||
case 'regular': | ||
return (value as RegularFacetValue).value; | ||
case 'numericalRange': | ||
return ( | ||
(value as NumericFacetValue).start + | ||
' - ' + | ||
(value as NumericFacetValue).end | ||
); | ||
case 'dateRange': | ||
return ( | ||
(value as DateFacetValue).start + | ||
' - ' + | ||
(value as DateFacetValue).end | ||
); | ||
default: | ||
return null; | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<button onClick={controller?.deselectAll}>Clear all filters</button> | ||
</div> | ||
<ul> | ||
{state.facetBreadcrumbs.map((facetBreadcrumb) => { | ||
return ( | ||
<li key={`${facetBreadcrumb.facetId}-breadcrumbs`}> | ||
{facetBreadcrumb.values.map((value, index) => { | ||
return ( | ||
<button | ||
key={`${value.value}-breadcrumb-${index}`} | ||
onClick={() => value.deselect()} | ||
> | ||
{facetBreadcrumb.facetDisplayName}:{' '} | ||
{renderBreadcrumbValue(value.value, facetBreadcrumb.type)} X | ||
</button> | ||
); | ||
})} | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters