-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientReact.js
More file actions
274 lines (265 loc) · 8.81 KB
/
clientReact.js
File metadata and controls
274 lines (265 loc) · 8.81 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* The page header
*/
class Header extends React.Component {
render() {
return (
<header>
<a href="http://www.freecodecamp.com">
<img className="fcclogo" src="https://s3.amazonaws.com/freecodecamp/freecodecamp_logo.svg" alt="FreeCodeCamp logo" />
</a>
</header>
);
}
}
/*
* The page footer
*/
class Footer extends React.Component {
render() {
return (
<footer>
<div className="container">
<p>*** By <a href="http://www.freecodecamp.com/roelver">@roelver</a> ***</p>
</div>
</footer>
);
}
}
/*
* Component for column headers including basic sort mechanism
*/
class ColumnHeadings extends React.Component {
render() {
var all = (this.props.modus === "alltime");
return (
<thead>
<tr id="colheaders" className="top100">
<th className="idcol">#</th>
<th onClick={this.handleClickStr.bind(this,"username")}>Camper Name</th>
<th id="defaultsort" className="sorted true" onClick={this.handleClickNum.bind(this, (all ? "total" : "totalRecent"))}>{all ? "All time score" : "Recent score"}</th>
<th onClick={this.handleClickNum.bind(this, (all ? "points" : "pointsRecent"))}>Points</th>
<th onClick={this.handleClickNum.bind(this, (all ? "basejumps" : "basejumpsRecent"))}>Basejumps (x60)</th>
<th onClick={this.handleClickNum.bind(this, (all ? "ziplines" : "ziplinesRecent"))}>Ziplines (x30)</th>
<th onClick={this.handleClickNum.bind(this, (all ? "bonfires" : "bonfiresRecent"))}>Bonfires (x3)</th>
<th onClick={this.handleClickNum.bind(this, (all ? "totalRecent" : "total"))}>{ all ? "Recent score" : "All-time score"}</th>
<th className="lastchecked">Last update</th>
<th className="buttons"></th>
</tr>
</thead>
);
}
removeSortClasses() {
var nodes = document.getElementById('colheaders').childNodes;
for (var i=0; i < nodes.length; i++) {
nodes.item(i).className = "";
};
}
handleClickStr(fieldname, evt) {
var reverse = !this.props.reverse;
if (evt.target.classList.contains('sorted')) {
evt.target.className = "sorted "+ reverse;
this.props.sortTableStr(fieldname, reverse);
}
else {
this.removeSortClasses();
evt.target.className = 'sorted true';
this.props.sortTableStr(fieldname, true);
}
}
handleClickNum(fieldname, evt) {
var reverse = !this.props.reverse;
if (evt.target.classList.contains('sorted')) {
evt.target.className = "sorted "+ reverse;
this.props.sortTableNum(fieldname, reverse);
}
else {
this.removeSortClasses();
evt.target.className = 'sorted true';
this.props.sortTableNum(fieldname, true);
}
}
}
/*
* Component represents a user row in the table and a handler for user updates
*/
class User extends React.Component {
render() {
var showDate = moment(this.props.user.lastUpdate).format("YYYY-MM-DD HH:mm:ss");
var all = (this.props.modus === "alltime");
return (
<tr className="top100">
<td className="idcol">{this.props.count}</td>
<td>
<a href={"http://www.freecodecamp.com/"+this.props.user.username} target="_blank">
<img src={this.props.user.img} className="userimg"/>
<span>{this.props.user.username}</span>
</a>
</td>
<td className="numbercol">{all ? this.props.user.total : this.props.user.totalRecent}</td>
<td className="numbercol">{all ? this.props.user.points : this.props.user.pointsRecent}</td>
<td className="numbercol">{all ? this.props.user.basejumps : this.props.user.basejumpsRecent}</td>
<td className="numbercol">{all ? this.props.user.ziplines : this.props.user.ziplinesRecent}</td>
<td className="numbercol">{all ? this.props.user.bonfires : this.props.user.bonfiresRecent}</td>
<td className="numbercol">{all ? this.props.user.totalRecent : this.props.user.total}</td>
<td className="lastchecked">{showDate}</td>
<td className="buttons">
<button className="btn btn-default" onClick={this.handleClickUpdateUser.bind(this)}>
<i className="glyphicon glyphicon-refresh"></i>
</button>
</td>
</tr>
);
}
handleClickUpdateUser() {
$.get( this.props.apiroot+"update/"+this.props.user.username,
function( data ) {
setTimeout(this.props.updatePage,2000);
}.bind(this)
)
.fail(function() {
console.error(this.props.apiroot, status, err.toString());
});
}
}
/*
* Component for
*/
class Leaderboard extends React.Component {
render() {
var count = 0;
var self = this;
var userlist = this.props.users.map(function(user) {
count++;
return (count <= 100 ?
<User user={user} key={user.username} count={count} apiroot={this.props.apiroot} modus={this.props.modus} updatePage={this.props.updatePage}/> : null
);
}.bind(this));
return (
<table className="table table-striped table-bordered">
<ColumnHeadings modus={this.props.modus} reverse={this.props.reverse}
sortTableNum={this.props.sortTableNum} sortTableStr={this.props.sortTableStr}/>
<tbody>
{userlist}
</tbody>
</table>
);
}
}
/*
* The page body, with state and status handlers for switching the mode (recent/all time)
* and handlers for sorting
*/
class Body extends React.Component {
constructor() {
super();
this.state = {
users: [],
modus: "recent",
reverse: true,
column: "totalRecent"
}
}
getData() {
$.ajax({
url: this.props.apiroot+this.state.modus,
dataType: 'json',
cache: false,
success: function(data) {
var users = data.map(function(user) {
user.totalRecent = (user.basejumpsRecent * 60) + (user.ziplinesRecent * 30) + (user.bonfiresRecent * 3) + user.pointsRecent;
user.total = (user.basejumps * 60) + (user.ziplines * 30) + (user.bonfires * 3) + user.points;
return user;
});
this.setState({users: users});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.apiroot, status, err.toString());
}.bind(this)
});
}
componentDidMount() {
this.getData();
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-lg-12">
<div id="header">
<h3>{ this.state.modus === "recent" ? "Leaderboard over past 30 days" : "All time leaderboard"}</h3>
<a href="#" className="switchbutton" onClick={this.switchModus.bind(this)}>{this.state.modus === "recent" ? "Show All-time" : "Show Recent"}</a>
</div>
</div>
</div>
<div className="row">
<div className="col-lg-12">
<Leaderboard
users={this.state.users}
modus={this.state.modus}
reverse={this.state.reverse}
apiroot={this.props.apiroot}
updatePage={this.getData.bind(this)}
sortTableNum={this.sortTableNum.bind(this)}
sortTableStr={this.sortTableStr.bind(this)}
/>
</div>
</div>
</div>
);
}
removeSortClasses() {
var nodes = document.getElementById('colheaders').childNodes;
for (var i=0; i < nodes.length; i++) {
nodes.item(i).className = "";
};
}
switchModus() {
if (this.state.modus === "recent") {
this.setState({modus: "alltime", reverse: true, column: "total"}, this.getData);
}
else {
this.setState({modus: "recent", reverse: true, column: "totalRecent"}, this.getData);
}
this.removeSortClasses();
var head = document.getElementById('defaultsort');
head.className = "sorted true";
}
sortTableNum(column, newReverse) {
var users = this.state.users;
users.sort(function(a,b) {
return a[column] - b[column];
} ) ;
if (newReverse) {
users.reverse();
}
this.setState({users: users, reverse: newReverse, column: column});
}
sortTableStr(column, newReverse) {
var users = this.state.users;
users.sort(function(a,b) {
if (a[column].toLowerCase() < b[column].toLowerCase()) return -1;
return 1;
} );
if (newReverse) {
users.reverse();
}
this.setState({users: users, reverse: newReverse, column: column});
}
}
/*
* The application level component
*/
class Application extends React.Component {
render() {
return <div>
<Header />
<Body apiroot={this.props.apiroot} />
<Footer />
</div>;
}
}
/*
* Put it all into the HTML
*/
ReactDOM.render(<Application apiroot="http://fcctop100.herokuapp.com/api/fccusers/"/>, document.getElementById('fcctop'));