1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00

Various react stuff.

This commit is contained in:
Jari Bakken 2014-10-23 16:52:41 +02:00
parent b395d411a6
commit db0fee0060

View File

@ -21,17 +21,9 @@
var FeatureEditor = React.createClass({ var FeatureEditor = React.createClass({
getInitialState: function () {
return {name: '', description: '', strategy: 'Default'};
},
handleNameChange: function(e) { this.setState({name: e.target.value.trim()}); },
handleDescriptionChange: function(e) { this.setState({description: e.target.value.trim()}); },
handleStrategyChange: function(e) { this.setState({strategy: e.target.value.trim()}); },
handleSubmit: function(e) { handleSubmit: function(e) {
e.preventDefault(); e.preventDefault();
this.props.onFeatureSubmit(this.state); this.props.onSubmit(this.props.feature);
return; return;
}, },
@ -39,20 +31,19 @@ var FeatureEditor = React.createClass({
return ( return (
<form className="form-horizontal" onSubmit={this.handleSubmit}> <form className="form-horizontal" onSubmit={this.handleSubmit}>
<fieldset> <fieldset>
<legend>Edit/new feature</legend>
<legend>Add a new feature</legend>
<div className="control-group"> <div className="control-group">
<label className="control-label" htmlFor="name">Name </label> <label className="control-label" htmlFor="name">Name</label>
<div className="controls"> <div className="controls">
<input <input
id="name" id="name"
name="name"
type="text" type="text"
placeholder="Superfeature" placeholder="Superfeature"
className="input-large" className="input-large"
required="" required=""
onChange={this.handleNameChange} value={this.props.feature.name} />
value={this.state.name} />
<p className="help-block">Give the feature a name</p> <p className="help-block">Give the feature a name</p>
</div> </div>
</div> </div>
@ -62,11 +53,11 @@ var FeatureEditor = React.createClass({
<div className="controls"> <div className="controls">
<input <input
id="description" id="description"
name="description"
type="text" type="text"
placeholder="It does this and that " placeholder="It does this and that "
className="input-large" className="input-large"
onChange={this.handleDescriptionChange} value={this.props.feature.description} />
value={this.state.description} />
<p className="help-block">Describe the feature</p> <p className="help-block">Describe the feature</p>
</div> </div>
</div> </div>
@ -77,8 +68,7 @@ var FeatureEditor = React.createClass({
<select <select
id="strategy" id="strategy"
className="input-large" className="input-large"
onChange={this.handleStrategyChange} value={this.props.feature.strategy}>
value={this.state.strategy}>
<option value="Default">Default</option> <option value="Default">Default</option>
</select> </select>
</div> </div>
@ -95,6 +85,8 @@ var FeatureEditor = React.createClass({
</div> </div>
</div> </div>
</fieldset> </fieldset>
<hr />
</form> </form>
); );
} }
@ -167,28 +159,52 @@ var FeatureViewer = React.createClass({
var Feature = React.createClass({ var Feature = React.createClass({
getInitialState: function() { getInitialState: function() {
return { mode: 'view' }; return { mode: 'view', error: '' };
}, },
onToggleMode: function() { onToggleMode: function() {
if (this.state.mode === 'view') { if (this.isInViewMode()) {
this.setState({mode: 'edit'}); this.setState({mode: 'edit'});
} else if (this.state.mode === 'edit') { } else if (this.isInEditMode()) {
this.setState({mode: 'view'}); this.setState({mode: 'view'});
} else { } else {
throw "invalid mode: " + this.state.mode; throw "invalid mode: " + this.state.mode;
} }
}, },
onSubmit: function() {
var isNew = false;
var cb = function(err) {
if (err) {
this.setState({error: err});
} else {
this.setState(this.getInitialState());
}
};
if (isNew) {
this.props.createFeature(this.props.feature, cb.bind(this));
} else {
this.props.updateFeature(this.props.feature, cb.bind(this));
}
},
render: function() { render: function() {
if (this.state.mode === 'view') { if (this.isInViewMode()) {
return (<FeatureViewer feature={this.props.feature} onToggleMode={this.onToggleMode} />); return (
} else if (this.state.mode === 'edit') { <FeatureViewer feature={this.props.feature} onToggleMode={this.onToggleMode} />
return (<FeatureEditor feature={this.props.feature} onToggleMode={this.onToggleMode} />); );
} else if (this.isInEditMode()) {
return (
<FeatureEditor feature={this.props.feature} onToggleMode={this.onToggleMode} onSubmit={this.onSubmit} />
);
} else { } else {
throw "invalid mode: " + this.state.mode; throw "invalid mode: " + this.state.mode;
} }
} },
isInViewMode: function() { return this.state.mode === 'view'; },
isInEditMode: function() { return this.state.mode === 'edit'; }
}); });
var FeatureList = React.createClass({ var FeatureList = React.createClass({
@ -204,14 +220,14 @@ var FeatureList = React.createClass({
}, },
loadFeaturesFromServer: function () { loadFeaturesFromServer: function () {
reqwest('/features').then(this.setFeatures); reqwest('features').then(this.setFeatures);
}, },
setFeatures: function (data) { setFeatures: function (data) {
this.setState({features: data.features}); this.setState({features: data.features});
}, },
updateFeature: function (changeRequest) { updateFeature: function (changeRequest, callback) {
var newFeatures = this.state.features; var newFeatures = this.state.features;
newFeatures.forEach(function(f){ newFeatures.forEach(function(f){
if(f.name === changeRequest.name) { if(f.name === changeRequest.name) {
@ -227,12 +243,14 @@ var FeatureList = React.createClass({
data: JSON.stringify(changeRequest) data: JSON.stringify(changeRequest)
}).then(function() { }).then(function() {
this.setState({features: newFeatures}); this.setState({features: newFeatures});
callback();
}.bind(this), function() { }.bind(this), function() {
callback('update failed');
window.alert('update failed'); window.alert('update failed');
}.bind(this)); }.bind(this));
}, },
createFeature: function (feature) { createFeature: function (feature, callback) {
reqwest({ reqwest({
url: 'features', url: 'features',
method: 'post', method: 'post',
@ -240,8 +258,9 @@ var FeatureList = React.createClass({
contentType: 'application/json', contentType: 'application/json',
data: JSON.stringify(feature) data: JSON.stringify(feature)
}).then(function() { }).then(function() {
// how do we communicate success? callback();
}.bind(this), function() { }.bind(this), function() {
callback('create failed');
window.alert('create failed'); window.alert('create failed');
}.bind(this)); }.bind(this));
}, },
@ -249,7 +268,10 @@ var FeatureList = React.createClass({
render: function () { render: function () {
var featureNodes = this.state.features.map(function (feature) { var featureNodes = this.state.features.map(function (feature) {
return ( return (
<Feature feature={feature} updateFeature={this.updateFeature} /> <Feature
feature={feature}
updateFeature={this.updateFeature}
createFeature={this.createFeature} />
); );
}.bind(this)); }.bind(this));