Use redux for drawer open state

This commit is contained in:
Ken Hibino 2021-01-22 22:26:04 -08:00
parent 3547d27214
commit 1eafcbeed5
3 changed files with 34 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import React, { useState } from "react"; import React from "react";
import { connect, ConnectedProps } from "react-redux"; import { connect, ConnectedProps } from "react-redux";
import clsx from "clsx"; import clsx from "clsx";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
@ -28,6 +28,7 @@ import { AppState } from "./store";
import { paths } from "./paths"; import { paths } from "./paths";
import { useTheme } from "./theme"; import { useTheme } from "./theme";
import { closeSnackbar } from "./actions/snackbarActions"; import { closeSnackbar } from "./actions/snackbarActions";
import { toggleDrawer } from "./actions/settingsActions";
import ListItemLink from "./components/ListItemLink"; import ListItemLink from "./components/ListItemLink";
import SchedulersView from "./views/SchedulersView"; import SchedulersView from "./views/SchedulersView";
import DashboardView from "./views/DashboardView"; import DashboardView from "./views/DashboardView";
@ -135,11 +136,13 @@ function mapStateToProps(state: AppState) {
return { return {
snackbar: state.snackbar, snackbar: state.snackbar,
themePreference: state.settings.themePreference, themePreference: state.settings.themePreference,
isDrawerOpen: state.settings.isDrawerOpen,
}; };
} }
const mapDispatchToProps = { const mapDispatchToProps = {
closeSnackbar, closeSnackbar,
toggleDrawer,
}; };
const connector = connect(mapStateToProps, mapDispatchToProps); const connector = connect(mapStateToProps, mapDispatchToProps);
@ -151,8 +154,6 @@ function SlideUpTransition(props: TransitionProps) {
function App(props: ConnectedProps<typeof connector>) { function App(props: ConnectedProps<typeof connector>) {
const theme = useTheme(props.themePreference); const theme = useTheme(props.themePreference);
const classes = useStyles(theme)(); const classes = useStyles(theme)();
const [open, setOpen] = useState(true);
const toggleDrawer = () => setOpen(!open);
return ( return (
<ThemeProvider theme={theme}> <ThemeProvider theme={theme}>
<Router> <Router>
@ -167,7 +168,7 @@ function App(props: ConnectedProps<typeof connector>) {
edge="start" edge="start"
color="inherit" color="inherit"
aria-label="open drawer" aria-label="open drawer"
onClick={toggleDrawer} onClick={props.toggleDrawer}
className={classes.menuButton} className={classes.menuButton}
> >
<MenuIcon /> <MenuIcon />
@ -189,10 +190,10 @@ function App(props: ConnectedProps<typeof connector>) {
classes={{ classes={{
paper: clsx( paper: clsx(
classes.drawerPaper, classes.drawerPaper,
!open && classes.drawerPaperClose !props.isDrawerOpen && classes.drawerPaperClose
), ),
}} }}
open={open} open={props.isDrawerOpen}
> >
<Snackbar <Snackbar
anchorOrigin={{ vertical: "bottom", horizontal: "left" }} anchorOrigin={{ vertical: "bottom", horizontal: "left" }}

View File

@ -2,21 +2,27 @@ import { ThemePreference } from "../reducers/settingsReducer";
// List of settings related action types. // List of settings related action types.
export const POLL_INTERVAL_CHANGE = "POLL_INTERVAL_CHANGE"; export const POLL_INTERVAL_CHANGE = "POLL_INTERVAL_CHANGE";
export const THEME_PREFERENCE_CHANGE = "THEME_PREFERENCE_CHANGE"; export const THEME_PREFERENCE_CHANGE = "THEME_PREFERENCE_CHANGE";
export const TOGGLE_DRAWER = "TOGGLE_DRAWER";
interface PollIntervalChangeAction { interface PollIntervalChangeAction {
type: typeof POLL_INTERVAL_CHANGE; type: typeof POLL_INTERVAL_CHANGE;
value: number; // new poll interval value in seconds value: number; // new poll interval value in seconds
} }
interface ToggleDarkThemeAction { interface ThemePreferenceChangeAction {
type: typeof THEME_PREFERENCE_CHANGE; type: typeof THEME_PREFERENCE_CHANGE;
value: ThemePreference; value: ThemePreference;
} }
interface ToggleDrawerAction {
type: typeof TOGGLE_DRAWER;
}
// Union of all settings related action types. // Union of all settings related action types.
export type SettingsActionTypes = export type SettingsActionTypes =
| PollIntervalChangeAction | PollIntervalChangeAction
| ToggleDarkThemeAction; | ThemePreferenceChangeAction
| ToggleDrawerAction;
export function pollIntervalChange(value: number) { export function pollIntervalChange(value: number) {
return { return {
@ -31,3 +37,7 @@ export function selectTheme(value: ThemePreference) {
value, value,
}; };
} }
export function toggleDrawer() {
return { type: TOGGLE_DRAWER };
}

View File

@ -2,6 +2,7 @@ import {
POLL_INTERVAL_CHANGE, POLL_INTERVAL_CHANGE,
SettingsActionTypes, SettingsActionTypes,
THEME_PREFERENCE_CHANGE, THEME_PREFERENCE_CHANGE,
TOGGLE_DRAWER,
} from "../actions/settingsActions"; } from "../actions/settingsActions";
export enum ThemePreference { export enum ThemePreference {
@ -13,11 +14,13 @@ export enum ThemePreference {
export interface SettingsState { export interface SettingsState {
pollInterval: number; pollInterval: number;
themePreference: ThemePreference; themePreference: ThemePreference;
isDrawerOpen: boolean;
} }
const initialState: SettingsState = { const initialState: SettingsState = {
pollInterval: 8, pollInterval: 8,
themePreference: ThemePreference.SystemDefault, themePreference: ThemePreference.SystemDefault,
isDrawerOpen: true,
}; };
function settingsReducer( function settingsReducer(
@ -26,12 +29,23 @@ function settingsReducer(
): SettingsState { ): SettingsState {
switch (action.type) { switch (action.type) {
case POLL_INTERVAL_CHANGE: case POLL_INTERVAL_CHANGE:
return { ...state, pollInterval: action.value }; return {
...state,
pollInterval: action.value,
};
case THEME_PREFERENCE_CHANGE: case THEME_PREFERENCE_CHANGE:
return { return {
...state, ...state,
themePreference: action.value, themePreference: action.value,
}; };
case TOGGLE_DRAWER:
return {
...state,
isDrawerOpen: !state.isDrawerOpen,
};
default: default:
return state; return state;
} }