Skip to content

Commit

Permalink
feat: keep drawer state
Browse files Browse the repository at this point in the history
keeps drawer state

closes #277
  • Loading branch information
hasanagh committed Nov 6, 2020
1 parent 3531ee2 commit cde0bd5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
30 changes: 24 additions & 6 deletions src/actions/layout.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { MAX_APP_HEIGHT, MIN_APP_HEIGHT } from '../config/layout';
import { RADIX } from '../config/constants';
import { OPEN_TOOLS, CLOSE_TOOLS, SET_TOOLS_WIDTH } from '../types';
import {
OPEN_TOOLS,
CLOSE_TOOLS,
SET_TOOLS_WIDTH,
SET_SIDE_BAR_IS_OPEN,
} from '../types';

const openTools = () => dispatch =>
const openTools = () => (dispatch) =>
dispatch({
type: OPEN_TOOLS,
});

const closeTools = () => dispatch =>
const closeTools = () => (dispatch) =>
dispatch({
type: CLOSE_TOOLS,
});

const setToolsWidth = ({ width }) => dispatch =>
const setToolsWidth = ({ width }) => (dispatch) =>
dispatch({
type: SET_TOOLS_WIDTH,
payload: width,
Expand All @@ -26,12 +31,25 @@ const setHeight = (id, height) => {
localStorage.setItem(id, String(heightToSave));
};

const getHeight = id => {
const getHeight = (id) => {
const height = localStorage.getItem(id);
if (height) {
return parseInt(height, RADIX);
}
return null;
};

export { setHeight, getHeight, openTools, closeTools, setToolsWidth };
const setSideBarIsOpen = (state) => (dispatch) =>
dispatch({
type: SET_SIDE_BAR_IS_OPEN,
payload: state,
});

export {
setHeight,
getHeight,
openTools,
closeTools,
setToolsWidth,
setSideBarIsOpen,
};
23 changes: 14 additions & 9 deletions src/components/common/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ import { connect } from 'react-redux';
import Styles from '../../Styles';
import Header from './Header';
import Sidebar from './Sidebar';
import { setSideBarIsOpen } from '../../actions';

class Main extends Component {
state = {
open: false,
};

static propTypes = {
classes: PropTypes.shape({
appBar: PropTypes.string.isRequired,
Expand Down Expand Up @@ -42,6 +39,8 @@ class Main extends Component {
}),
showSearch: PropTypes.bool,
handleOnSearch: PropTypes.func,
dispatchSetSideBarIsOpen: PropTypes.func.isRequired,
open: PropTypes.bool,
};

static defaultProps = {
Expand All @@ -50,14 +49,17 @@ class Main extends Component {
handleOnSearch: () => {},
id: null,
style: {},
open: false,
};

handleDrawerOpen = () => {
this.setState({ open: true });
const { dispatchSetSideBarIsOpen } = this.props;
dispatchSetSideBarIsOpen(true);
};

handleDrawerClose = () => {
this.setState({ open: false });
const { dispatchSetSideBarIsOpen } = this.props;
dispatchSetSideBarIsOpen(false);
};

render() {
Expand All @@ -69,8 +71,8 @@ class Main extends Component {
style,
showSearch,
handleOnSearch,
open,
} = this.props;
const { open } = this.state;

return (
<div className={classes.root} style={style}>
Expand Down Expand Up @@ -102,11 +104,14 @@ class Main extends Component {
}
}

const mapStateToProps = ({ authentication }) => ({
const mapStateToProps = ({ authentication, layout }) => ({
activity: Boolean(authentication.getIn(['current', 'activity']).size),
open: layout.getIn(['sideBarState', 'open']),
});

const mapDispatchToProps = {};
const mapDispatchToProps = {
dispatchSetSideBarIsOpen: setSideBarIsOpen,
};

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

Expand Down
9 changes: 8 additions & 1 deletion src/reducers/layout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Map } from 'immutable';
import { OPEN_TOOLS, CLOSE_TOOLS, SET_TOOLS_WIDTH } from '../types';
import {
OPEN_TOOLS,
CLOSE_TOOLS,
SET_TOOLS_WIDTH,
SET_SIDE_BAR_IS_OPEN,
} from '../types';
import { DEFAULT_TOOLS_WIDTH } from '../config/layout';

const INITIAL_STATE = Map({
Expand All @@ -17,6 +22,8 @@ export default (state = INITIAL_STATE, { type, payload }) => {
return state.setIn(['tools', 'open'], false);
case SET_TOOLS_WIDTH:
return state.setIn(['tools', 'width'], payload);
case SET_SIDE_BAR_IS_OPEN:
return state.setIn(['sideBarState', 'open'], payload);
default:
return state;
}
Expand Down
1 change: 1 addition & 0 deletions src/types/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
export const OPEN_TOOLS = 'OPEN_TOOLS';
export const CLOSE_TOOLS = 'CLOSE_TOOLS';
export const SET_TOOLS_WIDTH = 'SET_TOOLS_WIDTH';
export const SET_SIDE_BAR_IS_OPEN = 'SET_SIDE_BAR_IS_OPEN';

0 comments on commit cde0bd5

Please sign in to comment.