Skip to content

Commit 7a79859

Browse files
committed
add a default commerce api provider to store locator extension
1 parent f9a08ae commit 7a79859

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

packages/extension-chakra-store-locator/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ If you want to use this without having to install `@chakra-ui` in your project,
3535

3636
### `@salesforce/commerce-sdk-react` Provider
3737

38-
This extension uses the `@salesforce/commerce-sdk-react` package to fetch the store locator data from SCAPI. Your application must use the `CommerceApiProvider` in the React component tree.
38+
This extension uses the `@salesforce/commerce-sdk-react` package to fetch the store locator data from SCAPI. If you provide a `commerceApi` configuration in the extension config, the `CommerceApiProvider` will be added to the React component tree as the default provider. If you already have a `CommerceApiProvider` in your application, do not include the `commerceApi` configuration in the extension config.
3939

4040
## Configurations
4141

@@ -66,7 +66,16 @@ The Store Locator extension is configured via the `mobify.app.extensions` proper
6666
"countryCode": "DE",
6767
"countryName": "Germany"
6868
}
69-
]
69+
],
70+
"commerceApi": {
71+
"proxyPath": "/mobify/proxy/api",
72+
"parameters": {
73+
"shortCode": "8o7m175y",
74+
"clientId": "c9c45bfd-0ed3-4aa2-9971-40f88962b836",
75+
"organizationId": "f_ecom_zzrf_001",
76+
"siteId": "RefArchGlobal"
77+
}
78+
}
7079
}
7180
]
7281
]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2024, salesforce.com, inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
8+
import React from 'react'
9+
import {getAppOrigin} from '@salesforce/pwa-kit-react-sdk/utils/url'
10+
import {CommerceApiProvider} from '@salesforce/commerce-sdk-react'
11+
import {UserConfig} from '../types/config'
12+
13+
type WithOptionalCommerceSdkReactProvider = React.ComponentPropsWithoutRef<any>
14+
15+
/**
16+
* Higher-order component that conditionally wraps a given component with ChakraProvider.
17+
*
18+
* @param WrappedComponent - The component to be optionally wrapped with ChakraProvider.
19+
* @param theme - Optional Chakra UI theme to be used
20+
* @returns A component that wraps the given component with ChakraProvider if it is not already present in the component tree.
21+
*/
22+
export const withOptionalCommerceSdkReactProvider = <P extends object>(
23+
WrappedComponent: React.ComponentType<P>,
24+
config: UserConfig
25+
) => {
26+
const HOC: React.FC<P> = (props: WithOptionalCommerceSdkReactProvider) => {
27+
if (!config.commerceApi || !config.commerceApi?.parameters) {
28+
return <WrappedComponent {...(props as P)} />
29+
}
30+
const appOrigin = getAppOrigin()
31+
return (
32+
<CommerceApiProvider
33+
shortCode={config.commerceApi.parameters.shortCode}
34+
clientId={config.commerceApi.parameters.clientId}
35+
organizationId={config.commerceApi.parameters.organizationId}
36+
siteId={config.commerceApi.parameters.siteId}
37+
locale={config.commerceApi.parameters.locale}
38+
currency={config.commerceApi.parameters.currency}
39+
redirectURI={`${appOrigin}/callback`}
40+
proxy={`${appOrigin}${config.commerceApi.proxyPath}`}
41+
>
42+
<WrappedComponent {...(props as P)} />
43+
</CommerceApiProvider>
44+
)
45+
}
46+
47+
return HOC
48+
}

packages/extension-chakra-store-locator/src/setup-app.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {ApplicationExtension} from '@salesforce/pwa-kit-extension-sdk/react'
1414

1515
// Local Imports
1616
import {withOptionalChakra} from './components/with-optional-chakra-provider'
17+
import {withOptionalCommerceSdkReactProvider} from './components/with-optional-commerce-sdk-react-provider'
1718
import {withStoreLocator} from './components/with-store-locator'
1819
import {Config} from './types'
1920

@@ -35,7 +36,7 @@ class StoreLocatorExtension extends ApplicationExtension<Config> {
3536
)
3637
}
3738

38-
return withStoreLocator(withOptionalChakra(App), config)
39+
return withStoreLocator(withOptionalCommerceSdkReactProvider(withOptionalChakra(App), config), config)
3940
}
4041

4142
extendRoutes(routes: RouteProps[]): RouteProps[] {

packages/extension-chakra-store-locator/src/types/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ export interface UserConfig extends ApplicationExtensionConfig {
2121
countryCode: string
2222
countryName: string
2323
}>
24+
commerceApi?: {
25+
proxyPath: string
26+
parameters: {
27+
shortCode: string
28+
clientId: string
29+
organizationId: string
30+
siteId: string
31+
locale: string
32+
currency: string
33+
}
34+
}
2435
}
2536

2637
/**

packages/template-typescript-minimal/package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@emotion/react": "^11.13.3",
2323
"@emotion/styled": "^11.13.0",
2424
"@loadable/component": "^5.15.3",
25+
"@salesforce/extension-chakra-store-locator": "^0.1.0-extensibility-preview.3",
2526
"@salesforce/pwa-kit-dev": "4.0.0-extensibility-preview.3",
2627
"@salesforce/pwa-kit-extension-sdk": "4.0.0-extensibility-preview.3",
2728
"@salesforce/pwa-kit-react-sdk": "4.0.0-extensibility-preview.3",
@@ -44,6 +45,44 @@
4445
"npm": "^8.0.0 || ^9.0.0 || ^10.0.0"
4546
},
4647
"mobify": {
48+
"app": {
49+
"extensions": [
50+
[
51+
"@salesforce/extension-chakra-store-locator",
52+
{
53+
"enabled": true,
54+
"path": "/store-locator",
55+
"radius": 100,
56+
"radiusUnit": "km",
57+
"defaultPageSize": 10,
58+
"defaultPostalCode": "10178",
59+
"defaultCountry": "Germany",
60+
"defaultCountryCode": "DE",
61+
"supportedCountries": [
62+
{
63+
"countryCode": "US",
64+
"countryName": "United States"
65+
},
66+
{
67+
"countryCode": "DE",
68+
"countryName": "Germany"
69+
}
70+
],
71+
"commerceApi": {
72+
"proxyPath": "/mobify/proxy/api",
73+
"parameters": {
74+
"shortCode": "8o7m175y",
75+
"clientId": "c9c45bfd-0ed3-4aa2-9971-40f88962b836",
76+
"organizationId": "f_ecom_zzrf_001",
77+
"siteId": "RefArchGlobal",
78+
"locale": "en-GB",
79+
"currency": "USD"
80+
}
81+
}
82+
}
83+
]
84+
]
85+
},
4786
"ssrEnabled": true,
4887
"ssrOnly": [
4988
"ssr.js",

0 commit comments

Comments
 (0)