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

213 lines
6.3 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>
<li key="empact" className="nav empact">
<Link to="org" params={this.getParams()}>Empact</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 OrgStats = React.createClass({
2015-03-10 08:24:19 +00:00
mixins: [Router.State],
2015-03-07 14:13:47 +00:00
render: function(){
var org = Storage.get('org', this.getParams().org);
2015-03-07 14:13:47 +00:00
return (
2015-03-08 08:42:40 +00:00
<section className="content">
<InfoBlock image={org.avatar_url} title={org.login} text={org.descr} />
2015-03-09 16:24:33 +00:00
<BarChart key={this.getParams().team} api="/api/stat/orgs/top"
params={this.getParams()} items={["repo", "team", "user"]} />
2015-03-08 08:42:40 +00:00
</section>
2015-03-07 14:13:47 +00:00
);
}
});
var TeamStats = React.createClass({
2015-03-10 08:24:19 +00:00
mixins: [Router.State],
2015-03-07 14:13:47 +00:00
render: function(){
return (
2015-03-08 11:17:56 +00:00
<section className="content">
2015-03-10 08:24:19 +00:00
<InfoBlock key={"info-block-"+ this.getParams().team}
image="https://media.licdn.com/mpr/mpr/p/8/005/058/14b/0088c48.jpg"
title={this.getParams().team}
text={"The most awesome team in "+ this.getParams().org} />
<BarChart key={'bar-chart-'+ this.getParams().team} api="/api/stat/teams/top"
2015-03-09 16:24:33 +00:00
params={this.getParams()} items={["repo", "user"]} />
2015-03-08 11:17:56 +00:00
</section>
2015-03-07 14:13:47 +00:00
);
}
});
var UserStats = React.createClass({
2015-03-10 08:24:19 +00:00
mixins: [Router.State],
2015-03-07 14:13:47 +00:00
render: function(){
return (
2015-03-08 15:16:27 +00:00
<section className="content">
<InfoBlock title={this.getParams().user} />
<BarChart key={'bar-chart-'+ this.getParams().user} api="/api/stat/users/top"
2015-03-09 16:24:33 +00:00
params={this.getParams()} items={["repo"]} />
2015-03-08 15:16:27 +00:00
</section>
2015-03-07 14:13:47 +00:00
);
}
});
var RepoStats = React.createClass({
2015-03-10 08:24:19 +00:00
mixins: [Router.State],
2015-03-07 14:13:47 +00:00
render: function(){
return (
2015-03-08 15:16:27 +00:00
<section className="content">
<InfoBlock title={this.getParams().repo} />
2015-03-09 16:24:33 +00:00
<BarChart key={this.getParams().team} api="/api/stat/repos/top"
params={this.getParams()} items={["user", "team"]} />
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={OrgStats} />
<Router.Route name="team" path="teams/:team" handler={TeamStats} />
<Router.Route name="user" path="users/:user" handler={UserStats} />
<Router.Route name="repo" path="repos/:repo" handler={RepoStats} />
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
});