2015-03-17 20:29:03 +01:00
|
|
|
var React = require('react');
|
|
|
|
var StrategyList = require('./StrategyList');
|
|
|
|
var StrategyForm = require('./StrategyForm');
|
|
|
|
var strategyStore = require('../../stores/StrategyStore');
|
|
|
|
var ErrorActions = require('../../stores/ErrorActions');
|
2014-11-01 14:52:37 +01:00
|
|
|
|
2014-11-03 14:00:45 +01:00
|
|
|
var StrategiesComponent = React.createClass({
|
2014-11-01 14:52:37 +01:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
createView: false,
|
2015-03-17 20:29:03 +01:00
|
|
|
strategies: []
|
2014-11-01 14:52:37 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount: function () {
|
2014-12-09 09:22:54 +01:00
|
|
|
this.fetchStrategies();
|
|
|
|
},
|
|
|
|
|
2015-03-17 20:29:03 +01:00
|
|
|
fetchStrategies: function() {
|
2014-12-09 09:22:54 +01:00
|
|
|
strategyStore.getStrategies()
|
2015-03-17 20:29:03 +01:00
|
|
|
.then(function(res) {
|
|
|
|
this.setState({strategies: res.strategies});
|
|
|
|
}.bind(this))
|
|
|
|
.catch(this.initError);
|
2014-12-09 09:22:54 +01:00
|
|
|
|
2014-11-01 15:53:14 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
initError: function() {
|
2014-11-03 21:37:40 +01:00
|
|
|
this.onError("Could not load inital strategies from server");
|
2014-11-01 15:53:14 +01:00
|
|
|
},
|
|
|
|
|
2014-11-03 21:37:40 +01:00
|
|
|
onError: function(error) {
|
2015-03-17 20:29:03 +01:00
|
|
|
ErrorActions.error(error);
|
2014-11-01 15:53:14 +01:00
|
|
|
},
|
|
|
|
|
2014-11-03 21:37:40 +01:00
|
|
|
onNewStrategy: function() {
|
2014-11-01 15:53:14 +01:00
|
|
|
this.setState({createView: true});
|
|
|
|
},
|
|
|
|
|
2014-11-03 21:37:40 +01:00
|
|
|
onCancelNewStrategy: function() {
|
2014-11-01 15:53:14 +01:00
|
|
|
this.setState({createView: false});
|
2014-11-01 14:52:37 +01:00
|
|
|
},
|
|
|
|
|
2014-11-03 21:37:40 +01:00
|
|
|
onSave: function(strategy) {
|
2014-11-25 15:28:08 +01:00
|
|
|
function handleSuccess() {
|
|
|
|
var strategies = this.state.strategies.concat([strategy]);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
createView: false,
|
|
|
|
strategies: strategies
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log("Saved strategy: ", strategy);
|
|
|
|
}
|
|
|
|
|
|
|
|
strategyStore.createStrategy(strategy)
|
|
|
|
.then(handleSuccess.bind(this))
|
|
|
|
.catch(this.onError);
|
2014-11-02 22:39:52 +01:00
|
|
|
},
|
|
|
|
|
2014-12-09 09:22:54 +01:00
|
|
|
onRemove: function(strategy) {
|
|
|
|
strategyStore.removeStrategy(strategy)
|
2015-03-17 20:29:03 +01:00
|
|
|
.then(this.fetchStrategies)
|
|
|
|
.catch(this.onError);
|
2014-12-09 09:22:54 +01:00
|
|
|
},
|
|
|
|
|
2014-11-01 14:52:37 +01:00
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<div>
|
2015-03-17 20:29:03 +01:00
|
|
|
{this.state.createView ? this.renderCreateView() : this.renderCreateButton()}
|
2014-11-01 17:17:07 +01:00
|
|
|
<hr />
|
2015-03-17 20:29:03 +01:00
|
|
|
<StrategyList
|
|
|
|
strategies={this.state.strategies}
|
|
|
|
onRemove={this.onRemove} />
|
2014-11-01 14:52:37 +01:00
|
|
|
</div>
|
2015-03-17 20:29:03 +01:00
|
|
|
);
|
2014-11-01 15:53:14 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
renderCreateView: function() {
|
2015-03-17 20:29:03 +01:00
|
|
|
return (
|
|
|
|
<StrategyForm
|
|
|
|
onCancelNewStrategy={this.onCancelNewStrategy}
|
|
|
|
onSave={this.onSave}
|
|
|
|
/>);
|
2014-11-01 17:17:07 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
renderCreateButton: function() {
|
|
|
|
return (
|
2015-03-17 20:29:03 +01:00
|
|
|
<button className="mal" onClick={this.onNewStrategy}>
|
|
|
|
Create strategy
|
|
|
|
</button>
|
2014-11-03 13:54:06 +01:00
|
|
|
);
|
2014-11-01 14:52:37 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-17 20:29:03 +01:00
|
|
|
module.exports = StrategiesComponent;
|