import React from 'react'; import { withRouter, Link } from 'react-router-dom'; import PropTypes from 'prop-types'; import BadgeText from './badge_text.jsx'; import ProgressiveRainbowText from './progressive_rainbow_text.jsx'; class PagedLadder extends React.Component { constructor(props) { super(props); this.state = { //TODO: data? } if (props.getFetch) { props.getFetch( () => this.sendRequest('/ladderrequest', {start: this.props.start || 0, length: this.props.length || 20}) ); } } render() { return (

Username

Soldiers

Recruits

Gold

{Object.keys(this.state).map((key) =>

{this.state[key].username}

Soldiers: {this.state[key].soldiers}

Recruits: {this.state[key].recruits}

Gold: {this.state[key].gold}

)}
); } sendRequest(url, args = {}) { //send a unified request, using my credentials //build the XHR let xhr = new XMLHttpRequest(); xhr.open('POST', url, true); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200) { let json = JSON.parse(xhr.responseText); //on success this.setState(json); if (this.props.onReceived) { this.props.onReceived(json); } } else if (xhr.status === 400 && this.props.setWarning) { this.props.setWarning(xhr.responseText); } } }; xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); xhr.send(JSON.stringify({ //NOTE: No id or token needed for the news ...args })); } }; PagedLadder.propTypes = { start: PropTypes.number, length: PropTypes.number, highlightedName: PropTypes.string, setWarning: PropTypes.func, getFetch: PropTypes.func, onReceived: PropTypes.func }; export default withRouter(PagedLadder);