4
0
forked from crowetic/commerce

Merge pull request #21 from marbiano/mini-cart-fixes

Sidebar fixes
This commit is contained in:
B 2020-10-27 01:23:01 -03:00 committed by GitHub
commit fe7b00b72b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import { useTheme } from 'next-themes'
import cn from 'classnames' import cn from 'classnames'
import s from './DropdownMenu.module.css' import s from './DropdownMenu.module.css'
import { Moon, Sun } from '@components/icons' import { Moon, Sun } from '@components/icons'
import { useUI } from '@components/ui/context'
import { Menu, Transition } from '@headlessui/react' import { Menu, Transition } from '@headlessui/react'
import useLogout from '@bigcommerce/storefront-data-hooks/dist/use-logout' import useLogout from '@bigcommerce/storefront-data-hooks/dist/use-logout'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
@ -32,6 +33,8 @@ const DropdownMenu: FC<DropdownMenuProps> = ({ open = false }) => {
const logout = useLogout() const logout = useLogout()
const { pathname } = useRouter() const { pathname } = useRouter()
const { closeSidebarIfPresent } = useUI()
return ( return (
<Transition <Transition
show={open} show={open}
@ -51,6 +54,7 @@ const DropdownMenu: FC<DropdownMenuProps> = ({ open = false }) => {
className={cn(s.link, { className={cn(s.link, {
[s.active]: pathname === href, [s.active]: pathname === href,
})} })}
onClick={closeSidebarIfPresent}
> >
{name} {name}
</a> </a>

View File

@ -21,21 +21,18 @@ const UserNav: FC<Props> = ({ className, children, ...props }) => {
const { data } = useCart() const { data } = useCart()
const { data: customer } = useCustomer() const { data: customer } = useCustomer()
const { openSidebar, closeSidebar, displaySidebar, openModal } = useUI() const { toggleSidebar, closeSidebarIfPresent, openModal } = useUI()
const itemsCount = Object.values(data?.line_items ?? {}).reduce(countItems, 0) const itemsCount = Object.values(data?.line_items ?? {}).reduce(countItems, 0)
return ( return (
<nav className={cn(s.root, className)}> <nav className={cn(s.root, className)}>
<div className={s.mainContainer}> <div className={s.mainContainer}>
<ul className={s.list}> <ul className={s.list}>
<li <li className={s.item} onClick={toggleSidebar}>
className={s.item}
onClick={(e) => (displaySidebar ? closeSidebar() : openSidebar())}
>
<Bag /> <Bag />
{itemsCount > 0 && <span className={s.bagCount}>{itemsCount}</span>} {itemsCount > 0 && <span className={s.bagCount}>{itemsCount}</span>}
</li> </li>
<Link href="/wishlist"> <Link href="/wishlist">
<li className={s.item}> <li className={s.item} onClick={closeSidebarIfPresent}>
<Heart /> <Heart />
</li> </li>
</Link> </Link>

View File

@ -131,6 +131,12 @@ export const UIProvider: FC = (props) => {
const openSidebar = () => dispatch({ type: 'OPEN_SIDEBAR' }) const openSidebar = () => dispatch({ type: 'OPEN_SIDEBAR' })
const closeSidebar = () => dispatch({ type: 'CLOSE_SIDEBAR' }) const closeSidebar = () => dispatch({ type: 'CLOSE_SIDEBAR' })
const toggleSidebar = () =>
state.displaySidebar
? dispatch({ type: 'CLOSE_SIDEBAR' })
: dispatch({ type: 'OPEN_SIDEBAR' })
const closeSidebarIfPresent = () =>
state.displaySidebar && dispatch({ type: 'CLOSE_SIDEBAR' })
const openDropdown = () => dispatch({ type: 'OPEN_DROPDOWN' }) const openDropdown = () => dispatch({ type: 'OPEN_DROPDOWN' })
const closeDropdown = () => dispatch({ type: 'CLOSE_DROPDOWN' }) const closeDropdown = () => dispatch({ type: 'CLOSE_DROPDOWN' })
@ -149,6 +155,8 @@ export const UIProvider: FC = (props) => {
...state, ...state,
openSidebar, openSidebar,
closeSidebar, closeSidebar,
toggleSidebar,
closeSidebarIfPresent,
openDropdown, openDropdown,
closeDropdown, closeDropdown,
openModal, openModal,