1
0
Fork 0
chronos/app/layouts/main.jsx

162 lines
3.6 KiB
React
Raw Normal View History

2017-03-30 20:55:54 +08:00
import React from 'react';
import { Layout, NavDrawer, Panel, AppBar, Navigation, Link, List, ListItem } from 'react-toolbox';
export default class LayoutMain extends React.Component {
2017-04-16 17:03:00 +08:00
constructor(props, context) {
super(props, context);
2017-03-30 20:55:54 +08:00
this.state = {
drawerActive: false,
2017-04-16 17:03:00 +08:00
title: 'Chronos',
showPagination: false,
2017-03-30 20:55:54 +08:00
};
2017-04-16 17:03:00 +08:00
this.context = context;
// eslint-disable-next-line no-param-reassign
this.context.tooling.onChange(this.setToolbar.bind(this));
this.paginatePrev = this.paginatePrev.bind(this);
this.paginateNext = this.paginateNext.bind(this);
2017-03-30 20:55:54 +08:00
this.toggleDrawerActive = this.toggleDrawerActive.bind(this);
}
2017-04-16 17:03:00 +08:00
setToolbar(o) {
this.setState({
title: o.title || 'Chronos',
showPagination: o.showPagination,
});
}
paginatePrev() {
this.context.tooling.onPaginatePrev();
}
paginateNext() {
this.context.tooling.onPaginateNext();
}
2017-03-30 20:55:54 +08:00
toggleDrawerActive() {
this.setState({
2017-04-12 00:14:51 +08:00
drawerActive: !this.state.drawerActive, // TODO: use function instead
2017-03-30 20:55:54 +08:00
});
}
render() {
return (
<Layout>
<NavDrawer
active={this.state.drawerActive}
permanentAt="md"
onOverlayClick={this.toggleDrawerActive}
>
2017-04-13 00:27:16 +08:00
<div
style={{ fontSize: '1.2em' }}
>
2017-04-12 00:14:51 +08:00
{this.context.user.email ?
<span>
Hello, <span title={this.context.user.email}>{this.context.user.name}</span>!
2017-04-13 00:27:16 +08:00
</span>
:
'Not logged in'
}
2017-03-30 20:55:54 +08:00
</div>
2017-04-13 00:27:16 +08:00
<List
selectable
ripple
>
{this.context.user.email ?
[
<ListItem
key={0}
caption="Home"
onClick={() => this.context.router.history.push('/')}
/>,
<ListItem
key={1}
caption="Groups"
onClick={() => this.context.router.history.push('/groups')}
/>,
]
:
null
}
<ListItem
caption="Help"
onClick={() => {}}
/>
<ListItem
caption="About"
onClick={() => {}}
/>
<ListItem
caption="GitHub"
to="https://github.com/ambrosechua/chronos"
/>
2017-03-30 20:55:54 +08:00
</List>
</NavDrawer>
<Panel>
2017-04-13 00:27:16 +08:00
<AppBar
2017-04-16 17:03:00 +08:00
title={this.state.title}
2017-04-13 00:27:16 +08:00
leftIcon="menu"
onLeftIconClick={this.toggleDrawerActive}
>
<Navigation
type="horizontal"
>
2017-04-16 17:03:00 +08:00
{this.state.showPagination &&
<Link
style={{ color: 'var(--color-dark-contrast)' }}
icon="navigate_before"
onClick={this.paginatePrev}
/>
}
{this.state.showPagination &&
<Link
style={{ color: 'var(--color-dark-contrast)' }}
icon="navigate_next"
onClick={this.paginateNext}
/>
}
2017-04-13 00:27:16 +08:00
{this.context.user.email ?
<Link
style={{ color: 'var(--color-dark-contrast)' }}
label="Logout"
onClick={() => this.context.router.history.push('/logout')}
/>
:
<Link
style={{ color: 'var(--color-dark-contrast)' }}
label="Login"
onClick={() => this.context.router.history.push('/login')}
/>
}
2017-03-30 20:55:54 +08:00
</Navigation>
</AppBar>
{this.props.children}
</Panel>
</Layout>
);
}
}
LayoutMain.defaultProps = {
children: null,
};
LayoutMain.propTypes = {
children: React.PropTypes.node,
};
LayoutMain.contextTypes = {
router: React.PropTypes.shape({
history: React.PropTypes.shape({
push: React.PropTypes.func.isRequired,
}).isRequired,
}).isRequired,
2017-04-12 00:14:51 +08:00
// eslint-disable-next-line react/forbid-prop-types
user: React.PropTypes.object.isRequired,
2017-04-13 00:27:16 +08:00
token: React.PropTypes.string,
2017-04-16 17:03:00 +08:00
// eslint-disable-next-line react/forbid-prop-types
tooling: React.PropTypes.object.isRequired,
2017-03-30 20:55:54 +08:00
};