Add config reset button

This commit is contained in:
Itay Mizeretz 2017-09-21 17:32:36 +03:00
parent bc0cace866
commit 71c574b5f7
3 changed files with 37 additions and 4 deletions

View File

@ -15,6 +15,9 @@ class MonkeyConfiguration(flask_restful.Resource):
def post(self): def post(self):
config_json = json.loads(request.data) config_json = json.loads(request.data)
if config_json.has_key('reset'):
ConfigService.reset_config()
else:
ConfigService.update_config(config_json) ConfigService.update_config(config_json)
return self.get() return self.get()

View File

@ -816,6 +816,10 @@ class ConfigService:
def init_config(): def init_config():
if ConfigService.get_config() != {}: if ConfigService.get_config() != {}:
return return
ConfigService.reset_config()
@staticmethod
def reset_config():
config = ConfigService.get_default_config() config = ConfigService.get_default_config()
ConfigService.set_server_ips_in_config(config) ConfigService.set_server_ips_in_config(config)
ConfigService.update_config(config) ConfigService.update_config(config)

View File

@ -14,6 +14,7 @@ class ConfigurePageComponent extends React.Component {
schema: {}, schema: {},
configuration: {}, configuration: {},
saved: false, saved: false,
reset: false,
sections: [], sections: [],
selectedSection: 'basic' selectedSection: 'basic'
}; };
@ -46,6 +47,7 @@ class ConfigurePageComponent extends React.Component {
.then(res => { .then(res => {
this.setState({ this.setState({
saved: true, saved: true,
reset: false,
schema: res.schema, schema: res.schema,
configuration: res.configuration configuration: res.configuration
}); });
@ -74,9 +76,27 @@ class ConfigurePageComponent extends React.Component {
}); });
}; };
resetConfig = () => {
fetch('/api/configuration',
{
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({'reset': true})
})
.then(res => res.json())
.then(res => {
this.setState({
reset: true,
saved: false,
schema: res.schema,
configuration: res.configuration
});
this.props.onStatusChange();
});
};
render() { render() {
let displayedSchema = {}; let displayedSchema = {};
if (this.state.schema.hasOwnProperty('properties')) { if (this.state.schema.hasOwnProperty('properties')) {
displayedSchema = this.state.schema["properties"][this.state.selectedSection]; displayedSchema = this.state.schema["properties"][this.state.selectedSection];
displayedSchema["definitions"] = this.state.schema["definitions"]; displayedSchema["definitions"] = this.state.schema["definitions"];
@ -100,12 +120,18 @@ class ConfigurePageComponent extends React.Component {
onSubmit={this.onSubmit} onSubmit={this.onSubmit}
onChange={this.onChange} /> onChange={this.onChange} />
: ''} : ''}
<a onClick={this.resetConfig} className="btn btn-danger btn-lg">Reset to defaults</a>
<div className="alert alert-info"> <div className="alert alert-info">
<i className="glyphicon glyphicon-info-sign" style={{'marginRight': '5px'}}/> <i className="glyphicon glyphicon-info-sign" style={{'marginRight': '5px'}}/>
This configuration will only apply to new infections. This configuration will only apply to new infections.
</div> </div>
{ this.state.reset ?
<div className="alert alert-info">
<i className="glyphicon glyphicon-info-sign" style={{'marginRight': '5px'}}/>
Configuration reset successfully.
</div>
: ''}
{ this.state.saved ? { this.state.saved ?
<div className="alert alert-info"> <div className="alert alert-info">
<i className="glyphicon glyphicon-info-sign" style={{'marginRight': '5px'}}/> <i className="glyphicon glyphicon-info-sign" style={{'marginRight': '5px'}}/>