From b770f4b74629bee0febc3e42335b62ca2d681211 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Wed, 30 Sep 2020 13:38:39 -0300 Subject: [PATCH 1/2] Adding context --- components/core/Layout/Layout.tsx | 11 ++++++-- components/ui/context.tsx | 46 +++++++++++++++++++++++++++++++ pages/_app.tsx | 6 +++- 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 components/ui/context.tsx diff --git a/components/core/Layout/Layout.tsx b/components/core/Layout/Layout.tsx index 22113ad4f..0ea64d4bf 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/ui/context.tsx b/components/ui/context.tsx new file mode 100644 index 000000000..0ac416e4d --- /dev/null +++ b/components/ui/context.tsx @@ -0,0 +1,46 @@ +import React, { Context, FunctionComponent } from "react"; + +export interface ContextType { + displaySidebar: boolean; +} + +const initialState = { + displaySidebar: false, +}; + +function uiReducer(state, action) { + switch (action.type) { + case "OPEN_SIDEBAR": { + return { + ...state, + displaySidebar: true, + }; + } + case "CLOSE_SIDEBAR": { + return { + ...state, + displaySidebar: true, + }; + } + } +} + +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 ( <> - + + + ); } From 1af9a980df71f0756a3cddedb4b06cd02624fd8f Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Wed, 30 Sep 2020 15:56:32 -0300 Subject: [PATCH 2/2] Adding UI Context --- .../cart/CartSidebarView/CartSidebarView.tsx | 23 +++++-------------- components/core/Layout/Layout.tsx | 6 ++--- components/core/Navbar/Navbar.tsx | 7 +++++- components/icon/Cross.tsx | 16 +++++++++++++ components/icon/index.ts | 1 + components/ui/context.tsx | 15 ++++++------ 6 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 components/icon/Cross.tsx 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 0ea64d4bf..aa22d3d52 100644 --- a/components/core/Layout/Layout.tsx +++ b/components/core/Layout/Layout.tsx @@ -13,7 +13,7 @@ interface Props { const Layout: FunctionComponent = ({ className, children }) => { const rootClassName = cn(s.root, className); - // const { displaySidebar } = useUI(); + const { displaySidebar } = useUI(); return ( = ({ className, children }) => { />
{children}
- {/* {displaySidebar && ( + {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 index 0ac416e4d..9b1a1ede4 100644 --- a/components/ui/context.tsx +++ b/components/ui/context.tsx @@ -1,15 +1,16 @@ import React, { Context, FunctionComponent } from "react"; -export interface ContextType { - displaySidebar: boolean; -} - const initialState = { displaySidebar: false, + dispatch: null, }; +export interface UIState { + displaySidebar: boolean; + dispatch: (string) => void; +} function uiReducer(state, action) { - switch (action.type) { + switch (action) { case "OPEN_SIDEBAR": { return { ...state, @@ -19,13 +20,13 @@ function uiReducer(state, action) { case "CLOSE_SIDEBAR": { return { ...state, - displaySidebar: true, + displaySidebar: false, }; } } } -export const UIContext = React.createContext(initialState); +export const UIContext = React.createContext(initialState); UIContext.displayName = "UIContext"; export const UIProvider: FunctionComponent = (props) => {