diff --git a/package.json b/package.json index 575dc19143..8818dbd6ad 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "nconf": "0.6.9", "pg": "3.6.1", "react": "^0.12.0", + "reflux": "^0.2.5", "reqwest": "^1.1.4", "webpack": "1.4.15", "webpack-dev-middleware": "^1.0.11" diff --git a/public/js/components/feature/FeatureTogglesComponent.jsx b/public/js/components/feature/FeatureTogglesComponent.jsx index be50960d3d..815718c066 100644 --- a/public/js/components/feature/FeatureTogglesComponent.jsx +++ b/public/js/components/feature/FeatureTogglesComponent.jsx @@ -4,33 +4,20 @@ var ErrorMessages = require('../ErrorMessages'); var FeatureList = require('./FeatureList'); var FeatureForm = require('./FeatureForm'); var FeatureStore = require('../../stores/FeatureStore'); +var FeatureStore2 = require('../../stores/FeatureStore2'); +var FeatureActions = require('../../stores/FeatureActions'); +var Reflux = require('reflux'); var FeatureTogglesComponent = React.createClass({ getInitialState: function() { return { - features: [], errors: [], createView: false, featurePoller: new Timer(this.loadFeaturesFromServer, this.props.pollInterval) }; }, - componentDidMount: function () { - this.loadFeaturesFromServer(); - this.startFeaturePoller(); - }, - - componentWillUnmount: function () { - this.stopFeaturePoller(); - }, - - loadFeaturesFromServer: function () { - FeatureStore.getFeatures().then(this.setFeatures).catch(this.handleError); - }, - - setFeatures: function (data) { - this.setState({features: data.features}); - }, + mixins: [Reflux.connect(FeatureStore2,"features")], handleError: function (error) { if (this.isClientError(error)) { @@ -78,13 +65,20 @@ var FeatureTogglesComponent = React.createClass({ }, createFeature: function (feature) { - this.stopFeaturePoller(); + //this.stopFeaturePoller(); + + FeatureActions.addToggle.triggerPromise(feature) + .then(this.cancelNewFeature) + .catch(this.handleError); + +/* FeatureStore .createFeature(feature) .then(this.cancelNewFeature) .then(this.startFeaturePoller) .catch(this.handleError); +*/ }, newFeature: function() { @@ -150,4 +144,4 @@ var FeatureTogglesComponent = React.createClass({ } }); -module.exports = FeatureTogglesComponent; \ No newline at end of file +module.exports = FeatureTogglesComponent; diff --git a/public/js/stores/FeatureActions.js b/public/js/stores/FeatureActions.js new file mode 100644 index 0000000000..8c610f4348 --- /dev/null +++ b/public/js/stores/FeatureActions.js @@ -0,0 +1,4 @@ +var Reflux = require("reflux"); +module.exports = { + addToggle: Reflux.createAction({ asyncResult: true }) +}; diff --git a/public/js/stores/FeatureStore2.js b/public/js/stores/FeatureStore2.js new file mode 100644 index 0000000000..6f4bb23ac3 --- /dev/null +++ b/public/js/stores/FeatureStore2.js @@ -0,0 +1,69 @@ +var reqwest = require('reqwest'); +var Reflux = require('reflux'); +var FeatureActions = require('./FeatureActions'); + +var TYPE = 'json'; +var CONTENT_TYPE = 'application/json'; + +function getFeatures() { + return reqwest({ + url: 'features', + method: 'get', + type: CONTENT_TYPE + }); +} + + +var _toggles = []; + +// Creates a DataStore +var FeatureStore = Reflux.createStore({ + // Initial setup + init: function() { + this.listenTo(FeatureActions.addToggle, this.onAddToggle); + + getFeatures() + .then(function(data) { + this.setToggles(JSON.parse(data.response).features); + }.bind(this)) + .catch(this.handleError); + }, + + onAddToggle: function(feature) { + reqwest({ + url: 'features', + method: 'post', + type: TYPE, + contentType: CONTENT_TYPE, + data: JSON.stringify(feature), + error: function (error) { + FeatureActions.addToggle.failed(error); + }, + success: function () { + this.setToggles(_toggles.concat([feature])); + FeatureActions.addToggle.completed(); + }.bind(this) + }); + }, + + setToggles: function(toggles) { + _toggles = toggles; + this.trigger(_toggles); + }, + + handleError: function(er) { + console.log("error: "+ er); + }, + + //getter for notes + getToggles: function() { + return _toggles; + }, + + getInitialState: function() { + return _toggles; + } + +}); + +module.exports = FeatureStore;