-
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.
docs(headless): showcase the context controller in headless-ssr-comme…
…rce (#4649) https://coveord.atlassian.net/browse/KIT-3708 this PR adds better support for the context controller in headless-ssr-commerce by adding a contextDropdown with a hardcoded list of storefront associations. --------- Co-authored-by: ylakhdar <[email protected]>
- Loading branch information
1 parent
ea6085c
commit 5e516a4
Showing
6 changed files
with
97 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
62 changes: 62 additions & 0 deletions
62
packages/samples/headless-ssr-commerce/components/context-dropdown.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,62 @@ | ||
'use client'; | ||
|
||
import {useContext, useEngine} from '@/lib/commerce-engine'; | ||
import { | ||
CommerceEngine, | ||
ContextOptions, | ||
loadProductListingActions, | ||
loadSearchActions, | ||
} from '@coveo/headless-react/ssr-commerce'; | ||
|
||
// A hardcoded list of storefront associations for switching app context by language, country, and currency. | ||
// Found in the admin console under "Storefront Associations," this list is static for demonstration purposes. | ||
// In a real application, these values would likely come from sources like environment variables or an API. | ||
const storefrontAssociations = [ | ||
'en-CA-CAD', | ||
'fr-CA-CAD', | ||
'en-GB-GBP', | ||
'en-US-USD', | ||
]; | ||
|
||
export default function ContextDropdown({ | ||
useCase, | ||
}: { | ||
useCase?: 'listing' | 'search'; | ||
}) { | ||
const {state, methods} = useContext(); | ||
const engine = useEngine(); | ||
|
||
return ( | ||
<div> | ||
<p></p> | ||
Context dropdown : | ||
<select | ||
value={`${state.language}-${state.country}-${state.currency}`} | ||
onChange={(e) => { | ||
const [language, country, currency] = e.target.value.split('-'); | ||
methods?.setLanguage(language); | ||
methods?.setCountry(country); | ||
methods?.setCurrency(currency as ContextOptions['currency']); | ||
|
||
useCase === 'search' | ||
? engine?.dispatch( | ||
loadSearchActions(engine as CommerceEngine).executeSearch() | ||
) | ||
: useCase === 'listing' && | ||
engine?.dispatch( | ||
loadProductListingActions( | ||
engine as CommerceEngine | ||
).fetchProductListing() | ||
); | ||
}} | ||
> | ||
{storefrontAssociations.map((association) => ( | ||
<option key={association} value={association}> | ||
{association} | ||
</option> | ||
))} | ||
</select> | ||
<p></p> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {ContextState} from '@coveo/headless-react/ssr-commerce'; | ||
|
||
export const defaultContext: { | ||
language: string; | ||
country: string; | ||
currency: ContextState['currency']; | ||
} = { | ||
language: 'en', | ||
country: 'US', | ||
currency: 'USD', | ||
}; |