diff --git a/src/components/common/CartDrawer/CartDrawer.tsx b/src/components/common/CartDrawer/CartDrawer.tsx index 9410cfd9c..18a55677a 100644 --- a/src/components/common/CartDrawer/CartDrawer.tsx +++ b/src/components/common/CartDrawer/CartDrawer.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import { useCartDrawer } from 'src/components/contexts/CartDrawer/CartDrawerContext'; import { PRODUCT_CART_DATA_TEST } from 'src/utils/demo-data'; import { DrawerCommon } from '..'; import s from './CartDrawer.module.scss'; @@ -8,24 +9,24 @@ import CartRecommendation from './components/CartRecommendation/CartRecommendati import ProductsInCart from './components/ProductsInCart/ProductsInCart'; interface Props { - visible: boolean - onClose: () => void + } -const CartDrawer = ({ visible, onClose }: Props) => { +const CartDrawer = ({ }: Props) => { + const { cartVisible, closeCartDrawer } = useCartDrawer() return ( + visible={cartVisible} + onClose={closeCartDrawer}>
- +
- +
diff --git a/src/components/common/Header/Header.tsx b/src/components/common/Header/Header.tsx index d1d88adda..a0b220c08 100644 --- a/src/components/common/Header/Header.tsx +++ b/src/components/common/Header/Header.tsx @@ -19,15 +19,6 @@ const Header = memo(({ toggleFilter, visibleFilter }: props) => { const [isFullHeader, setIsFullHeader] = useState(true) const { visible: visibleModalAuthen, closeModal: closeModalAuthen, openModal: openModalAuthen } = useModalCommon({ initialValue: false }) const { visible: visibleModalInfo, closeModal: closeModalInfo, openModal: openModalInfo } = useModalCommon({ initialValue: false }) - const { visible: visibleCartDrawer, openModal: openCartDrawer, closeModal: closeCartDrawer } = useModalCommon({ initialValue: false }) - - const toggleCart = () => { - if (visibleCartDrawer) { - closeCartDrawer() - } else { - openCartDrawer() - } - } useEffect(() => { const handleScroll = () => { @@ -36,10 +27,6 @@ const Header = memo(({ toggleFilter, visibleFilter }: props) => { } else { setIsFullHeader(true) } - // if (!isMobile()) { - // } else { - // setIsFullHeader(true) - // } } window.addEventListener('scroll', handleScroll) return () => { @@ -56,7 +43,6 @@ const Header = memo(({ toggleFilter, visibleFilter }: props) => { @@ -68,7 +54,6 @@ const Header = memo(({ toggleFilter, visibleFilter }: props) => { isFull={isFullHeader} visibleFilter={visibleFilter} toggleFilter={toggleFilter} - toggleCart={toggleCart} openModalAuthen={openModalAuthen} openModalInfo={openModalInfo} /> @@ -78,9 +63,7 @@ const Header = memo(({ toggleFilter, visibleFilter }: props) => { - + ) }) diff --git a/src/components/common/Header/components/HeaderMenu/HeaderMenu.tsx b/src/components/common/Header/components/HeaderMenu/HeaderMenu.tsx index 74ebc1187..671055f1b 100644 --- a/src/components/common/Header/components/HeaderMenu/HeaderMenu.tsx +++ b/src/components/common/Header/components/HeaderMenu/HeaderMenu.tsx @@ -5,6 +5,7 @@ import { memo, useMemo } from 'react' import { ButtonCommon } from 'src/components/common' import InputSearch from 'src/components/common/InputSearch/InputSearch' import MenuDropdown from 'src/components/common/MenuDropdown/MenuDropdown' +import { useCartDrawer } from 'src/components/contexts/CartDrawer/CartDrawerContext' import { IconBuy, IconFilter, IconHeart, IconHistory, IconUser } from 'src/components/icons' import { ACCOUNT_TAB, FILTER_PAGE, QUERY_KEY, ROUTE } from 'src/utils/constanst.utils' import Logo from '../../../Logo/Logo' @@ -17,13 +18,12 @@ interface Props { openModalAuthen: () => void, openModalInfo: () => void, toggleFilter: () => void, - toggleCart: () => void, } -const HeaderMenu = memo(({ isFull, isStickyHeader, visibleFilter, openModalAuthen, openModalInfo, toggleFilter, toggleCart }: Props) => { +const HeaderMenu = memo(({ isFull, isStickyHeader, visibleFilter, openModalAuthen, openModalInfo, toggleFilter }: Props) => { const router = useRouter() - + const { toggleCartDrawer } = useCartDrawer() const optionMenu = useMemo(() => [ { @@ -74,7 +74,7 @@ const HeaderMenu = memo(({ isFull, isStickyHeader, visibleFilter, openModalAuthe ) } - @@ -108,7 +108,7 @@ const HeaderMenu = memo(({ isFull, isStickyHeader, visibleFilter, openModalAuthe
  • -
  • diff --git a/src/components/common/Layout/Layout.tsx b/src/components/common/Layout/Layout.tsx index 4070c8aa6..f2e898425 100644 --- a/src/components/common/Layout/Layout.tsx +++ b/src/components/common/Layout/Layout.tsx @@ -1,14 +1,8 @@ import { CommerceProvider } from '@framework' import { useRouter } from 'next/router' import { FC } from 'react' -import { useModalCommon } from 'src/components/hooks' -import { BRAND, CATEGORY, FEATURED, FILTER_PAGE, ROUTE } from 'src/utils/constanst.utils' -import { ScrollToTop } from '..' -import Footer from '../Footer/Footer' -import Header from '../Header/Header' -import MenuNavigationProductList from '../MenuNavigationProductList/MenuNavigationProductList' -import s from './Layout.module.scss' - +import { CartDrawerProvider } from 'src/components/contexts/CartDrawer/CartDrawerProvider' +import LayoutContent from './LayoutContent/LayoutContent' interface Props { className?: string children?: any @@ -16,37 +10,14 @@ interface Props { // note: demo code const Layout: FC = ({ children }) => { - const { locale = 'en-US', pathname } = useRouter() - const { visible: visibleFilter, openModal: openFilter, closeModal: closeFilter } = useModalCommon({ initialValue: false }) - - const router = useRouter() - - const toggleFilter = () => { - if (visibleFilter) { - closeFilter() - } else { - openFilter() - } - } - + const { locale = 'en-US' } = useRouter() return ( -
    -
    - { - router.pathname === ROUTE.ACCOUNT ? -
    -
    {children}
    -
    : -
    {children}
    - } -
    - - { - FILTER_PAGE.includes(pathname) && (
    ) - } -
    -
    + + + {children} + +
    ) diff --git a/src/components/common/Layout/Layout.module.scss b/src/components/common/Layout/LayoutContent/LayoutContent.module.scss similarity index 100% rename from src/components/common/Layout/Layout.module.scss rename to src/components/common/Layout/LayoutContent/LayoutContent.module.scss diff --git a/src/components/common/Layout/LayoutContent/LayoutContent.tsx b/src/components/common/Layout/LayoutContent/LayoutContent.tsx new file mode 100644 index 000000000..70c078f3a --- /dev/null +++ b/src/components/common/Layout/LayoutContent/LayoutContent.tsx @@ -0,0 +1,53 @@ +import { useRouter } from 'next/router' +import { FC } from 'react' +import { useCartDrawer } from 'src/components/contexts/CartDrawer/CartDrawerContext' +import { useModalCommon } from 'src/components/hooks' +import { BRAND, CATEGORY, FEATURED, FILTER_PAGE, ROUTE } from 'src/utils/constanst.utils' +import { CartDrawer, Footer, ScrollToTop } from '../..' +import Header from '../../Header/Header' +import MenuNavigationProductList from '../../MenuNavigationProductList/MenuNavigationProductList' +import s from './LayoutContent.module.scss' + +interface Props { + className?: string + children?: any +} + +const LayoutContent: FC = ({ children }) => { + const { pathname } = useRouter() + const { visible: visibleFilter, openModal: openFilter, closeModal: closeFilter } = useModalCommon({ initialValue: false }) + const router = useRouter() + + const toggleFilter = () => { + if (visibleFilter) { + closeFilter() + } else { + openFilter() + } + } + + return ( + <> +
    +
    + { + router.pathname === ROUTE.ACCOUNT ? +
    +
    {children}
    +
    : +
    {children}
    + } +
    + + { + FILTER_PAGE.includes(pathname) && (
    ) + } +
    +
    + + + + ) +} + +export default LayoutContent diff --git a/src/components/contexts/CartDrawer/CartDrawerContext.tsx b/src/components/contexts/CartDrawer/CartDrawerContext.tsx new file mode 100644 index 000000000..8a24be1be --- /dev/null +++ b/src/components/contexts/CartDrawer/CartDrawerContext.tsx @@ -0,0 +1,20 @@ +import { createContext, useContext } from 'react'; + +export type CartDrawerContextType = { + cartVisible: boolean; + toggleCartDrawer: (visible?: boolean) => void; + openCartDrawer: () => void; + closeCartDrawer: () => void; +}; +export const DEFAULT_CART_DRAWER_CONTEXT: CartDrawerContextType = { + cartVisible: false, + toggleCartDrawer: () => { }, + openCartDrawer: () => { }, + closeCartDrawer: () => { }, +}; + +export const CartDrawerContext = createContext(DEFAULT_CART_DRAWER_CONTEXT) + +export function useCartDrawer() { + return useContext(CartDrawerContext); +} diff --git a/src/components/contexts/CartDrawer/CartDrawerProvider.tsx b/src/components/contexts/CartDrawer/CartDrawerProvider.tsx new file mode 100644 index 000000000..3a08d57e7 --- /dev/null +++ b/src/components/contexts/CartDrawer/CartDrawerProvider.tsx @@ -0,0 +1,42 @@ +import { ReactNode, useEffect, useState } from "react"; +import { CartDrawerContext } from "./CartDrawerContext"; + +type Props = { + children: ReactNode; +}; + +export function CartDrawerProvider({ children }: Props) { + const [visible, setVisible] = useState(false); + + useEffect(() => { + const bodyElement = document.getElementsByTagName('body')[0] + if (bodyElement) { + if (visible) { + bodyElement.style.overflow = 'hidden' + } else { + bodyElement.style.overflow = 'auto' + bodyElement.removeAttribute('scroll') + } + } + }, [visible]) + + const closeCartDrawer = () => { + setVisible(false); + }; + + const openCartDrawer = () => { + setVisible(true); + }; + + const toggleCartDrawer = () => { + setVisible(!visible); + }; + + return ( + <> + + {children} + + + ); +} \ No newline at end of file