Settings.jsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var DomainStore = require('../stores').Domains;
  2. var React = require('react');
  3. var dateToString = require('../util').dateToString;
  4. var DevModeToggle = React.createClass({
  5. getInitialState: function() {
  6. return {toggling: false};
  7. },
  8. componentWillReceiveProps: function() {
  9. this.setState({toggling: false});
  10. },
  11. toggleDevMode: function() {
  12. this.setState({toggling: true});
  13. DomainStore.setDevelopmentMode(this.props.domain, this.props.devMode * 1000 <= Date.now());
  14. },
  15. render: function() {
  16. if(this.state.toggling) {
  17. return <button className="btn btn-warning" disabled>{this.props.devMode > 0 ? 'Disabling...' : 'Enabling...'}</button>
  18. }
  19. else if(this.props.devMode * 1000 > Date.now()) {
  20. var date = new Date(this.props.devMode*1000);
  21. return (
  22. <div>
  23. <button className='btn btn-success' onClick={this.toggleDevMode}>On</button>
  24. <span> Active until {dateToString(date)}</span>
  25. </div>
  26. );
  27. }
  28. else {
  29. return <button className='btn btn-default' onClick={this.toggleDevMode}>Off</button>
  30. }
  31. }
  32. });
  33. var PurgeButton = React.createClass({
  34. getInitialState: function() {
  35. return {purging: false, failed: false};
  36. },
  37. purgeCache: function() {
  38. var self = this;
  39. this.setState({purging: true});
  40. var reset = function() {
  41. this.setState({purging: false, failed: false});
  42. }.bind(this);
  43. DomainStore.purgeCache(this.props.domain).then(function(data) {
  44. setTimeout(reset, 5*1000);
  45. }, function(error) {
  46. self.setState({failed: true});
  47. setTimeout(reset, 10*1000);
  48. });
  49. },
  50. render: function() {
  51. if(this.state.failed) {
  52. return <button className="btn btn-danger" disabled>Purge failed</button>
  53. }
  54. else if(this.state.purging) {
  55. return <button className="btn btn-warning" disabled>Purging...</button>
  56. }
  57. else {
  58. return <button className="btn btn-success" onClick={this.purgeCache}>Purge</button>
  59. }
  60. }
  61. });
  62. var Settings = React.createClass({
  63. render: function() {
  64. if(!this.props.settings.val()) {
  65. return <div className="alert alert-info">Loading...</div>
  66. }
  67. return (
  68. <div>
  69. <table className="table">
  70. <tr>
  71. <td>Development Mode</td>
  72. <td><DevModeToggle domain={this.props.domain} devMode={this.props.settings.development_mode.val()} /></td>
  73. </tr>
  74. <tr>
  75. <td>Purge Cache</td>
  76. <td><PurgeButton domain={this.props.domain} /></td>
  77. </tr>
  78. </table>
  79. </div>
  80. );
  81. }
  82. });
  83. module.exports = Settings;