-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathindex.jsx
More file actions
99 lines (84 loc) · 3.56 KB
/
index.jsx
File metadata and controls
99 lines (84 loc) · 3.56 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
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright (c) 2021, 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
*/
import {useEffect, useContext} from 'react'
import {useLocation, useHistory} from 'react-router-dom'
import PropTypes from 'prop-types'
import useSeStoreSelection from '@salesforce/retail-react-app/app/hooks/use-se-store-selection'
import {useStoreLocatorParams} from '@salesforce/retail-react-app/app/contexts/store-locator-params'
import useExternalSearch from '@salesforce/retail-react-app/app/hooks/use-external-search'
import {StoreLocatorContext} from '@salesforce/retail-react-app/app/contexts/store-locator-provider'
const SeInputHandler = ({onOpenStoreLocator}) => {
useExternalSearch()
const location = useLocation()
const history = useHistory()
const {shouldOpenModal, setShouldOpenModal, storeLocatorParams, processSeParameters} =
useSeStoreSelection()
const {setParams} = useStoreLocatorParams()
const {setState: setStoreLocatorState} = useContext(StoreLocatorContext) || {}
useEffect(() => {
const urlParams = new URLSearchParams(location.search)
processSeParameters(urlParams)
}, [location.search, processSeParameters])
// Handle store locator params updates
useEffect(() => {
if (!storeLocatorParams) return
setParams(storeLocatorParams)
const storeName = new URLSearchParams(location.search).get('store')
if (setStoreLocatorState && !storeName) {
if (storeLocatorParams.postalCode && storeLocatorParams.countryCode) {
setStoreLocatorState((prev) => ({
...prev,
mode: 'input',
formValues: {
postalCode: storeLocatorParams.postalCode,
countryCode: storeLocatorParams.countryCode
}
}))
} else if (storeLocatorParams.latitude && storeLocatorParams.longitude) {
setStoreLocatorState((prev) => ({
...prev,
mode: 'device',
deviceCoordinates: {
latitude: storeLocatorParams.latitude,
longitude: storeLocatorParams.longitude
}
}))
}
}
}, [storeLocatorParams, setParams, setStoreLocatorState])
useEffect(() => {
if (!shouldOpenModal || !storeLocatorParams) return
const urlParams = new URLSearchParams(location.search)
const hasSeParamKeys = ['lat', 'lng', 'zip', 'city', 'store', 'country']
onOpenStoreLocator()
setShouldOpenModal(false)
const hasSeParams = hasSeParamKeys.some((key) => urlParams.has(key))
if (hasSeParams) {
cleanURLParams(location, history, hasSeParamKeys)
}
}, [
shouldOpenModal,
storeLocatorParams,
onOpenStoreLocator,
setShouldOpenModal,
location.search,
location.pathname,
history
])
return null
}
export const cleanURLParams = (location, history, hasSeParamKeys) => {
const cleanParams = new URLSearchParams(location.search)
hasSeParamKeys.forEach((key) => cleanParams.delete(key))
const cleanSearch = cleanParams.toString()
const newUrl = location.pathname + (cleanSearch ? `?${cleanSearch}` : '')
history.replace(newUrl)
}
SeInputHandler.propTypes = {
onOpenStoreLocator: PropTypes.func.isRequired
}
export default SeInputHandler