/*** @jsx React.DOM */ var api_management = createReactClass({ getInitialState: function(props) { var info = this.scheduleServerInfo(window.servers); return { new_client: { name: '', }, new_server: { uri: 'https://', token: '', }, info: info, clients: window.clients, servers: window.servers, editing_client: null, editing_server: null, }; }, componentDidUpdate: function() { if (this.focus_element && this.focus_element != this.already_focused) { this.focus_element.focus(); this.already_focused = this.focus_element; } }, scheduleServerInfo: function(servers) { var info = {}; servers.map(function(server) { if (this.state && this.state.info[server.id]) { // We already got this info info[server.id] = this.state.info[server.id]; return; } // We don't have this looked up yet info[server.id] = {loading: true}; AJAX.get( Link.get('queryserver', server.id), function(response) { var sinfo = this.state.info; sinfo[server.id] = response; sinfo[server.id].loading = false; this.setState({info: sinfo}); }.bind(this) ); }.bind(this)); return info; }, addNewClient: function(event) { AJAX.post( Link.get('addclient'), {client: this.state.new_client}, function(response) { this.setState({ clients: response.clients, new_client: { name: '', }, }); }.bind(this) ); event.preventDefault(); }, saveClient: function(event) { AJAX.post( Link.get('updateclient'), {client: this.state.editing_client}, function(response) { this.setState({ clients: response.clients, editing_client: null, }); }.bind(this) ); event.preventDefault(); }, deleteExistingClient: function(event, clientid) { $.confirm({ escapeKey: 'Cancel', animation: 'none', closeAnimation: 'none', title: 'Delete Client', content: 'Are you sure you want to delete and de-auth this client from the network?', buttons: { Delete: { btnClass: 'delete', action: function() { AJAX.post( Link.get('removeclient'), {clientid: clientid}, function(response) { this.setState({ clients: response.clients, }); }.bind(this) ); }.bind(this), }, Cancel: function() { }, } }); event.preventDefault(); }, addNewServer: function(event) { AJAX.post( Link.get('addserver'), {server: this.state.new_server}, function(response) { // Schedule probe of new server just added var info = this.scheduleServerInfo(response.servers); this.setState({ servers: response.servers, info: info, new_server: { uri: 'https://', token: '', }, }); }.bind(this) ); event.preventDefault(); }, saveServer: function(event) { var serverid = this.state.editing_server.id; AJAX.post( Link.get('updateserver'), {server: this.state.editing_server}, function(response) { // Kill our existing info so we can refresh with changes delete this.state.info[serverid]; var info = this.scheduleServerInfo(response.servers); this.setState({ info: info, servers: response.servers, editing_server: null, }); }.bind(this) ); event.preventDefault(); }, deleteExistingServer: function(event, serverid) { $.confirm({ escapeKey: 'Cancel', animation: 'none', closeAnimation: 'none', title: 'Delete Server', content: 'Are you sure you want to delete this remote server from the network?', buttons: { Delete: { btnClass: 'delete', action: function() { AJAX.post( Link.get('removeserver'), {serverid: serverid}, function(response) { // Kill the entry we no longer need var info = this.state.info; delete info[serverid]; this.setState({ info: info, servers: response.servers, }); }.bind(this) ); }.bind(this), }, Cancel: function() { }, } }); event.preventDefault(); }, renderClientEditButton: function(client) { if(this.state.editing_client) { if (this.state.editing_client.id == client.id) { return ( <> ); } else { return null; } } else { return ( <> ); } }, renderClientName: function(client) { if (this.state.editing_client && client.id == this.state.editing_client.id) { return (this.focus_element = c)} value={ this.state.editing_client.name } onChange={function(event) { var client = this.state.editing_client; client.name = event.target.value; this.setState({ editing_client: client, }); }.bind(this)} />; } else { return { client.name }; } }, sortClientName: function(a, b) { return a.name.localeCompare(b.name); }, renderClientToken: function(client) { return { client.token }; }, sortClientToken: function(a, b) { return a.token.localeCompare(b.token); }, renderServerEditButton: function(server) { if(this.state.editing_server) { if (this.state.editing_server.id == server.id) { return ( <> ); } else { return null; } } else { return ( <> ); } }, renderServerURI: function(server) { if (this.state.editing_server && server.id == this.state.editing_server.id) { return (this.focus_element = c)} value={ this.state.editing_server.uri } onChange={function(event) { var server = this.state.editing_server; server.uri = event.target.value; this.setState({ editing_server: server, }); }.bind(this)} />; } else { return { server.uri }; } }, sortServerURI: function(a, b) { return a.uri.localeCompare(b.uri); }, renderServerToken: function(server) { if (this.state.editing_server && server.id == this.state.editing_server.id) { return ; } else { return { server.token }; } }, sortServerToken: function(a, b) { return a.token.localeCompare(b.token); }, renderServerInfo: function(server) { if (!this.state.info[server.id]) { return No info!; } if (this.state.info[server.id].loading) { return ( <> {' querying server for info...'} ); } if (this.state.info[server.id].status == 'badauth') { return ( Invalid auth token provided! ); } if (this.state.info[server.id].status == 'error') { return ( Error requesting server info! ); } return ( <>
{this.state.info[server.id].name} {' administered by '} {this.state.info[server.id].email}
{ this.state.info[server.id].status == 'badversion' ? This server supports an incompatible version of the API! : null } ); }, renderServerAllowedData: function(server) { if (this.state.editing_server && server.id == this.state.editing_server.id) { return ( <>
); } else { if (!server.allow_stats && !server.allow_scores) { return remote data fetching disabled; } var enabled = []; if (server.allow_stats) { enabled.push('play statistics'); } if (server.allow_scores) { enabled.push('rivals'); enabled.push('scores'); } return { enabled.join(', ') }; } }, render: function() { return (

Authorized Clients

Add Client

Name

Remote Servers

Add Remote Server

Name Token
); }, }); ReactDOM.render( React.createElement(api_management, null), document.getElementById('content') );