1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/unleash-server/public/js/unleash.jsx

305 lines
8.7 KiB
React
Raw Normal View History

2014-10-21 18:57:05 +02:00
/** @jsx React.DOM */
/* jshint quotmark:false */
// Unleash
// - Menu
// - FeatureList
// - UnsavedFeature
// - SavedFeature
//
2014-10-21 18:57:05 +02:00
var Timer = function(cb, interval) {
this.cb = cb;
this.interval = interval;
this.timerId = null;
};
Timer.prototype.start = function() {
if (this.timerId != null) {
console.warn("timer already started");
}
console.log('starting timer');
this.timerId = setInterval(this.cb, this.interval);
};
Timer.prototype.stop = function() {
if (this.timerId == null) {
console.warn('no timer running');
} else {
console.log('stopping timer');
clearInterval(this.timerId);
this.timerId = null;
}
};
var Menu = React.createClass({
2014-10-24 12:55:12 +02:00
render: function() { return (
<nav className='navbar navbar-default' role='navigation'>
<div className='container'>
<a className='navbar-brand' href='#'>Unleash</a>
</div>
</nav>
);
}
});
2014-10-23 14:30:02 +02:00
var UnsavedFeature = React.createClass({
2014-10-24 11:54:59 +02:00
render: function() {
return (
2014-10-24 13:31:42 +02:00
<div className="bg-info new-feature-form row">
2014-10-24 11:54:59 +02:00
<form className="form-inline" role="form" ref="form">
2014-10-24 13:31:42 +02:00
<div className="checkbox col-md-1 text-right">
<input ref="enabled" type="checkbox" defaultValue={this.props.feature.enabled} />
</div>
<div className="form-group col-md-4">
2014-10-24 11:54:59 +02:00
<input
type="text"
2014-10-24 13:31:42 +02:00
className="form-control"
2014-10-24 11:54:59 +02:00
id="name"
ref="name"
defaultValue={this.props.feature.name}
placeholder="Enter name" />
2014-10-24 12:04:17 +02:00
<input className="form-control"
type="text"
ref="description"
2014-10-24 11:54:59 +02:00
placeholder="Enter description" />
</div>
2014-10-24 13:31:42 +02:00
<div className="form-group col-md-1 col-md-offset-5">
<select id="strategy"
ref="strategy"
className=""
defaultValue={this.props.feature.strategy}>
2014-10-24 11:54:59 +02:00
<option value="default">default</option>
</select>
</div>
2014-10-24 13:31:42 +02:00
<div className="form-group col-md-1">
<button type="submit" className="btn btn-primary btn-xs" onClick={this.saveFeature}>
Save
</button>
2014-10-24 11:54:59 +02:00
</div>
</form>
</div>
);
},
saveFeature: function(e) {
e.preventDefault();
this.props.feature.name = this.refs.name.getDOMNode().value;
this.props.feature.description = this.refs.description.getDOMNode().value;
this.props.feature.strategy = this.refs.strategy.getDOMNode().value;
this.props.feature.enabled = this.refs.enabled.getDOMNode().checked;
this.props.onSubmit(this.props.feature);
}
2014-10-23 14:30:02 +02:00
});
var SavedFeature = React.createClass({
2014-10-24 10:31:36 +02:00
onChange: function(event) {
this.props.onChange({
name: this.props.feature.name,
field: 'enabled',
value: event.target.checked
});
},
render: function() {
return (
2014-10-24 11:42:17 +02:00
<div className='row'>
2014-10-24 13:31:42 +02:00
<div className='col-md-1 text-right'>
2014-10-24 12:00:33 +02:00
<input
type='checkbox'
checked={this.props.feature.enabled}
onChange={this.onChange} />
</div>
<div
2014-10-24 13:31:42 +02:00
className='col-md-4'
2014-10-24 12:00:33 +02:00
title='{this.props.feature.description}'>{this.props.feature.name}</div>
2014-10-24 13:31:42 +02:00
<div className='col-md-2 col-md-offset-5'>
2014-10-24 12:04:17 +02:00
{this.props.feature.strategy}
</div>
2014-10-24 10:31:36 +02:00
</div>
);
}
});
2014-10-21 18:57:05 +02:00
var FeatureList = React.createClass({
render: function() {
var featureNodes = [];
2014-10-23 16:00:13 +02:00
2014-10-24 11:54:59 +02:00
this.props.unsavedFeatures.forEach(function(feature, idx) {
var key = 'new-' + idx;
featureNodes.push(
<UnsavedFeature
key={key}
feature={feature}
onSubmit={this.props.onFeatureSubmit} />
);
2014-10-24 10:31:36 +02:00
}.bind(this));
2014-10-23 16:52:41 +02:00
this.props.savedFeatures.forEach(function(feature) {
2014-10-24 10:05:15 +02:00
featureNodes.push(
2014-10-24 10:31:36 +02:00
<SavedFeature
key={feature.name}
feature={feature}
onChange={this.props.onFeatureChanged} />
2014-10-24 10:05:15 +02:00
);
2014-10-24 10:31:36 +02:00
}.bind(this));
2014-10-23 16:52:41 +02:00
2014-10-24 10:40:41 +02:00
return (
2014-10-24 13:31:42 +02:00
<div className="container">
2014-10-24 13:02:53 +02:00
<div className='panel panel-primary'>
<div className='panel-heading'>
<h3 className='panel-title'>Features</h3>
<div className='text-right'>
<button type="button"
2014-10-24 13:31:42 +02:00
className="btn btn-default btn-sm"
2014-10-24 13:02:53 +02:00
onClick={this.props.onNewFeature}>
<span className="glyphicon glyphicon-plus"></span> New
</button>
</div>
2014-10-24 10:40:41 +02:00
</div>
2014-10-24 13:02:53 +02:00
<div className='panel-body'>
{featureNodes}
</div>
</div>
</div>
2014-10-24 10:40:41 +02:00
);
}
2014-10-23 16:00:13 +02:00
});
2014-10-24 10:49:10 +02:00
var ErrorMessages = React.createClass({
render: function() {
if (!this.props.errors.length) {
return <div/>;
}
var errorNodes = this.props.errors.map(function(e) {
2014-10-24 10:59:40 +02:00
return (<li key={e}>{e}</li>);
2014-10-24 10:49:10 +02:00
});
return (
<div className="alert alert-danger" role="alert">
<ul>{errorNodes}</ul>
</div>
);
}
});
var Unleash = React.createClass({
2014-10-21 18:57:05 +02:00
getInitialState: function() {
2014-10-24 10:49:10 +02:00
return {
savedFeatures: [],
unsavedFeatures: [],
errors: [],
poller: null
2014-10-24 10:49:10 +02:00
};
2014-10-21 18:57:05 +02:00
},
componentDidMount: function () {
2014-10-23 15:30:38 +02:00
this.loadFeaturesFromServer();
this.state.timer = new Timer(this.loadFeaturesFromServer, this.props.pollInterval);
this.state.timer.start();
2014-10-23 15:30:38 +02:00
},
loadFeaturesFromServer: function () {
reqwest('features').then(this.setFeatures, this.handleError);
},
setFeatures: function (data) {
this.setState({savedFeatures: data.features});
2014-10-21 18:57:05 +02:00
},
handleError: function (error) {
2014-10-24 10:59:40 +02:00
this.state.errors.push(error);
2014-10-23 15:16:09 +02:00
},
2014-10-24 10:05:15 +02:00
updateFeature: function (changeRequest) {
2014-10-24 10:31:36 +02:00
var newFeatures = this.state.savedFeatures;
newFeatures.forEach(function(f){
if(f.name === changeRequest.name) {
f[changeRequest.field] = changeRequest.value;
}
});
this.setState({features: newFeatures});
this.state.timer.stop();
2014-10-24 10:31:36 +02:00
reqwest({
url: 'features/' + changeRequest.name,
method: 'patch',
type: 'json',
contentType: 'application/json',
data: JSON.stringify(changeRequest)
}).then(function() {
// all good
this.state.timer.start();
}.bind(this), this.handleError);
2014-10-24 10:05:15 +02:00
},
createFeature: function (feature) {
2014-10-24 11:54:59 +02:00
var unsaved = [], state = this.state;
this.state.unsavedFeatures.forEach(function(f) {
// TODO: make sure we don't overwrite an existing feature
if (f.name === feature.name) {
state.savedFeatures.unshift(f);
} else {
unsaved.push(f);
}
});
this.setState({unsavedFeatures: unsaved});
reqwest({
url: 'features',
method: 'post',
type: 'json',
contentType: 'application/json',
data: JSON.stringify(feature)
}).then(function(r) {
console.log(r);
}.bind(this), this.handleError);
},
newFeature: function() {
var blankFeature = {
name: '',
enabled: false,
strategy: 'default',
parameters: {}
};
this.state.unsavedFeatures.push(blankFeature);
this.forceUpdate();
2014-10-24 10:05:15 +02:00
},
render: function() {
2014-10-21 18:57:05 +02:00
return (
<div>
2014-10-24 10:31:36 +02:00
<Menu />
2014-10-24 10:49:10 +02:00
<ErrorMessages errors={this.state.errors} />
2014-10-24 10:40:41 +02:00
<FeatureList
unsavedFeatures={this.state.unsavedFeatures}
savedFeatures={this.state.savedFeatures}
onFeatureChanged={this.updateFeature}
2014-10-24 11:54:59 +02:00
onFeatureSubmit={this.createFeature}
onNewFeature={this.newFeature}
/>
2014-10-21 18:57:05 +02:00
</div>
);
}
});
2014-10-21 18:57:05 +02:00
React.renderComponent(
<Unleash pollInterval={5000} />,
2014-10-21 18:57:05 +02:00
document.getElementById('content')
);