diff --git a/components/cart/CartSidebarView/CartSidebarView.tsx b/components/cart/CartSidebarView/CartSidebarView.tsx index 11d24d234..f392a0890 100644 --- a/components/cart/CartSidebarView/CartSidebarView.tsx +++ b/components/cart/CartSidebarView/CartSidebarView.tsx @@ -1,11 +1,11 @@ import React, { FunctionComponent } from "react"; // import s from "./CartSidebarView.module.css"; import { Button } from "@components/ui"; -import { Trash } from "@components/icon"; +import { Trash, Cross } from "@components/icon"; +import { useUI } from "@components/ui/context"; -interface Props {} - -const CartSidebarView: FunctionComponent = () => { +const CartSidebarView: FunctionComponent = () => { + const { dispatch } = useUI(); return ( <>
@@ -17,22 +17,11 @@ const CartSidebarView: FunctionComponent = () => {
diff --git a/components/core/Layout/Layout.tsx b/components/core/Layout/Layout.tsx index 22113ad4f..aa22d3d52 100644 --- a/components/core/Layout/Layout.tsx +++ b/components/core/Layout/Layout.tsx @@ -4,6 +4,8 @@ import s from "./Layout.module.css"; import { Navbar, Featurebar } from "@components/core"; import { Container, Sidebar } from "@components/ui"; import { CartSidebarView } from "@components/cart"; +import { useUI } from "@components/ui/context"; + interface Props { className?: string; children?: any; @@ -11,6 +13,7 @@ interface Props { const Layout: FunctionComponent = ({ className, children }) => { const rootClassName = cn(s.root, className); + const { displaySidebar } = useUI(); return ( = ({ className, children }) => { />
{children}
- {/* - - */} + {displaySidebar && ( + + + + )}
); }; diff --git a/components/core/Navbar/Navbar.tsx b/components/core/Navbar/Navbar.tsx index e849205d3..5ac0c93bd 100644 --- a/components/core/Navbar/Navbar.tsx +++ b/components/core/Navbar/Navbar.tsx @@ -4,6 +4,7 @@ import s from "./Navbar.module.css"; import { Logo, Container } from "@components/ui"; import { Avatar, Searchbar } from "@components/core"; import { Heart, Bag } from "@components/icon"; +import { useUI } from "@components/ui/context"; interface Props { className?: string; children?: any; @@ -11,6 +12,10 @@ interface Props { const Navbar: FunctionComponent = ({ className }) => { const rootClassName = cn(s.root, className); + const { dispatch } = useUI(); + + const handleCartClick = () => dispatch("OPEN_SIDEBAR"); + return ( @@ -23,7 +28,7 @@ const Navbar: FunctionComponent = ({ className }) => { diff --git a/components/icon/Cross.tsx b/components/icon/Cross.tsx new file mode 100644 index 000000000..cff53612d --- /dev/null +++ b/components/icon/Cross.tsx @@ -0,0 +1,16 @@ +import React from "react"; + +const Cross = ({ ...props }) => { + return ( + + + + ); +}; + +export default Cross; diff --git a/components/icon/index.ts b/components/icon/index.ts index e354c1755..2dc7ced7c 100644 --- a/components/icon/index.ts +++ b/components/icon/index.ts @@ -1,3 +1,4 @@ export { default as Bag } from "./Bag"; export { default as Heart } from "./Heart"; export { default as Trash } from "./Trash"; +export { default as Cross } from "./Cross"; diff --git a/components/ui/context.tsx b/components/ui/context.tsx new file mode 100644 index 000000000..9b1a1ede4 --- /dev/null +++ b/components/ui/context.tsx @@ -0,0 +1,47 @@ +import React, { Context, FunctionComponent } from "react"; + +const initialState = { + displaySidebar: false, + dispatch: null, +}; +export interface UIState { + displaySidebar: boolean; + dispatch: (string) => void; +} + +function uiReducer(state, action) { + switch (action) { + case "OPEN_SIDEBAR": { + return { + ...state, + displaySidebar: true, + }; + } + case "CLOSE_SIDEBAR": { + return { + ...state, + displaySidebar: false, + }; + } + } +} + +export const UIContext = React.createContext(initialState); +UIContext.displayName = "UIContext"; + +export const UIProvider: FunctionComponent = (props) => { + const [state, dispatch] = React.useReducer(uiReducer, initialState); + const value = { + ...state, + dispatch, + }; + return ; +}; + +export const useUI = () => { + const context = React.useContext(UIContext); + if (context === undefined) { + throw new Error(`useUI must be used within a UIProvider`); + } + return context; +}; diff --git a/pages/_app.tsx b/pages/_app.tsx index 68a70e9de..bca14e53a 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -1,9 +1,13 @@ import "@assets/global.css"; +import React from "react"; +import { UIProvider } from "@components/ui/context"; export default function MyApp({ Component, pageProps }) { return ( <> - + + + ); }