mirror of
https://github.com/vercel/commerce.git
synced 2025-07-27 04:01:23 +00:00
assets
components
auth
cart
common
Avatar
FeatureBar
Footer
Head
HomeAllProductsGrid
I18nWidget
Layout
Navbar
Searchbar
UserNav
DropdownMenu.module.css
DropdownMenu.tsx
UserNav.module.css
UserNav.tsx
index.ts
index.ts
icons
product
ui
wishlist
config
docs
lib
pages
public
.editorconfig
.env.template
.gitignore
.prettierignore
README.md
global.d.ts
license.md
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import { FC } from 'react'
|
|
import Link from 'next/link'
|
|
import cn from 'classnames'
|
|
import useCart from '@bigcommerce/storefront-data-hooks/cart/use-cart'
|
|
import useCustomer from '@bigcommerce/storefront-data-hooks/use-customer'
|
|
import { Heart, Bag } from '@components/icons'
|
|
import { useUI } from '@components/ui/context'
|
|
import DropdownMenu from './DropdownMenu'
|
|
import s from './UserNav.module.css'
|
|
import { Avatar } from '@components/common'
|
|
|
|
interface Props {
|
|
className?: string
|
|
}
|
|
|
|
const countItem = (count: number, item: any) => count + item.quantity
|
|
const countItems = (count: number, items: any[]) =>
|
|
items.reduce(countItem, count)
|
|
|
|
const UserNav: FC<Props> = ({ className, children, ...props }) => {
|
|
const { data } = useCart()
|
|
const { data: customer } = useCustomer()
|
|
const { toggleSidebar, closeSidebarIfPresent, openModal } = useUI()
|
|
const itemsCount = Object.values(data?.line_items ?? {}).reduce(countItems, 0)
|
|
|
|
return (
|
|
<nav className={cn(s.root, className)}>
|
|
<div className={s.mainContainer}>
|
|
<ul className={s.list}>
|
|
<li className={s.item} onClick={toggleSidebar}>
|
|
<Bag />
|
|
{itemsCount > 0 && <span className={s.bagCount}>{itemsCount}</span>}
|
|
</li>
|
|
<li className={s.item}>
|
|
<Link href="/wishlist">
|
|
<a onClick={closeSidebarIfPresent} aria-label="Wishlist">
|
|
<Heart />
|
|
</a>
|
|
</Link>
|
|
</li>
|
|
<li className={s.item}>
|
|
{customer ? (
|
|
<DropdownMenu />
|
|
) : (
|
|
<button
|
|
className={s.avatarButton}
|
|
aria-label="Menu"
|
|
onClick={() => openModal()}
|
|
>
|
|
<Avatar />
|
|
</button>
|
|
)}
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</nav>
|
|
)
|
|
}
|
|
|
|
export default UserNav
|