Implemented ladders

This commit is contained in:
2019-05-24 13:00:27 +10:00
parent 683ab95d63
commit 1ad4a14b35
7 changed files with 163 additions and 3 deletions
+2
View File
@@ -4,6 +4,7 @@ import { BrowserRouter, Switch, Route } from 'react-router-dom';
//include pages
import Home from './pages/home.jsx';
import Profile from './pages/profile.jsx';
import Ladder from './pages/ladder.jsx';
import PasswordReset from './pages/password_reset.jsx'
import PageNotFound from './pages/page_not_found.jsx';
@@ -23,6 +24,7 @@ export default class App extends React.Component {
<Switch>
<Route exact path='/' component={Home} />
<Route path='/profile' component={Profile} />
<Route path='/ladder' component={Ladder} />
<Route path='/passwordreset' component={PasswordReset} />
<Route path='*' component={PageNotFound} />
</Switch>
+79
View File
@@ -0,0 +1,79 @@
import React from 'react';
import CommonLinks from '../panels/common_links.jsx';
import PagedLadder from '../panels/paged_ladder.jsx';
class Ladder extends React.Component {
constructor(props) {
super(props);
this.state = {
start: 0,
length: 50,
fetch: null
};
}
componentDidUpdate() {
this.state.fetch();
}
render() {
let ButtonHeader = this.buttonHeader.bind(this);
return (
<div className='page'>
<h1 style={{textAlign: 'center', fontSize: '50px', margin: '30px'}}>KINGDOM BATTLES!</h1>
<div className='sidePanelPage'>
<div className='sidePanel'>
<CommonLinks />
</div>
<div className='mainPanel'>
<ButtonHeader />
<PagedLadder start={this.state.start} length={this.state.length} getFetch={this.getFetch.bind(this)} onReceived={this.onReceived.bind(this)} />
<ButtonHeader />
</div>
</div>
</div>
);
}
buttonHeader() {
return (
<div className='table'>
<div className='row'>
<button className='col' onClick={this.decrement.bind(this)}>{'< Back'}</button>
<div className='col' />
<div className='col' />
<button className='col' onClick={this.increment.bind(this)}>{'Next >'}</button>
</div>
</div>
);
}
increment() {
this.setState({
start: this.state.start + this.state.length
});
}
decrement() {
this.setState({
start: Math.max(0, this.state.start - this.state.length)
});
}
//bound callbacks
getFetch(fn) {
this.setState({ fetch: fn });
}
onReceived(data) {
if (data.length === 0) {
this.decrement();
}
}
}
export default Ladder;
+66
View File
@@ -0,0 +1,66 @@
import React from 'react';
import { withRouter, Link } from 'react-router-dom';
import PropTypes from 'prop-types';
class PagedLadder extends React.Component {
constructor(props) {
super(props);
this.state = {
data: {}
}
if (props.getFetch) {
props.getFetch(this.fetchLadder.bind(this));
}
this.fetchLadder();
}
render() {
return (
<div className='table'>
<div className='row'>
<p className='col'>Username</p>
<p className='col'>Soldiers</p>
<p className='col'>Recruits</p>
<p className='col'>Gold</p>
</div>
{Object.keys(this.state.data).map((key) => <div key={key} className={'row'}> <Link to={`/profile?username=${this.state.data[key].username}`} className={'col'}>{this.state.data[key].username}</Link><p className={'col'}>{this.state.data[key].soldiers}</p><p className={'col'}>{this.state.data[key].recruits}</p><p className={'col'}>{this.state.data[key].gold}</p><hr /></div> )}
</div>
);
}
fetchLadder(start = this.props.start, length = this.props.length) {
//build the XHR
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let data = JSON.parse(xhr.responseText);
this.setState({data: data});
if (this.props.onReceived) {
this.props.onReceived(data);
}
}
}
}
xhr.open('POST', '/ladderrequest', true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(JSON.stringify({
start: start,
length: length
}));
}
}
PagedLadder.propTypes = {
start: PropTypes.number.isRequired,
length: PropTypes.number.isRequired,
getFetch: PropTypes.func,
onReceived: PropTypes.func
};
export default withRouter(PagedLadder);