From dd36a0e65abe3e49d85a6df89e239e7b06bdee74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ey=C3=BE=C3=B3r=20Magn=C3=BAsson?= Date: Tue, 26 Mar 2019 11:49:00 +0100 Subject: [PATCH] refactor: add ui state provider --- dashboard/src/app.tsx | 91 +++++++++++++++++----------- dashboard/src/containers/sidebar.tsx | 8 ++- dashboard/src/context/ui.tsx | 48 +++++++++++++++ 3 files changed, 109 insertions(+), 38 deletions(-) create mode 100644 dashboard/src/context/ui.tsx diff --git a/dashboard/src/app.tsx b/dashboard/src/app.tsx index 4f3cb18c52..e9056550bd 100644 --- a/dashboard/src/app.tsx +++ b/dashboard/src/app.tsx @@ -8,7 +8,7 @@ import cls from "classnames" import { css } from "emotion/macro" -import React from "react" +import React, { useContext } from "react" import styled from "@emotion/styled/macro" import { Route } from "react-router-dom" @@ -27,6 +27,7 @@ import { DataProvider } from "./context/data" import { NavLink } from "./components/links" import logo from "./assets/logo.png" +import { UiStateProvider, UiStateContext } from "./context/ui" // Style and align properly const Logo = styled.img` @@ -41,44 +42,60 @@ const SidebarWrapper = styled.div` height: 100vh; ` -const App = () => ( -
- - -
{ + return ( +
+ + + + + + + +
+ ) + +} + +const App = () => { + const { state: { isSidebarOpen }, actions: { toggleSidebar } } = useContext(UiStateContext) + + console.log(isSidebarOpen) + + return ( +
+ + +
+ + + +
+ +
+
- -
- - - -
- -
-
-
- - - - -
-
+ `, "p-2")}> + + + +
- - -
-) +
+
+ ) +} -export default App +export default AppContainer diff --git a/dashboard/src/containers/sidebar.tsx b/dashboard/src/containers/sidebar.tsx index 1b99b99764..0c60b50fc2 100644 --- a/dashboard/src/containers/sidebar.tsx +++ b/dashboard/src/containers/sidebar.tsx @@ -43,7 +43,11 @@ const builtinPages: Page[] = [ }, ] -export default () => { +interface SidebarProps { + isOpen: boolean +} + +const SidebarContainer: React.SFC = () => { const { actions: { loadStatus }, store: { status }, @@ -72,3 +76,5 @@ export default () => { return } + +export default SidebarContainer diff --git a/dashboard/src/context/ui.tsx b/dashboard/src/context/ui.tsx new file mode 100644 index 0000000000..404af649fd --- /dev/null +++ b/dashboard/src/context/ui.tsx @@ -0,0 +1,48 @@ +import React, { useState } from "react"; + +interface UiState { + isSidebarOpen: boolean +} + +interface UiActions { + toggleSidebar: () => void +} + +const INITIAL_UI_STATE: UiState = { + isSidebarOpen: false, +} + +interface UiStateAndActions { + state: UiState, + actions: UiActions, +} + +export const UiStateContext = React.createContext(null) + +const useUiState = () => { + const [uiState, setState] = useState(INITIAL_UI_STATE) + + const toggleSidebar = () => { + setState({ + ...uiState, + isSidebarOpen: !uiState.isSidebarOpen, + }) + } + + return { + state: uiState, + actions: { + toggleSidebar, + }, + } +} + +export const UiStateProvider: React.SFC = ({ children }) => { + const storeAndActions = useUiState() + + return ( + + {children} + + ) +}