-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathsetup-app.ts
More file actions
87 lines (75 loc) · 2.82 KB
/
setup-app.ts
File metadata and controls
87 lines (75 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
// Third-Party
import React from 'react'
import {RouteProps} from 'react-router-dom'
// Platform Imports
import {
ApplicationExtension,
SliceInitializer,
withApplicationExtensionStore
} from '@salesforce/pwa-kit-extension-sdk/react'
import {applyHOCs} from '@salesforce/pwa-kit-extension-sdk/react/utils'
// Local Imports
import {withOptionalChakra} from './components/with-optional-chakra-provider'
import {withOptionalCommerceSdkReactProvider} from './components/with-optional-commerce-sdk-react-provider'
import {withStoreLocator} from './components/with-store-locator'
import {Config} from './types'
import StoreLocatorPage from './pages/store-locator'
import {logger} from './logger'
import extensionMeta from '../extension-meta.json'
interface StoreSlice {
isModalOpen: boolean
openModal: () => void
closeModal: () => void
}
// @TODO: I noticed a bug that the initial render on client with have the store state always default to {}.
const storeSliceInitializer: SliceInitializer<StoreSlice> = (set) => ({
isModalOpen: false,
openModal: () => {
set((state) => ({...state, isModalOpen: true}))
},
closeModal: () => {
set((state) => ({...state, isModalOpen: false}))
}
})
class StoreLocatorExtension extends ApplicationExtension<Config> {
static readonly id = extensionMeta.id
extendApp<T extends React.ComponentType<T>>(
App: React.ComponentType<T>
): React.ComponentType<T> {
const config = this.getConfig()
if (!config.supportedCountries || config.supportedCountries.length === 0) {
logger.error(
'[extension-chakra-store-locator] Missing supportedCountries, this extension will not work.'
)
}
const HOCs = [
(component: React.ComponentType<any>) =>
withApplicationExtensionStore(component, {
id: extensionMeta.id,
initializer: storeSliceInitializer
}),
(component: React.ComponentType<any>) => withStoreLocator(component, config),
(component: React.ComponentType<any>) =>
withOptionalCommerceSdkReactProvider(component, config),
(component: React.ComponentType<any>) => withOptionalChakra(component)
]
return applyHOCs(App, HOCs)
}
extendRoutes(routes: RouteProps[]): RouteProps[] {
return [
{
exact: true,
path: this.getConfig().path,
component: StoreLocatorPage
},
...routes
]
}
}
export default StoreLocatorExtension