/*** @jsx React.DOM */
var api_management = React.createClass({
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 ;
}
} else {
return (