-
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/commerce): add basic plp, search and recs use case exam…
…ples (#4079) I also exposed the sample commerce engine configuration in headless. [CAPI-918] [CAPI-918]: https://coveord.atlassian.net/browse/CAPI-918?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
- Loading branch information
1 parent
56da0e3
commit 7dcb7ac
Showing
13 changed files
with
324 additions
and
12 deletions.
There are no files selected for viewing
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
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
39 changes: 39 additions & 0 deletions
39
packages/samples/headless-react/src/components/commerce/product-listing.fn.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,39 @@ | ||
import {ProductListing as HeadlessProductListing} from '@coveo/headless/commerce'; | ||
import {useEffect, useState, FunctionComponent} from 'react'; | ||
|
||
interface ProductListingProps { | ||
controller: HeadlessProductListing; | ||
} | ||
|
||
export const ProductListing: FunctionComponent<ProductListingProps> = ( | ||
props | ||
) => { | ||
const {controller} = props; | ||
const [state, setState] = useState(controller.state); | ||
|
||
useEffect(() => controller.subscribe(() => setState(controller.state)), []); | ||
|
||
if (!state.products.length) { | ||
return <button onClick={() => controller.refresh()}>Refresh</button>; | ||
} | ||
|
||
return ( | ||
<ul> | ||
{state.products.map(({ec_name, clickUri, permanentid}) => ( | ||
<li key={permanentid}> | ||
<a href={clickUri}>{ec_name}</a> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
}; | ||
|
||
// usage | ||
|
||
/** | ||
* ```tsx | ||
* const controller = buildProductListing(engine); | ||
* | ||
* <ProductListing controller={controller} />; | ||
* ``` | ||
*/ |
39 changes: 39 additions & 0 deletions
39
packages/samples/headless-react/src/components/commerce/recommendations.fn.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,39 @@ | ||
import {Recommendations as HeadlessRecommendations} from '@coveo/headless/commerce'; | ||
import {useEffect, useState, FunctionComponent} from 'react'; | ||
|
||
interface RecommendationsProps { | ||
controller: HeadlessRecommendations; | ||
} | ||
|
||
export const Recommendations: FunctionComponent<RecommendationsProps> = ( | ||
props | ||
) => { | ||
const {controller} = props; | ||
const [state, setState] = useState(controller.state); | ||
|
||
useEffect(() => controller.subscribe(() => setState(controller.state)), []); | ||
|
||
if (!state.products.length) { | ||
return <button onClick={() => controller.refresh()}>Refresh</button>; | ||
} | ||
|
||
return ( | ||
<ul> | ||
{state.products.map(({ec_name, clickUri, permanentid}) => ( | ||
<li key={permanentid}> | ||
<a href={clickUri}>{ec_name}</a> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
}; | ||
|
||
// usage | ||
|
||
/** | ||
* ```tsx | ||
* const controller = buildRecommendations(engine); | ||
* | ||
* <Recommendations controller={controller} />; | ||
* ``` | ||
*/ |
39 changes: 39 additions & 0 deletions
39
packages/samples/headless-react/src/components/commerce/search.fn.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,39 @@ | ||
import {Search as HeadlessSearch} from '@coveo/headless/commerce'; | ||
import {useEffect, useState, FunctionComponent} from 'react'; | ||
|
||
interface SearchProps { | ||
controller: HeadlessSearch; | ||
} | ||
|
||
export const Search: FunctionComponent<SearchProps> = (props) => { | ||
const {controller} = props; | ||
const [state, setState] = useState(controller.state); | ||
|
||
useEffect(() => controller.subscribe(() => setState(controller.state)), []); | ||
|
||
if (!state.products.length) { | ||
return ( | ||
<button onClick={() => controller.executeFirstSearch()}>Refresh</button> | ||
); | ||
} | ||
|
||
return ( | ||
<ul> | ||
{state.products.map(({ec_name, clickUri, permanentid}) => ( | ||
<li key={permanentid}> | ||
<a href={clickUri}>{ec_name}</a> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
}; | ||
|
||
// usage | ||
|
||
/** | ||
* ```tsx | ||
* const controller = buildSearch(engine); | ||
* | ||
* <Search controller={controller} />; | ||
* ``` | ||
*/ |
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
41 changes: 41 additions & 0 deletions
41
packages/samples/headless-react/src/pages/commerce/CommerceApp.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,41 @@ | ||
import {NavLink, Outlet} from 'react-router-dom'; | ||
import {Section} from '../../layout/section'; | ||
|
||
export function CommerceApp() { | ||
const activeNavLink: React.CSSProperties = {color: 'red'}; | ||
|
||
return ( | ||
<Section title="commerce"> | ||
<nav> | ||
<button> | ||
<NavLink | ||
end | ||
to="/commerce/search" | ||
style={({isActive}) => (isActive ? activeNavLink : {})} | ||
> | ||
Search | ||
</NavLink> | ||
</button> | ||
<button> | ||
<NavLink | ||
end | ||
to="/commerce/product-listing" | ||
style={({isActive}) => (isActive ? activeNavLink : {})} | ||
> | ||
Product Listing | ||
</NavLink> | ||
</button> | ||
<button> | ||
<NavLink | ||
end | ||
to="/commerce/recommendations" | ||
style={({isActive}) => (isActive ? activeNavLink : {})} | ||
> | ||
Recommendations | ||
</NavLink> | ||
</button> | ||
</nav> | ||
<Outlet /> | ||
</Section> | ||
); | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/samples/headless-react/src/pages/commerce/ProductListingPage.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,29 @@ | ||
import { | ||
buildCommerceEngine, | ||
buildProductListing, | ||
getSampleCommerceEngineConfiguration, | ||
} from '@coveo/headless/commerce'; | ||
import {useMemo} from 'react'; | ||
import {ProductListing} from '../../components/commerce/product-listing.fn'; | ||
import {AppContext} from '../../context/engine'; | ||
import {Section} from '../../layout/section'; | ||
|
||
export function ProductListingPage() { | ||
const engine = useMemo( | ||
() => | ||
buildCommerceEngine({ | ||
configuration: getSampleCommerceEngineConfiguration(), | ||
}), | ||
[] | ||
); | ||
|
||
const productListing = buildProductListing(engine); | ||
|
||
return ( | ||
<AppContext.Provider value={{commerceEngine: engine}}> | ||
<Section title="product-listing"> | ||
<ProductListing controller={productListing} /> | ||
</Section> | ||
</AppContext.Provider> | ||
); | ||
} |
Oops, something went wrong.