1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/frontend/src/component/app.jsx

208 lines
8.5 KiB
React
Raw Normal View History

2016-11-10 14:26:24 +01:00
import React, { Component } from 'react';
2016-12-04 11:56:41 +01:00
import { Layout, Drawer, Header, Navigation, Content,
Footer, FooterSection, FooterDropDownSection, FooterLinkList,
2016-12-05 15:15:01 +01:00
Grid, Cell,
2016-12-04 11:56:41 +01:00
} from 'react-mdl';
2016-12-05 17:04:00 +01:00
import { Link } from 'react-router';
2016-11-10 14:26:24 +01:00
import style from './styles.scss';
import ErrorContainer from './error/error-container';
import UserContainer from './user/user-container';
2016-11-25 15:37:06 +01:00
import ShowUserContainer from './user/show-user-container';
2016-12-05 17:04:00 +01:00
const base = {
name: 'Unleash',
link: '/',
};
function replace (input, params) {
if (!params) {
return input;
}
Object.keys(params).forEach(key => {
input = input.replace(`:${key}`, params[key]);
});
return input;
}
2016-11-10 14:26:24 +01:00
export default class App extends Component {
constructor (props) {
super(props);
this.state = { drawerActive: false };
this.toggleDrawerActive = () => {
this.setState({ drawerActive: !this.state.drawerActive });
};
}
2016-12-04 11:56:41 +01:00
static contextTypes = {
router: React.PropTypes.object,
}
2016-11-10 14:26:24 +01:00
2016-12-05 17:10:32 +01:00
componentWillReceiveProps (nextProps) {
// copied from https://github.com/react-mdl/react-mdl/issues/254
// If our locations are different and drawer is open,
// force a close
if (this.props.location.pathname !== nextProps.location.pathname) {
const layout = document.querySelector('.mdl-js-layout');
const drawer = document.querySelector('.mdl-layout__drawer');
2016-12-05 19:29:07 +01:00
if (drawer.classList.contains('is-visible')) {
2016-12-05 17:10:32 +01:00
layout.MaterialLayout.toggleDrawer();
}
}
}
2016-12-05 17:15:55 +01:00
getSections () {
2016-12-05 17:04:00 +01:00
const { routes, params } = this.props;
const unique = {};
let result = [base].concat(routes.splice(1).map((routeEntry) => ({
name: replace(routeEntry.pageTitle, params),
link: replace(routeEntry.link || routeEntry.path, params),
}))).filter(entry => {
if (!unique[entry.link]) {
unique[entry.link] = true;
return true;
}
return false;
});
2016-12-05 17:15:55 +01:00
// mutate document.title:
document.title = result
.map(e => e.name)
2016-12-05 17:16:51 +01:00
.reverse()
2016-12-05 17:15:55 +01:00
.join(' - ');
2016-12-05 17:04:00 +01:00
if (result.length > 2) {
result = result.splice(1);
}
2016-12-05 17:15:55 +01:00
return result;
}
getTitleWithLinks () {
const result = this.getSections();
2016-12-05 17:04:00 +01:00
return (
<span>
{result.map((entry, index) => (
<span><Link style={{ color: '#f1f1f1', textDecoration: 'none' }} key={entry.link + index} to={entry.link}>
{entry.name}
</Link> {(index + 1) < result.length ? ' / ' : null}</span>
))}
</span>
);
}
2016-11-10 14:26:24 +01:00
onOverlayClick = () => this.setState({ drawerActive: false });
render () {
2016-12-04 11:56:41 +01:00
const createListItem = (path, caption) =>
<a
2016-12-05 15:15:01 +01:00
href={this.context.router.createHref(path)}
2016-12-04 11:56:41 +01:00
className={this.context.router.isActive(path) ? style.active : ''}>
{caption}
</a>;
return (
<div style={{}}>
<UserContainer />
<Layout fixedHeader>
2016-12-05 17:04:00 +01:00
<Header title={this.getTitleWithLinks()}>
2016-12-04 11:56:41 +01:00
<Navigation>
<a href="https://github.com/Unleash" target="_blank">Github</a>
<ShowUserContainer />
</Navigation>
</Header>
<Drawer title="Unleash Admin">
<Navigation>
{createListItem('/features', 'Feature toggles')}
{createListItem('/strategies', 'Strategies')}
{createListItem('/history', 'Event history')}
{createListItem('/archive', 'Archived toggles')}
<hr />
2016-12-05 15:15:01 +01:00
{createListItem('/applications', 'Applications')}
2016-12-05 18:25:46 +01:00
{/*createListItem('/metrics', 'Client metrics')*/}
2016-12-04 11:56:41 +01:00
{createListItem('/client-strategies', 'Client strategies')}
2016-12-05 18:25:46 +01:00
{/*createListItem('/client-instances', 'Client instances')*/}
2016-12-04 11:56:41 +01:00
</Navigation>
</Drawer>
<Content>
2016-12-05 19:29:07 +01:00
<Grid shadow={1} style={{ maxWidth: '1200px', margin: '0 auto' }}>
2016-12-04 11:56:41 +01:00
<Cell col={12}>
{this.props.children}
<ErrorContainer />
</Cell>
</Grid>
<Footer size="mega">
<FooterSection type="middle">
<FooterDropDownSection title="Menu">
<FooterLinkList>
{createListItem('/features', 'Feature toggles')}
{createListItem('/strategies', 'Strategies')}
{createListItem('/history', 'Event history')}
{createListItem('/archive', 'Archived toggles')}
</FooterLinkList>
</FooterDropDownSection>
<FooterDropDownSection title="Metrics">
<FooterLinkList>
2016-12-05 15:15:01 +01:00
{createListItem('/applications', 'Applications')}
2016-12-05 18:25:46 +01:00
{/*createListItem('/metrics', 'Client metrics')*/}
2016-12-04 11:56:41 +01:00
{createListItem('/client-strategies', 'Client strategies')}
2016-12-05 18:25:46 +01:00
{/*createListItem('/client-instances', 'Client instances')*/}
2016-12-04 11:56:41 +01:00
</FooterLinkList>
</FooterDropDownSection>
<FooterDropDownSection title="FAQ">
<FooterLinkList>
<a href="#">Help</a>
<a href="#">Privacy & Terms</a>
<a href="#">Questions</a>
<a href="#">Answers</a>
<a href="#">Contact Us</a>
</FooterLinkList>
</FooterDropDownSection>
<FooterDropDownSection title="Clients">
<FooterLinkList>
<a href="https://github.com/Unleash/unleash-node-client/">Node.js</a>
<a href="https://github.com/Unleash/unleash-java-client/">Java</a>
</FooterLinkList>
</FooterDropDownSection>
</FooterSection>
<FooterSection type="bottom" logo="Unleash Admin">
<FooterLinkList>
<a href="https://github.com/Unleash/unleash/" target="_blank">
GitHub
</a>
<a href="https://finn.no" target="_blank"><small>A product by</small> FINN.no</a>
</FooterLinkList>
</FooterSection>
</Footer>
</Content>
</Layout>
</div>
);
2016-11-10 14:26:24 +01:00
return (
<div className={style.container}>
2016-11-25 15:37:06 +01:00
<AppBar title="Unleash Admin" leftIcon="menu" onLeftIconClick={this.toggleDrawerActive} className={style.appBar}>
2016-12-04 11:56:41 +01:00
2016-11-25 15:37:06 +01:00
</AppBar>
2016-11-10 14:26:24 +01:00
<div className={style.container} style={{ top: '6.4rem' }}>
<Layout>
<NavDrawer active={this.state.drawerActive} permanentAt="sm" onOverlayClick={this.onOverlayClick} >
<Navigation />
</NavDrawer>
<Panel scrollY>
<div style={{ padding: '1.8rem' }}>
2016-12-04 11:56:41 +01:00
2016-11-10 14:26:24 +01:00
{this.props.children}
</div>
</Panel>
2016-12-04 11:56:41 +01:00
2016-11-10 14:26:24 +01:00
</Layout>
</div>
</div>
);
}
};