Skip to content

Commit cde0bd5

Browse files
committed
feat: keep drawer state
keeps drawer state closes #277
1 parent 3531ee2 commit cde0bd5

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

src/actions/layout.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import { MAX_APP_HEIGHT, MIN_APP_HEIGHT } from '../config/layout';
22
import { RADIX } from '../config/constants';
3-
import { OPEN_TOOLS, CLOSE_TOOLS, SET_TOOLS_WIDTH } from '../types';
3+
import {
4+
OPEN_TOOLS,
5+
CLOSE_TOOLS,
6+
SET_TOOLS_WIDTH,
7+
SET_SIDE_BAR_IS_OPEN,
8+
} from '../types';
49

5-
const openTools = () => dispatch =>
10+
const openTools = () => (dispatch) =>
611
dispatch({
712
type: OPEN_TOOLS,
813
});
914

10-
const closeTools = () => dispatch =>
15+
const closeTools = () => (dispatch) =>
1116
dispatch({
1217
type: CLOSE_TOOLS,
1318
});
1419

15-
const setToolsWidth = ({ width }) => dispatch =>
20+
const setToolsWidth = ({ width }) => (dispatch) =>
1621
dispatch({
1722
type: SET_TOOLS_WIDTH,
1823
payload: width,
@@ -26,12 +31,25 @@ const setHeight = (id, height) => {
2631
localStorage.setItem(id, String(heightToSave));
2732
};
2833

29-
const getHeight = id => {
34+
const getHeight = (id) => {
3035
const height = localStorage.getItem(id);
3136
if (height) {
3237
return parseInt(height, RADIX);
3338
}
3439
return null;
3540
};
3641

37-
export { setHeight, getHeight, openTools, closeTools, setToolsWidth };
42+
const setSideBarIsOpen = (state) => (dispatch) =>
43+
dispatch({
44+
type: SET_SIDE_BAR_IS_OPEN,
45+
payload: state,
46+
});
47+
48+
export {
49+
setHeight,
50+
getHeight,
51+
openTools,
52+
closeTools,
53+
setToolsWidth,
54+
setSideBarIsOpen,
55+
};

src/components/common/Main.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ import { connect } from 'react-redux';
88
import Styles from '../../Styles';
99
import Header from './Header';
1010
import Sidebar from './Sidebar';
11+
import { setSideBarIsOpen } from '../../actions';
1112

1213
class Main extends Component {
13-
state = {
14-
open: false,
15-
};
16-
1714
static propTypes = {
1815
classes: PropTypes.shape({
1916
appBar: PropTypes.string.isRequired,
@@ -42,6 +39,8 @@ class Main extends Component {
4239
}),
4340
showSearch: PropTypes.bool,
4441
handleOnSearch: PropTypes.func,
42+
dispatchSetSideBarIsOpen: PropTypes.func.isRequired,
43+
open: PropTypes.bool,
4544
};
4645

4746
static defaultProps = {
@@ -50,14 +49,17 @@ class Main extends Component {
5049
handleOnSearch: () => {},
5150
id: null,
5251
style: {},
52+
open: false,
5353
};
5454

5555
handleDrawerOpen = () => {
56-
this.setState({ open: true });
56+
const { dispatchSetSideBarIsOpen } = this.props;
57+
dispatchSetSideBarIsOpen(true);
5758
};
5859

5960
handleDrawerClose = () => {
60-
this.setState({ open: false });
61+
const { dispatchSetSideBarIsOpen } = this.props;
62+
dispatchSetSideBarIsOpen(false);
6163
};
6264

6365
render() {
@@ -69,8 +71,8 @@ class Main extends Component {
6971
style,
7072
showSearch,
7173
handleOnSearch,
74+
open,
7275
} = this.props;
73-
const { open } = this.state;
7476

7577
return (
7678
<div className={classes.root} style={style}>
@@ -102,11 +104,14 @@ class Main extends Component {
102104
}
103105
}
104106

105-
const mapStateToProps = ({ authentication }) => ({
107+
const mapStateToProps = ({ authentication, layout }) => ({
106108
activity: Boolean(authentication.getIn(['current', 'activity']).size),
109+
open: layout.getIn(['sideBarState', 'open']),
107110
});
108111

109-
const mapDispatchToProps = {};
112+
const mapDispatchToProps = {
113+
dispatchSetSideBarIsOpen: setSideBarIsOpen,
114+
};
110115

111116
const ConnectedComponent = connect(mapStateToProps, mapDispatchToProps)(Main);
112117

src/reducers/layout.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { Map } from 'immutable';
2-
import { OPEN_TOOLS, CLOSE_TOOLS, SET_TOOLS_WIDTH } from '../types';
2+
import {
3+
OPEN_TOOLS,
4+
CLOSE_TOOLS,
5+
SET_TOOLS_WIDTH,
6+
SET_SIDE_BAR_IS_OPEN,
7+
} from '../types';
38
import { DEFAULT_TOOLS_WIDTH } from '../config/layout';
49

510
const INITIAL_STATE = Map({
@@ -17,6 +22,8 @@ export default (state = INITIAL_STATE, { type, payload }) => {
1722
return state.setIn(['tools', 'open'], false);
1823
case SET_TOOLS_WIDTH:
1924
return state.setIn(['tools', 'width'], payload);
25+
case SET_SIDE_BAR_IS_OPEN:
26+
return state.setIn(['sideBarState', 'open'], payload);
2027
default:
2128
return state;
2229
}

src/types/layout.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
export const OPEN_TOOLS = 'OPEN_TOOLS';
33
export const CLOSE_TOOLS = 'CLOSE_TOOLS';
44
export const SET_TOOLS_WIDTH = 'SET_TOOLS_WIDTH';
5+
export const SET_SIDE_BAR_IS_OPEN = 'SET_SIDE_BAR_IS_OPEN';

0 commit comments

Comments
 (0)