fleet/frontend/layouts/CoreLayout/CoreLayout.jsx

160 lines
3.7 KiB
React
Raw Normal View History

import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import LoadingBar from 'react-redux-loading-bar';
import { logoutUser } from 'redux/nodes/auth/actions';
import { push } from 'react-router-redux';
2017-02-24 23:01:43 +00:00
import classnames from 'classnames';
2016-12-01 22:14:39 +00:00
import configInterface from 'interfaces/config';
import FlashMessage from 'components/flash_messages/FlashMessage';
import PersistentFlash from 'components/flash_messages/PersistentFlash';
2016-12-01 22:14:39 +00:00
import SiteNavHeader from 'components/side_panels/SiteNavHeader';
import SiteNavSidePanel from 'components/side_panels/SiteNavSidePanel';
import userInterface from 'interfaces/user';
2017-01-05 18:08:19 +00:00
import notificationInterface from 'interfaces/notification';
import { hideFlash } from 'redux/nodes/notifications/actions';
export class CoreLayout extends Component {
static propTypes = {
children: PropTypes.node,
config: configInterface,
dispatch: PropTypes.func,
user: userInterface,
2017-01-05 18:08:19 +00:00
fullWidthFlash: PropTypes.bool,
2017-02-24 23:01:43 +00:00
isSmallNav: PropTypes.bool,
2017-01-05 18:08:19 +00:00
notifications: notificationInterface,
persistentFlash: PropTypes.shape({
showFlash: PropTypes.bool.isRequired,
message: PropTypes.string.isRequired,
}).isRequired,
};
onLogoutUser = () => {
const { dispatch } = this.props;
dispatch(logoutUser());
return false;
}
onNavItemClick = (path) => {
return (evt) => {
evt.preventDefault();
const { dispatch } = this.props;
2017-01-18 14:40:28 +00:00
if (path.indexOf('http') !== -1) {
global.window.open(path, '_blank');
return false;
}
dispatch(push(path));
return false;
};
}
2017-01-05 18:08:19 +00:00
onRemoveFlash = () => {
const { dispatch } = this.props;
dispatch(hideFlash);
return false;
}
onUndoActionClick = (undoAction) => {
return (evt) => {
evt.preventDefault();
const { dispatch } = this.props;
const { onRemoveFlash } = this;
dispatch(undoAction);
return onRemoveFlash();
};
}
render () {
2017-02-24 23:01:43 +00:00
const {
fullWidthFlash,
notifications,
children,
config,
persistentFlash,
user,
isSmallNav,
} = this.props;
2017-01-05 18:08:19 +00:00
const { onRemoveFlash, onUndoActionClick } = this;
if (!user) return false;
const { onLogoutUser, onNavItemClick } = this;
const { pathname } = global.window.location;
2017-02-24 23:01:43 +00:00
const siteNavClasses = classnames('site-nav', {
'site-nav--small': isSmallNav,
});
const coreWrapperClasses = classnames('core-wrapper', {
'core-wrapper--small': isSmallNav,
});
return (
<div className="app-wrap">
<LoadingBar />
2017-02-24 23:01:43 +00:00
<nav className={siteNavClasses}>
2016-12-01 22:14:39 +00:00
<SiteNavHeader
config={config}
onLogoutUser={onLogoutUser}
onNavItemClick={onNavItemClick}
2016-12-01 22:14:39 +00:00
user={user}
/>
<SiteNavSidePanel
config={config}
onNavItemClick={onNavItemClick}
2016-12-01 22:14:39 +00:00
pathname={pathname}
user={user}
/>
</nav>
2017-02-24 23:01:43 +00:00
<div className={coreWrapperClasses}>
{persistentFlash.showFlash && <PersistentFlash message={persistentFlash.message} />}
2017-01-05 18:08:19 +00:00
<FlashMessage
fullWidth={fullWidthFlash}
notification={notifications}
onRemoveFlash={onRemoveFlash}
onUndoActionClick={onUndoActionClick}
/>
{children}
</div>
</div>
);
}
}
const mapStateToProps = (state) => {
const {
2017-02-24 23:01:43 +00:00
app: {
config,
isSmallNav,
},
auth: { user },
2017-01-05 18:08:19 +00:00
notifications,
persistentFlash,
} = state;
2017-01-05 18:08:19 +00:00
const fullWidthFlash = !user;
return {
config,
fullWidthFlash,
2017-02-24 23:01:43 +00:00
isSmallNav,
notifications,
persistentFlash,
user,
};
};
export default connect(mapStateToProps)(CoreLayout);