-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
347 lines (333 loc) · 15 KB
/
index.jsx
File metadata and controls
347 lines (333 loc) · 15 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
/*
* Copyright (c) 2025, 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, {useState} from 'react'
import PropTypes from 'prop-types'
import {useIntl} from 'react-intl'
import {
Badge,
Box,
Button,
Flex,
Heading,
IconButton,
List,
Popover,
Text,
Separator,
// hooks
useSlotRecipe
} from '@chakra-ui/react'
import {AuthHelpers, useAuthHelper, useCustomerType} from '@salesforce/commerce-sdk-react'
import {
useApplicationExtension,
useApplicationExtensionsStore
} from '@salesforce/pwa-kit-extension-sdk/react'
import {useCurrentBasket} from '../../hooks'
import Link from '../../components/link'
import Search from '../../components/search'
import withRegistration from '../../components/with-registration'
import {
AccountIcon,
BrandLogo,
BasketIcon,
HamburgerIcon,
ChevronDownIcon,
HeartIcon,
SignoutIcon,
StoreIcon
} from '../icons'
import {navLinks, messages} from '../../pages/account/constant'
import useNavigation from '../../hooks/use-navigation'
import LoadingSpinner from '../../components/loading-spinner'
import {HideOnDesktop, HideOnMobile} from '../responsive'
import {noop} from '../../utils/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 recipe = useSlotRecipe({key: 'header'})
const styles = recipe()
const intl = useIntl()
const placeholder = intl.formatMessage({
id: 'header.field.placeholder.search_for_products',
defaultMessage: 'Search for products...'
})
return (
<Box css={styles.searchContainer}>
<Search aria-label={placeholder} placeholder={placeholder} {...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.onWishlistClick click event handler for with list button
* @param {React.ReactElement} props.children - React Node to be rendered inside Header
* @return {React.ReactElement} - Header component
*/
const Header = ({
children,
onMenuClick = noop,
onMyAccountClick = noop,
onLogoClick = noop,
onMyCartClick = noop,
onWishlistClick = noop,
...props
}) => {
const intl = useIntl()
const {
derivedData: {totalItems},
data: basket
} = useCurrentBasket()
const {isRegistered} = useCustomerType()
const logout = useAuthHelper(AuthHelpers.Logout)
const navigate = useNavigation()
const storeLocatorExtension = useApplicationExtension(
'@salesforce/extension-chakra-store-locator'
)
const isStoreLocatorEnabled = !!storeLocatorExtension && storeLocatorExtension.isEnabled
const openModal = useApplicationExtensionsStore((state) => {
return state.state['@salesforce/extension-chakra-store-locator']?.openModal || noop
})
const [showLoading, setShowLoading] = useState(false)
const recipe = useSlotRecipe({key: 'header'})
const styles = recipe()
const onSignoutClick = async () => {
setShowLoading(true)
await logout.mutateAsync()
navigate('/login')
setShowLoading(false)
}
return (
<Box css={styles.container} {...props}>
<Box css={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'
})}
css={styles.iconButton}
variant="unstyled"
display={{lg: 'none'}}
onClick={onMenuClick}
>
<HamburgerIcon />
</IconButton>
<IconButton
aria-label={intl.formatMessage({
id: 'header.button.assistive_msg.logo',
defaultMessage: 'Logo'
})}
css={styles.iconButton}
variant="unstyled"
onClick={onLogoClick}
>
<BrandLogo css={styles.logo} />
</IconButton>
<Box css={styles.bodyContainer}>{children}</Box>
<HideOnMobile>
<SearchBar />
</HideOnMobile>
<IconButtonWithRegistration
aria-label={intl.formatMessage({
id: 'header.button.assistive_msg.my_account',
defaultMessage: 'My Account'
})}
variant="unstyled"
css={{...styles.iconButton, ...styles.accountIconButton}}
onClick={onMyAccountClick}
>
<AccountIcon boxSize="6" />
</IconButtonWithRegistration>
{isRegistered && (
<Popover.Root
lazyMount
unmountOnExit
defaultOpen={false}
positioning={{placement: 'bottom-end'}}
>
<Popover.Trigger asChild>
<IconButton
gap={0}
variant="unstyled"
aria-label={intl.formatMessage({
id: 'header.button.assistive_msg.my_account_menu',
defaultMessage: 'Open account menu'
})}
css={{...styles.iconButton, ...styles.arrowDownButton}}
>
<HideOnMobile>
<ChevronDownIcon />
</HideOnMobile>
</IconButton>
</Popover.Trigger>
<Popover.Positioner>
<Popover.Content>
<Popover.Arrow />
<Popover.Body css={styles.dropdownMenuBody}>
<Popover.Header pb={1}>
<Popover.Title>
<Heading as="h2" size="lg">
{intl.formatMessage({
defaultMessage: 'My Account',
id: 'header.popover.title.my_account'
})}
</Heading>
</Popover.Title>
</Popover.Header>
<Box asChild px="3">
<nav>
<List.Root
variant="plain"
as="ul"
data-testid="account-detail-nav"
>
{navLinks.map((link, index) => {
const LinkIcon = link.icon
return (
<List.Item key={link.name}>
<Button
asChild
variant="menu-link"
css={styles.menuAccountLink}
>
<Link
to={`/account${link.path}`}
useNavLink={true}
>
<LinkIcon
boxSize="5"
mr="2"
/>
{intl.formatMessage(
messages[link.name]
)}
</Link>
</Button>
</List.Item>
)
})}
</List.Root>
</nav>
</Box>
<Separator mx="3" my="2" />
<Popover.Footer px="3" py="0">
<Button
variant="ghost"
css={styles.signoutButton}
onClick={onSignoutClick}
>
<SignoutIcon
aria-hidden={true}
boxSize="5"
css={styles.signoutIcon}
/>
<Text as="span" css={styles.signoutText}>
{intl.formatMessage({
defaultMessage: 'Log out',
id: 'header.popover.action.log_out'
})}
</Text>
</Button>
</Popover.Footer>
</Popover.Body>
</Popover.Content>
</Popover.Positioner>
</Popover.Root>
)}
<IconButtonWithRegistration
aria-label={intl.formatMessage({
defaultMessage: 'Wishlist',
id: 'header.button.assistive_msg.wishlist'
})}
variant="unstyled"
css={{...styles.iconButton, ...styles.wishlistIconButton}}
onClick={onWishlistClick}
>
<HeartIcon boxSize="6" />
</IconButtonWithRegistration>
{isStoreLocatorEnabled && (
<IconButton
aria-label={intl.formatMessage({
defaultMessage: 'Store Locator',
id: 'header.button.assistive_msg.store_locator'
})}
css={styles.iconButton}
variant="unstyled"
onClick={() => {
openModal()
}}
>
<StoreIcon boxSize="6" />
</IconButton>
)}
<IconButton
aria-label={intl.formatMessage(
{
id: 'header.button.assistive_msg.my_cart_with_num_items',
defaultMessage: 'My cart, number of items: {numItems}'
},
{numItems: totalItems}
)}
variant="unstyled"
css={styles.iconButton}
onClick={onMyCartClick}
>
<>
<BasketIcon boxSize="6" />
{basket && totalItems > 0 && (
<Badge variant="notification">{totalItems}</Badge>
)}
</>
</IconButton>
<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,
searchInputRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({current: PropTypes.elementType})
])
}
export default Header