const React = window.React; const Dropdown = window.Dropdown; class SyncbackRequestDetails extends React.Component { constructor(props) { super(props); this.state = { open: false, accountId: props.accountId, syncbackRequests: null, counts: null, statusFilter: 'all', }; } getDetails() { const req = new XMLHttpRequest(); const url = `${window.location.protocol}/syncback-requests/${this.state.accountId}`; req.open("GET", url, true); req.onreadystatechange = () => { if (req.readyState === XMLHttpRequest.DONE) { if (req.status === 200) { this.setState({syncbackRequests: req.responseText}); } else { console.error(req.responseText); } } } req.send(); } getCounts() { const since = Date.now() - 1000 * 60 * 60; // one hour ago const req = new XMLHttpRequest(); const url = `${window.location.protocol}/syncback-requests/${this.state.accountId}/counts?since=${since}`; req.open("GET", url, true); req.onreadystatechange = () => { if (req.readyState === XMLHttpRequest.DONE) { if (req.status === 200) { this.setState({counts: JSON.parse(req.responseText)}); } else { console.error(req.responseText); } } } req.send(); } setStatusFilter(statusFilter) { this.setState({statusFilter: statusFilter}); } open() { this.getDetails(); this.getCounts(); this.setState({open: true}); } close() { this.setState({open: false}); } render() { if (this.state.open) { let counts = Of requests created in the last hour: ... if (this.state.counts) { const total = this.state.counts.new + this.state.counts.failed + this.state.counts.succeeded; if (total === 0) { counts = "No requests made in the last hour"; } else { counts = (
Of requests created in the last hour: {this.state.counts.failed / total * 100}% failed {this.state.counts.succeeded / total * 100}% succeeded {/* .new was throwing off my syntax higlighting, so ignoring linter*/} {this.state.counts['new'] / total * 100}% are still new
) } } let details = "Loading..." if (this.state.syncbackRequests) { let reqs = JSON.parse(this.state.syncbackRequests); if (this.state.statusFilter !== 'all') { reqs = reqs.filter((req) => req.status === this.state.statusFilter); } let rows = []; if (reqs.length === 0) { rows.push(No results--); } for (let i = reqs.length - 1; i >= 0; i--) { const req = reqs[i]; const date = new Date(req.createdAt); rows.push( {req.status} {req.type} {date.toLocaleTimeString()}, {date.toLocaleDateString()} ) } details = ( {rows}
Status:  this.setStatusFilter.call(this, status)} /> Type Created At
); } return (
Syncback Request Details
this.close.call(this)}> X
{counts} {details}
); } // else, the modal isn't open return (
this.open.call(this)}> Syncback Request Details
); } } SyncbackRequestDetails.propTypes = { accountId: React.PropTypes.number, } window.SyncbackRequestDetails = SyncbackRequestDetails;