-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
390 lines (376 loc) · 16.7 KB
/
index.jsx
File metadata and controls
390 lines (376 loc) · 16.7 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* 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 React, {useRef, useState} from 'react'
import PropTypes from 'prop-types'
import {useIntl} from 'react-intl'
import {
useMultiStyleConfig,
Box,
Flex,
IconButton,
Badge,
Button,
Popover,
PopoverHeader,
PopoverTrigger,
PopoverContent,
PopoverBody,
PopoverFooter,
PopoverArrow,
Stack,
Text,
Divider,
useDisclosure,
useMediaQuery
} from '@salesforce/retail-react-app/app/components/shared/ui'
import {AuthHelpers, useAuthHelper, useCustomerType} from '@salesforce/commerce-sdk-react'
import {useCurrentBasket} from '@salesforce/retail-react-app/app/hooks/use-current-basket'
import Link from '@salesforce/retail-react-app/app/components/link'
import Search from '@salesforce/retail-react-app/app/components/search'
import withRegistration from '@salesforce/retail-react-app/app/components/with-registration'
import {
AccountIcon,
BrandLogo,
BasketIcon,
HamburgerIcon,
ChevronDownIcon,
HeartIcon,
SignoutIcon,
StoreIcon,
SparkleIcon
} from '@salesforce/retail-react-app/app/components/icons'
import {navLinks, messages} from '@salesforce/retail-react-app/app/pages/account/constant'
import useNavigation from '@salesforce/retail-react-app/app/hooks/use-navigation'
import LoadingSpinner from '@salesforce/retail-react-app/app/components/loading-spinner'
import {HideOnDesktop, HideOnMobile} from '@salesforce/retail-react-app/app/components/responsive'
import {isHydrated, noop} from '@salesforce/retail-react-app/app/utils/utils'
import {STORE_LOCATOR_IS_ENABLED} from '@salesforce/retail-react-app/app/constants'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import {getCommerceAgentConfig} from '@salesforce/retail-react-app/app/utils/config-utils'
const IconButtonWithRegistration = withRegistration(IconButton)
/**
* Search bar for the header.
*
* The search bar is a simple input field with a search icon.
* It can be used to search for products or navigate to a
* specific page.
*
* @param props {object} the component props
* @returns {Element} the search bar element
*/
const SearchBar = (props) => {
const styles = useMultiStyleConfig('Header')
const intl = useIntl()
const placeholder = intl.formatMessage({
id: 'header.field.placeholder.search_for_products',
defaultMessage: 'Search for products...'
})
return (
<Box {...styles.searchContainer}>
<Search
aria-label={placeholder}
placeholder={placeholder}
{...styles.search}
{...props}
/>
</Box>
)
}
/**
* The header is the main source for accessing
* navigation, search, basket, and other
* important information and actions. It persists
* on the top of your application and will
* respond to changes in device size.
*
* To customize the styles, update the themes
* in theme/components/project/header.js
* @param props
* @param {func} props.onMenuClick click event handler for menu button
* @param {func} props.onLogoClick click event handler for menu button
* @param {object} props.searchInputRef reference of the search input
* @param {func} props.onMyAccountClick click event handler for my account button
* @param {func} props.onMyCartClick click event handler for my cart button
* @param {func} props.onAgentClick click event handler for agent button
* @return {React.ReactElement} - Header component
*/
const Header = ({
children,
onMenuClick = noop,
onMyAccountClick = noop,
onLogoClick = noop,
onMyCartClick = noop,
onWishlistClick = noop,
onStoreLocatorClick = noop,
onAgentClick = noop,
...props
}) => {
const intl = useIntl()
const popoverTriggerRef = useRef(null)
const {
derivedData: {totalItems},
data: basket
} = useCurrentBasket()
const {isRegistered} = useCustomerType()
const logout = useAuthHelper(AuthHelpers.Logout)
const navigate = useNavigation()
const storeLocatorEnabled = getConfig()?.app?.storeLocatorEnabled ?? STORE_LOCATOR_IS_ENABLED
const commerceAgentConfig = getCommerceAgentConfig()
const showLaunchAgentButton = commerceAgentConfig?.enableAgentFromHeader === 'true'
const {
getButtonProps: getAccountMenuButtonProps,
getDisclosureProps: getAccountMenuDisclosureProps,
isOpen: isAccountMenuOpen,
onClose: onAccountMenuClose,
onOpen: onAccountMenuOpen
} = useDisclosure()
const [isDesktop] = useMediaQuery('(min-width: 992px)')
const [showLoading, setShowLoading] = useState(false)
// tracking if users enter the popover Content,
// so we can decide whether to close the menu when users leave account icons
const hasEnterPopoverContent = useRef()
const styles = useMultiStyleConfig('Header')
const onSignoutClick = async () => {
setShowLoading(true)
await logout.mutateAsync()
navigate('/login')
setShowLoading(false)
}
const handleIconsMouseLeave = () => {
// don't close the menu if users enter the popover content
setTimeout(() => {
if (!hasEnterPopoverContent.current) onAccountMenuClose()
}, 100)
}
const handleKeyDown = (event) => {
if (event.key === 'Tab' && event.shiftKey && isAccountMenuOpen) {
// Prevent default behavior to keep focus on the popup trigger
event.preventDefault()
popoverTriggerRef.current.focus()
}
}
return (
<Box as="header" role="banner" {...styles.container} {...props}>
<Box {...styles.content}>
{showLoading && <LoadingSpinner wrapperStyles={{height: '100vh'}} />}
<Flex wrap="wrap" alignItems={['baseline', 'baseline', 'baseline', 'center']}>
<IconButton
aria-label={intl.formatMessage({
id: 'header.button.assistive_msg.menu',
defaultMessage: 'Menu'
})}
title={intl.formatMessage({
id: 'header.button.assistive_msg.menu.open_dialog',
defaultMessage: 'Opens a dialog'
})}
icon={<HamburgerIcon />}
variant="unstyled"
display={{lg: 'none'}}
{...styles.icons}
onClick={onMenuClick}
/>
<IconButton
aria-label={intl.formatMessage({
id: 'header.button.assistive_msg.logo',
defaultMessage: 'Logo'
})}
icon={<BrandLogo {...styles.logo} />}
{...styles.icons}
variant="unstyled"
onClick={onLogoClick}
/>
<Box {...styles.bodyContainer}>{children}</Box>
<HideOnMobile>
<SearchBar />
</HideOnMobile>
{showLaunchAgentButton && (
<IconButton
icon={<SparkleIcon />}
aria-label={intl.formatMessage({
id: 'header.button.assistive_msg.ask_shopping_agent',
defaultMessage: 'Ask Shopping Agent'
})}
variant="unstyled"
{...styles.icons}
onClick={onAgentClick}
/>
)}
<IconButtonWithRegistration
icon={<AccountIcon />}
aria-label={intl.formatMessage({
id: 'header.button.assistive_msg.my_account',
defaultMessage: 'My Account'
})}
variant="unstyled"
{...styles.icons}
{...styles.accountIcon}
onClick={onMyAccountClick}
onMouseOver={isDesktop ? onAccountMenuOpen : noop}
/>
{isRegistered && isHydrated() && (
<Popover
isLazy
arrowSize={15}
isOpen={isAccountMenuOpen}
placement="bottom-end"
onClose={onAccountMenuClose}
onOpen={onAccountMenuOpen}
>
<PopoverTrigger>
<IconButton
aria-label={intl.formatMessage({
id: 'header.button.assistive_msg.my_account_menu',
defaultMessage: 'Open account menu'
})}
icon={<ChevronDownIcon />}
variant="unstyled"
{...styles.icons}
{...styles.arrowDown}
{...getAccountMenuButtonProps()}
onMouseOver={onAccountMenuOpen}
onMouseLeave={handleIconsMouseLeave}
ref={popoverTriggerRef}
onKeyDown={handleKeyDown}
/>
</PopoverTrigger>
<PopoverContent
{...styles.popoverContent}
onMouseLeave={() => {
hasEnterPopoverContent.current = false
onAccountMenuClose()
}}
onMouseOver={() => {
hasEnterPopoverContent.current = true
}}
{...getAccountMenuDisclosureProps()}
>
<PopoverArrow />
<PopoverHeader>
<Text as="h2" fontSize="l" fontFamily="body" fontWeight="700">
{intl.formatMessage({
defaultMessage: 'My Account',
id: 'header.popover.title.my_account'
})}
</Text>
</PopoverHeader>
<PopoverBody>
<Box as="nav">
<Stack spacing={0} as="ul" data-testid="account-detail-nav">
{navLinks.map((link) => {
const LinkIcon = link.icon
return (
<Box
key={link.name}
position="relative"
as="li"
listStyleType="none"
>
<Button
as={Link}
to={`/account${link.path}`}
useNavLink={true}
variant="menu-link"
leftIcon={<LinkIcon boxSize={5} />}
width="100%"
>
{intl.formatMessage(
messages[link.name]
)}
</Button>
</Box>
)
})}
</Stack>
</Box>
</PopoverBody>
<PopoverFooter onClick={onSignoutClick} cursor="pointer">
<Divider colorScheme="gray" />
<Button variant="unstyled" {...styles.signout}>
<Flex>
<SignoutIcon
aria-hidden={true}
boxSize={5}
{...styles.signoutIcon}
/>
<Text as="span" {...styles.signoutText}>
{intl.formatMessage({
defaultMessage: 'Log out',
id: 'header.popover.action.log_out'
})}
</Text>
</Flex>
</Button>
</PopoverFooter>
</PopoverContent>
</Popover>
)}
<IconButtonWithRegistration
aria-label={intl.formatMessage({
defaultMessage: 'Wishlist',
id: 'header.button.assistive_msg.wishlist'
})}
icon={<HeartIcon />}
variant="unstyled"
{...styles.icons}
{...styles.wishlistIcon}
onClick={onWishlistClick}
/>
{storeLocatorEnabled && (
<IconButton
aria-label={intl.formatMessage({
defaultMessage: 'Store Locator',
id: 'header.button.assistive_msg.store_locator'
})}
icon={<StoreIcon />}
{...styles.icons}
variant="unstyled"
onClick={onStoreLocatorClick}
/>
)}
<IconButton
aria-label={intl.formatMessage(
{
id: 'header.button.assistive_msg.my_cart_with_num_items',
defaultMessage: 'My cart, number of items: {numItems}'
},
{numItems: totalItems}
)}
icon={
<>
<BasketIcon />
{basket && totalItems > 0 && (
<Badge variant="notification">{totalItems}</Badge>
)}
</>
}
variant="unstyled"
{...styles.icons}
onClick={onMyCartClick}
/>
<HideOnDesktop display={{base: 'contents', lg: 'none'}}>
<SearchBar />
</HideOnDesktop>
</Flex>
</Box>
</Box>
)
}
Header.propTypes = {
children: PropTypes.node,
onMenuClick: PropTypes.func,
onLogoClick: PropTypes.func,
onMyAccountClick: PropTypes.func,
onWishlistClick: PropTypes.func,
onMyCartClick: PropTypes.func,
onStoreLocatorClick: PropTypes.func,
onAgentClick: PropTypes.func,
searchInputRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({current: PropTypes.elementType})
])
}
export default Header