Settings.jsx 2.5 KB

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