1
0
Fork 0
empact/app/scripts/src/app.jsx

210 lines
6.4 KiB
React
Raw Normal View History

2015-03-07 14:13:47 +00:00
var Router = ReactRouter,
2015-03-08 08:42:40 +00:00
Link = Router.Link;
2015-03-07 14:13:47 +00:00
var Storage = {
set: function(category, key, value) {
window.localStorage.setItem(category +'-'+ key, JSON.stringify(value));
},
get: function(category, key) {
var val = window.localStorage.getItem(category +'-'+ key);
return val === null ? null : JSON.parse(val);
2015-03-07 14:13:47 +00:00
}
};
2015-03-07 14:13:47 +00:00
var App = React.createClass({
mixins: [Router.Navigation, Router.State],
orgsURL: "/api/orgs",
teamsURL: "/api/teams?org=",
2015-03-07 14:13:47 +00:00
2015-03-07 08:34:35 +00:00
getInitialState: function() {
return {
orgs: [],
org: null,
teams: [],
team: null
};
2015-03-07 08:34:35 +00:00
},
componentDidMount: function() {
this.loadOrgs();
2015-03-07 08:34:35 +00:00
this.loadTeams();
},
loadOrgs: function() {
$.get(this.orgsURL, function(res){
this.setState({orgs: res});
if (res !== null) {
for (var i = 0; i < res.length; i++) {
var org = res[i];
Storage.set('org', org.login, org);
}
}
}.bind(this));
},
2015-03-07 08:34:35 +00:00
loadTeams: function() {
$.get(this.teamsURL + this.getParams().org, function(res){
2015-03-07 08:34:35 +00:00
this.setState({teams: res})
if (res !== null) {
for (var i = 0; i < res.length; i++) {
var team = res[i];
Storage.set('team', team.name, team);
}
}
2015-03-07 08:34:35 +00:00
}.bind(this));
},
render: function(){
return (
<section className="app">
<Menu orgs={this.state.orgs} teams={this.state.teams} />
<Router.RouteHandler />
</section>
);
}
});
var Menu = React.createClass({
mixins: [Router.State],
2015-03-07 08:34:35 +00:00
render: function() {
var renderOrg = function(org) {
return (
<li key={'org-'+ org.login} className="nav org">
<Link to="org" params={{org: org.login}}>{org.login}</Link>
</li>
)
};
2015-03-07 08:34:35 +00:00
var renderTeam = function(team) {
return (
<li key={'team-'+ team.name} className="nav team">
2015-03-08 10:02:14 +00:00
<Link to="team" params={{org: team.owner, team: team.name}}>{team.name}</Link>
2015-03-07 14:13:47 +00:00
</li>
2015-03-07 08:34:35 +00:00
)
};
return (
2015-03-07 14:13:47 +00:00
<section className="menu">
<ul>
2015-03-11 12:10:11 +00:00
<li key="empact">
<Link to="org" params={this.getParams()} className="logo-button">
2015-03-10 12:48:57 +00:00
<div className="logo e">e</div>
2015-03-11 12:10:11 +00:00
<div className="logo m">m</div>
<div className="logo p">p</div>
<div className="logo a">a</div>
<div className="logo c">c</div>
<div className="logo t">t</div>
2015-03-10 12:48:57 +00:00
</Link>
2015-03-07 14:13:47 +00:00
</li>
<li key="orgs-header" className="nav header">Organizations:</li>
{this.props.orgs.map(renderOrg)}
2015-03-07 14:13:47 +00:00
<li key="teams-header" className="nav header">Teams:</li>
{this.props.teams.map(renderTeam)}
2015-03-07 14:13:47 +00:00
</ul>
</section>
);
}
});
2015-03-10 08:04:59 +00:00
var Org = React.createClass({
2015-03-07 14:13:47 +00:00
render: function(){
return (
<Router.RouteHandler />
2015-03-07 14:13:47 +00:00
);
}
});
var Dashboard = React.createClass({
2015-03-10 08:24:19 +00:00
mixins: [Router.State],
2015-03-07 14:13:47 +00:00
render: function(){
var p = this.getParams(),
infoImage, infoTitle, infoText,
bcApi, bcItems,
sacApi, sacItems;
if (p.team) {
infoTitle = p.team;
bcApi = '/api/stat/teams/top';
bcItems = ['repo', 'user'],
sacApi = '/api/stat/teams/activity';
sacItems = ['user', 'repo'];
} else if (p.user) {
infoTitle = p.user;
bcApi = '/api/stat/users/top';
bcItems = ['repo'],
sacApi = '/api/stat/users/activity';
sacItems = ['repo'];
} else if (p.repo) {
infoTitle = p.repo;
bcApi = '/api/stat/repos/top';
bcItems = ['user', 'team'],
sacApi = '/api/stat/repos/activity';
sacItems = ['user', 'team'];
} else {
var info = Storage.get('org', p.org);
infoImage = info.avatar_url;
infoTitle = info.login;
infoText = info.descr;
bcApi = '/api/stat/orgs/top';
bcItems = ['repo', 'team', 'user'],
sacApi = '/api/stat/orgs/activity';
sacItems = ['team', 'user', 'repo'];
}
2015-03-07 14:13:47 +00:00
return (
2015-03-08 15:16:27 +00:00
<section className="content">
<InfoBlock image={infoImage} title={infoTitle} text={infoText} />
<BarChart api={bcApi} params={this.getParams()} items={bcItems} />
<StackedAreaChart api={sacApi} params={this.getParams()} items={sacItems} />
2015-03-08 15:16:27 +00:00
</section>
2015-03-07 14:13:47 +00:00
);
}
});
var NotFound = React.createClass({
render: function(){
return (
<section className="content">NOT FOUND :(</section>
2015-03-07 08:34:35 +00:00
);
}
});
2015-03-07 14:13:47 +00:00
var SelectOrg = React.createClass({
render: function(){
return (
<section className="content">Please select organization from the menu!</section>
);
}
});
2015-03-10 08:24:19 +00:00
var InfoBlock = React.createClass({
render: function() {
var img = <div className="img" style={{backgroundImage: "url("+ this.props.image +")"}} />;
return (
<div className="info-block">
{ this.props.image ? img : null }
<h1>{this.props.title}</h1>
<p>{this.props.text}</p>
</div>
)
}
});
2015-03-07 14:13:47 +00:00
var routes = [
<Router.Route name="root" path="/app/" handler={App}>
<Router.DefaultRoute handler={SelectOrg} />
<Router.NotFoundRoute handler={NotFound} />
<Router.Route name="org" path=":org" handler={Org}>
<Router.DefaultRoute handler={Dashboard} />
<Router.Route name="team" path="teams/:team" handler={Dashboard} />
<Router.Route name="user" path="users/:user" handler={Dashboard} />
<Router.Route name="repo" path="repos/:repo" handler={Dashboard} />
2015-03-08 08:42:40 +00:00
</Router.Route>
</Router.Route>
];
2015-03-07 14:13:47 +00:00
Router.run(routes, Router.HistoryLocation, function(Handler) {
React.render(<Handler />, document.body);
2015-03-07 14:13:47 +00:00
});