Bind redux state, actions to high-level "App" Container.

Refactor/remove unneeded boilerplate redux state/dispatch.
This commit is contained in:
Daniel Ternyak 2017-04-14 01:22:53 -05:00
parent bfbd3b2319
commit 212455cfd2

View File

@ -1,11 +1,18 @@
import React, {Component} from 'react' import React, {Component} from 'react'
import {connect} from 'react-redux' import {connect} from 'react-redux'
import {Dimmer, Sidebar as SidebarSemantic, Container} from 'semantic-ui-react' import {Header, Footer} from 'components'
import {Header, Sidebar, Footer} from 'components'
import {CLOSE_SIDEBAR, OPEN_SIDEBAR, WINDOW_RESIZE} from 'actions/layout'
import {LOGOUT_AUTH} from 'actions/auth'
import {push} from 'react-router-redux' import {push} from 'react-router-redux'
import {sidebarRouting} from 'routing' import {sidebarRouting} from 'routing'
import PropTypes from 'prop-types';
import {Container} from 'semantic-ui-react'
import {
CHANGE_LANGUAGE,
TOGGLE_LANGUAGE_DROPDOWN,
CHANGE_NODE,
TOGGLE_NODE_DROPDOWN
} from 'actions/config'
class App extends Component { class App extends Component {
constructor(props) { constructor(props) {
@ -13,79 +20,72 @@ class App extends Component {
} }
static propTypes = { static propTypes = {
children: React.PropTypes.node.isRequired, children: PropTypes.node.isRequired,
location: React.PropTypes.object, location: PropTypes.object,
sidebarOpened: React.PropTypes.bool, sidebarOpened: PropTypes.bool,
closeSidebar: React.PropTypes.func, closeSidebar: PropTypes.func,
isLoggedIn: React.PropTypes.bool, isLoggedIn: PropTypes.bool,
handleWindowResize: React.PropTypes.func, handleWindowResize: PropTypes.func,
logout: React.PropTypes.func, logout: PropTypes.func,
checkAuthLogic: React.PropTypes.func, checkAuthLogic: PropTypes.func,
toggleSidebar: React.PropTypes.func, toggleSidebar: PropTypes.func,
onHeaderRightButtonClick: React.PropTypes.func, onHeaderRightButtonClick: PropTypes.func,
router: React.PropTypes.object, router: PropTypes.object,
isMobile: React.PropTypes.bool isMobile: PropTypes.bool,
// BEGIN ACTUAL
languageSelection: PropTypes.number,
languageToggle: PropTypes.bool,
changeLanguage: PropTypes.func,
toggleLanguageDropdown: PropTypes.func,
changeNode: PropTypes.func,
toggleNodeDropdown: PropTypes.func,
nodeSelection: PropTypes.number,
nodeToggle: PropTypes.bool,
} }
componentWillMount() { componentWillMount() {
let {handleWindowResize, isLoggedIn} = this.props let {handleWindowResize, isLoggedIn} = this.props
window.addEventListener('resize', handleWindowResize) window.addEventListener('resize', handleWindowResize)
this.checkAppAuthLogic(isLoggedIn)
} }
/**
* Call checkAuthLogic
* @param {Bool} loggedIn state.auth.loggedIn, current prop
* @return {Bool} Nothing
*/
checkAppAuthLogic(loggedIn) {
let {router, checkAuthLogic} = this.props
let path = router.getCurrentLocation().pathname
checkAuthLogic(path, loggedIn)
return false
}
componentWillReceiveProps(nextProps) {
this.checkAppAuthLogic(nextProps.isLoggedIn)
}
render() { render() {
let { let {
children, children,
sidebarOpened,
closeSidebar,
isLoggedIn,
logout,
onHeaderRightButtonClick,
toggleSidebar,
isMobile
} = this.props
let title = children.props.route.name // ACTUAL
languageSelection,
changeLanguage,
languageToggle,
toggleLanguageDropdown,
changeNode,
toggleNodeDropdown,
nodeSelection,
nodeToggle,
} = this.props;
let title = children.props.route.name;
let sidebarProps = {
isMobile,
logout,
open: sidebarOpened,
routing: sidebarRouting
}
let headerProps = { let headerProps = {
toggleSidebar,
title, title,
isLoggedIn,
onHeaderRightButtonClick
}
let dimmerProps = { changeLanguage,
active: sidebarOpened, toggleLanguageDropdown,
onClick: closeSidebar languageSelection,
languageToggle,
changeNode,
toggleNodeDropdown,
nodeSelection,
nodeToggle,
} }
return ( return (
<div className="page-layout"> <div className="page-layout">
<main> <main>
<Header {...headerProps}/> <Header {...headerProps}/>
<div className="main-content"> <div className="main-content">
@ -103,46 +103,26 @@ class App extends Component {
function mapStateToProps(state) { function mapStateToProps(state) {
return { return {
sidebarOpened: state.layout.sidebarOpened, nodeSelection: state.config.nodeSelection,
isMobile: state.layout.isMobile, nodeToggle: state.config.nodeToggle,
isLoggedIn: state.auth.loggedIn languageSelection: state.config.languageSelection,
languageToggle: state.config.languageToggle
} }
} }
function mapDispatchToProps(dispatch) { function mapDispatchToProps(dispatch) {
let resizer
return { return {
closeSidebar: () => { changeNode: (i) => {
dispatch(CLOSE_SIDEBAR()) dispatch(CHANGE_NODE(i))
}, },
logout: () => { toggleNodeDropdown: () => {
dispatch(LOGOUT_AUTH()) dispatch(TOGGLE_NODE_DROPDOWN())
dispatch(push('/auth'))
}, },
toggleSidebar: () => { changeLanguage: (i) => {
dispatch(OPEN_SIDEBAR()) dispatch(CHANGE_LANGUAGE(i))
}, },
onHeaderRightButtonClick: () => { toggleLanguageDropdown: () => {
}, dispatch(TOGGLE_LANGUAGE_DROPDOWN())
/**
* Immediately push to homePath('/'), if user is logged.
* Can be used for other auth logic checks.
* Useful, because we don't need to dispatch `push(homePath)` action
* from `Login` container after LOGIN_AUTH_SUCCESS action
* @param {String} path [current location path]
* @param {Boolean} isLoggedIn [is user logged in?]
* @return {[type]} [description]
*/
checkAuthLogic: (path, isLoggedIn) => {
let authPath = '/auth'
let homePath = '/'
if (isLoggedIn && path === authPath) {
dispatch(push(homePath))
}
},
handleWindowResize: () => {
clearTimeout(resizer)
resizer = setTimeout((() => dispatch(WINDOW_RESIZE())), 100)
} }
} }
} }