-
Notifications
You must be signed in to change notification settings - Fork 25
/
app.jsx
158 lines (146 loc) · 6.14 KB
/
app.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React from "react";
import { Router, Route, Link } from "react-router";
import {browserHistory } from "react-router";
import ReactDOM from "react-dom";
import Application from "./components/application.jsx";
import Row from "./components/row.jsx";
import Col from "./components/col.jsx";
import NavBox from "./components/nav-box.jsx";
import NavItem from "./components/nav-item.jsx";
import Spinner from "./components/spinner.jsx";
import FlashList from "./components/flash-list.jsx";
import UserActions from "./actions/user";
import LoginStore from "./stores/login";
import MessageActions from "./actions/messages";
import MessageStore from "./stores/message";
import Welcome from "./application/welcome.jsx";
import LoginWelcome from "./application/login.jsx";
import LogoutButton from "./application/logout-button.jsx";
import RepoManager from "./application/repo-manager.jsx";
import RepoList from "./application/repo-list.jsx";
import ContributorsList from "./application/contributors-list.jsx";
import AdminUsers from "./application/admin/users.jsx";
import AdminGroups from "./application/admin/groups.jsx";
import EditUser from "./application/admin/edit-user.jsx";
import PickUser from "./application/admin/pick-user.jsx";
import AddUser from "./application/admin/add-user.jsx";
import PRViewer from "./application/pr/viewer.jsx";
import PROpen from "./application/pr/open.jsx";
import PRLastWeek from "./application/pr/last-week.jsx";
let utils = require("./application/utils")
, pp = utils.pathPrefix()
;
function getState () {
return { loggedIn: LoginStore.isLoggedIn(), admin: LoginStore.isAdmin(), importGranted: LoginStore.isImportGranted() };
}
class AshNazg extends React.Component {
constructor (props) {
super(props);
this.state = getState();
}
componentDidMount () {
LoginStore.addChangeListener(this._onChange.bind(this));
UserActions.login();
}
componentWillUnmount () {
LoginStore.removeChangeListener(this._onChange.bind(this));
}
_onChange () {
this.setState(getState());
}
render () {
let st = this.state
, nav
, body
, admin
, repoNav
, userNav
;
// show admin links as well
if (st.admin) {
admin = <NavBox title="Admin">
<NavItem><Link to={`${pp}admin/users`}>Users</Link></NavItem>
<NavItem><Link to={`${pp}admin/add-user`}>Add User</Link></NavItem>
<NavItem><Link to={`${pp}admin/groups`}>Groups</Link></NavItem>
</NavBox>
;
}
if (st.importGranted) {
repoNav = <div>
<NavItem><Link to={`${pp}repo/new`}>New Repository</Link></NavItem>
<NavItem><Link to={`${pp}repo/import`}>Import Repository</Link></NavItem>
</div>
;
}
if (st.loggedIn) {
userNav = <NavBox title="User">
<NavItem><LogoutButton/></NavItem>
</NavBox>
;
} else {
userNav = <NavBox title="User"><NavItem><Link to={`${pp}login`}>Login</Link></NavItem></NavBox>;
}
// when logged in show an actual menu and content
const isRoutePublic = this.props.routes[this.props.routes.length - 1].public;
if (st.loggedIn === true || isRoutePublic) {
nav = <Col className="nav">
<NavBox title="Repositories">
<NavItem><Link to={`${pp}repos`}>List Repositories</Link></NavItem>
{repoNav}
</NavBox>
<NavBox title="Pull Requests">
<NavItem><Link to={`${pp}pr/open`}>Currently Open</Link></NavItem>
<NavItem><Link to={`${pp}pr/last-week`}>Active Last Week</Link></NavItem>
</NavBox>
{admin}
{userNav}
</Col>;
body = <Col>{ renderChildrenWithAdminProp(this.props.children, st.admin) || <Welcome/> }</Col>;
}
// when logged out off to log in
else if (st.loggedIn === false) {
nav = <Col className="nav"><NavBox title="Login"/></Col>;
body = <Col><LoginWelcome/></Col>;
}
// while we don't know if we're logged in or out, spinner
else {
body = <Col><Spinner prefix={pp}/></Col>;
}
return <Application title="Repository Manager">
<FlashList store={MessageStore} actions={MessageActions}/>
<Row>
{nav}
{body}
</Row>
</Application>
;
}
}
// Set the "isAdmin" property on children components
function renderChildrenWithAdminProp(children, admin) {
return React.Children.map(children, child =>
React.cloneElement(child, {
isAdmin: admin
})
);
}
ReactDOM.render(
<Router history={browserHistory}>
<Route path={pp} component={AshNazg} public="true">
<Route path="login" component={LoginWelcome}/>
<Route path="repo/:owner/:shortname/:mode" component={RepoManager}/>
<Route path="repo/:mode" component={RepoManager}/>
<Route path="repos" component={RepoList} public="true"/>
<Route path="repos/:owner/:shortName/contributors" component={ContributorsList} public="true"/>
<Route path="pr/id/:owner/:shortName/:num" component={PRViewer} public="true"/>
<Route path="pr/open" component={PROpen} public="true"/>
<Route path="pr/last-week" component={PRLastWeek} public="true"/>
<Route path="admin/add-user" component={PickUser}/>
<Route path="admin/users" component={AdminUsers}/>
<Route path="admin/user/:username" component={EditUser}/>
<Route path="admin/user/:username/add" component={AddUser}/>
<Route path="admin/groups" component={AdminGroups}/>
</Route>
</Router>
, document.getElementById("ashnazg")
);