mirror of
https://github.com/vercel/commerce.git
synced 2025-06-29 18:01:21 +00:00
Removing Providers from Layout
This commit is contained in:
parent
d6bbd13b19
commit
6184eb04c8
@ -1,10 +1,12 @@
|
|||||||
import { useContext } from 'react'
|
import { useContext } from 'react'
|
||||||
import { CommerceContext } from '../context'
|
import { CommerceContext } from '../context'
|
||||||
|
|
||||||
export const useCommerce = () => {
|
const useCommerce = () => {
|
||||||
const context = useContext(CommerceContext)
|
const context = useContext(CommerceContext)
|
||||||
if (context === undefined) {
|
if (context === undefined) {
|
||||||
throw new Error(`useCommerce must be used within a CommerceProvider`)
|
throw new Error(`useCommerce must be used within a CommerceProvider`)
|
||||||
}
|
}
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default useCommerce
|
||||||
|
@ -1 +1,3 @@
|
|||||||
export { default as useCart } from './hooks/useCart'
|
export { default as useCart } from './hooks/useCart'
|
||||||
|
export { default as useCommerce } from './hooks/useCommerce'
|
||||||
|
export * from './context'
|
||||||
|
@ -1,16 +1,15 @@
|
|||||||
import cn from 'classnames'
|
import cn from 'classnames'
|
||||||
import s from './Layout.module.css'
|
import s from './Layout.module.css'
|
||||||
import { useRouter } from 'next/router'
|
|
||||||
import { usePreventScroll } from '@react-aria/overlays'
|
import { usePreventScroll } from '@react-aria/overlays'
|
||||||
import { FC, useCallback, useEffect, useState } from 'react'
|
import { FC, useCallback, useEffect, useState } from 'react'
|
||||||
import { useUI } from '@components/ui/context'
|
import { useUI } from '@components/ui/context'
|
||||||
import { CartSidebarView } from '@components/cart'
|
import { CartSidebarView } from '@components/cart'
|
||||||
import { Navbar, Featurebar, Footer } from '@components/common'
|
import { Navbar, Featurebar, Footer } from '@components/common'
|
||||||
import { LoginView, SignUpView, ForgotPassword } from '@components/auth'
|
import { LoginView, SignUpView, ForgotPassword } from '@components/auth'
|
||||||
import { Container, Sidebar, Button, Modal, Toast } from '@components/ui'
|
import { Container, Sidebar, Button, Modal } from '@components/ui'
|
||||||
import debounce from 'lodash.debounce'
|
import debounce from 'lodash.debounce'
|
||||||
|
|
||||||
import { CommerceProvider } from '@bigcommerce/storefront-data-hooks'
|
|
||||||
import type { Page } from '@bigcommerce/storefront-data-hooks/api/operations/get-all-pages'
|
import type { Page } from '@bigcommerce/storefront-data-hooks/api/operations/get-all-pages'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@ -29,7 +28,6 @@ const Layout: FC<Props> = ({ children, pageProps }) => {
|
|||||||
} = useUI()
|
} = useUI()
|
||||||
const [acceptedCookies, setAcceptedCookies] = useState(false)
|
const [acceptedCookies, setAcceptedCookies] = useState(false)
|
||||||
const [hasScrolled, setHasScrolled] = useState(false)
|
const [hasScrolled, setHasScrolled] = useState(false)
|
||||||
const { locale = 'en-US' } = useRouter()
|
|
||||||
|
|
||||||
usePreventScroll({
|
usePreventScroll({
|
||||||
isDisabled: !(displaySidebar || displayModal),
|
isDisabled: !(displaySidebar || displayModal),
|
||||||
@ -52,38 +50,36 @@ const Layout: FC<Props> = ({ children, pageProps }) => {
|
|||||||
}, [handleScroll])
|
}, [handleScroll])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CommerceProvider locale={locale}>
|
<div className={cn(s.root)}>
|
||||||
<div className={cn(s.root)}>
|
<header className={cn(s.header, { 'shadow-magical': hasScrolled })}>
|
||||||
<header className={cn(s.header, { 'shadow-magical': hasScrolled })}>
|
<Container>
|
||||||
<Container>
|
<Navbar />
|
||||||
<Navbar />
|
</Container>
|
||||||
</Container>
|
</header>
|
||||||
</header>
|
<main className="fit">{children}</main>
|
||||||
<main className="fit">{children}</main>
|
<Footer pages={pageProps.pages} />
|
||||||
<Footer pages={pageProps.pages} />
|
|
||||||
|
|
||||||
{/** Aditional UI Components */}
|
{/** Aditional UI Components */}
|
||||||
<Sidebar open={displaySidebar} onClose={closeSidebar}>
|
<Sidebar open={displaySidebar} onClose={closeSidebar}>
|
||||||
<CartSidebarView />
|
<CartSidebarView />
|
||||||
</Sidebar>
|
</Sidebar>
|
||||||
|
|
||||||
<Modal open={displayModal} onClose={closeModal}>
|
<Modal open={displayModal} onClose={closeModal}>
|
||||||
{modalView === 'LOGIN_VIEW' && <LoginView />}
|
{modalView === 'LOGIN_VIEW' && <LoginView />}
|
||||||
{modalView === 'SIGNUP_VIEW' && <SignUpView />}
|
{modalView === 'SIGNUP_VIEW' && <SignUpView />}
|
||||||
{modalView === 'FORGOT_VIEW' && <ForgotPassword />}
|
{modalView === 'FORGOT_VIEW' && <ForgotPassword />}
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
<Featurebar
|
<Featurebar
|
||||||
title="This site uses cookies to improve your experience. By clicking, you agree to our Privacy Policy."
|
title="This site uses cookies to improve your experience. By clicking, you agree to our Privacy Policy."
|
||||||
hide={acceptedCookies}
|
hide={acceptedCookies}
|
||||||
action={
|
action={
|
||||||
<Button className="mx-5" onClick={() => setAcceptedCookies(true)}>
|
<Button className="mx-5" onClick={() => setAcceptedCookies(true)}>
|
||||||
Accept cookies
|
Accept cookies
|
||||||
</Button>
|
</Button>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</CommerceProvider>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,22 +3,26 @@ import 'keen-slider/keen-slider.min.css'
|
|||||||
|
|
||||||
import { FC } from 'react'
|
import { FC } from 'react'
|
||||||
import type { AppProps } from 'next/app'
|
import type { AppProps } from 'next/app'
|
||||||
|
|
||||||
import { ManagedUIContext } from '@components/ui/context'
|
|
||||||
import { Head } from '@components/common'
|
import { Head } from '@components/common'
|
||||||
|
import { ManagedUIContext } from '@components/ui/context'
|
||||||
|
|
||||||
|
import { useRouter } from 'next/router'
|
||||||
|
import { CommerceProvider } from '@bigcommerce/storefront-data-hooks'
|
||||||
|
|
||||||
const Noop: FC = ({ children }) => <>{children}</>
|
const Noop: FC = ({ children }) => <>{children}</>
|
||||||
|
|
||||||
export default function MyApp({ Component, pageProps }: AppProps) {
|
export default function MyApp({ Component, pageProps }: AppProps) {
|
||||||
const Layout = (Component as any).Layout || Noop
|
const Layout = (Component as any).Layout || Noop
|
||||||
|
const { locale = 'en-US' } = useRouter()
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Head />
|
<Head />
|
||||||
<ManagedUIContext>
|
<ManagedUIContext>
|
||||||
<Layout pageProps={pageProps}>
|
<CommerceProvider locale={locale}>
|
||||||
<Component {...pageProps} />
|
<Layout pageProps={pageProps}>
|
||||||
</Layout>
|
<Component {...pageProps} />
|
||||||
|
</Layout>
|
||||||
|
</CommerceProvider>
|
||||||
</ManagedUIContext>
|
</ManagedUIContext>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user