Skip to content

Instantly share code, notes, and snippets.

@ljack
Last active April 9, 2016 06:38
Show Gist options
  • Save ljack/6cc7a222f62dd7aba1a3a4b5ea6db7cd to your computer and use it in GitHub Desktop.
Save ljack/6cc7a222f62dd7aba1a3a4b5ea6db7cd to your computer and use it in GitHub Desktop.
React button component to simulate start/stop failure scenario
import React from "react";
// React button component to simulate start/stop failure scenario
export default const StartButton = React.createClass({
forward() {
const baseDelay = 4*1000;
switch (this.state.status) {
case 'recovering':
break;
case 'failedToStart':
this.setState({
status: "recovering"
});
setTimeout(() => {
this.setState({
status: Math.random() < 0.5 ? "stopped" : "started"
});
}, baseDelay * Math.random());
break;
case 'failedToStop':
this.setState({
status: "recovering"
});
setTimeout(() => {
this.setState({
status: Math.random() < 0.5 ? "stopped" : "started"
});
}, baseDelay * Math.random());
break;
case 'stopped':
this.setState({
status: "starting"
});
setTimeout(() => {
this.setState({
status: Math.random() < 0.5 ? "started" : "failedToStart"
});
}, baseDelay * Math.random());
break;
case 'starting':
break;
case 'started':
this.setState({
status: "stopping"
});
setTimeout(() => {
this.setState({
status: Math.random() < 0.5 ? "stopped" : "failedToStop"
});
}, baseDelay * Math.random());
break;
case 'stopping':
break;
default:
this.setState({
status: "unknown"
});
}
},
getInitialState() {
return {
status: "stopped",
started: "fa fa-border fa-stop-circle fa-lg",
starting: "fa fa-hourglass-start fa-spin fa-lg",
stopping: "fa fa-refresh fa-spin fa-lg",
stopped: "fa fa-border fa-play fa-lg",
failedToStop: "fa fa-border fa-exclamation-triangle fa-lg",
failedToStart: "fa fa-border fa-exclamation-triangle fa-lg",
recovering: "fa fa-lg fa-cog fa-spin"
}
},
toggle(evt) {
evt.preventDefault();
evt.stopPropagation();
this.forward();
},
render: function() {
const paddingStyle = {
leftPadding: "5px"
};
return (
<span>
<span id="start-button" onClick={this.toggle} title={this.state.status}><i className={this.state[this.state.status]} /> {this.state.status}</span>
</span>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment