Settings.jsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var cloudflare = require('../cloudflare');
  2. var React = require('react');
  3. var DevModeToggle = React.createClass({
  4. render: function() {
  5. if(this.props.devMode > 0) {
  6. var date = new Date(this.props.devMode*1000);
  7. var dateString = date.getFullYear()+'/'+(1+date.getMonth())+'/'+date.getDay()+' '+date.getHours()+':'+date.getMinutes();
  8. return (
  9. <div>
  10. <button className='btn btn-success' onClick={this.props.onClick}>On</button>
  11. <span> Active until {dateString}</span>
  12. </div>
  13. );
  14. }
  15. else {
  16. return <button className='btn btn-default' onClick={this.props.onClick}>Off</button>
  17. }
  18. }
  19. });
  20. var PurgeButton = React.createClass({
  21. getInitialState: function() {
  22. return {purging: false};
  23. },
  24. purgeCache: function() {
  25. this.setState({purging: true});
  26. var reset = function() {
  27. this.setState({purging: false});
  28. }.bind(this);
  29. cloudflare.purge_cache(this.props.domain).then(function(data) {
  30. var timeout = data.attributes.cooldown;
  31. setTimeout(reset, timeout*1000);
  32. });
  33. },
  34. render: function() {
  35. if(this.state.purging) {
  36. return <button className="btn btn-warning" disabled>Purging...</button>
  37. }
  38. else {
  39. return <button className="btn btn-success" onClick={this.purgeCache}>Purge</button>
  40. }
  41. }
  42. });
  43. var Settings = React.createClass({
  44. getInitialState: function() {
  45. return {settings: {}};
  46. },
  47. componentDidMount: function() {
  48. this.reload();
  49. },
  50. reload: function() {
  51. return cloudflare.settings(this.props.domain).then(function(data) {
  52. this.setState({settings: data.response.result.objs[0]});
  53. }.bind(this));
  54. },
  55. toggleDevMode: function() {
  56. cloudflare.set_devmode(this.props.domain, this.state.settings.dev_mode == 0).then(this.reload);
  57. },
  58. render: function() {
  59. return (
  60. <div>
  61. <table className="table">
  62. <tr>
  63. <td>Development Mode</td>
  64. <td><DevModeToggle devMode={this.state.settings.dev_mode} onClick={this.toggleDevMode} /></td>
  65. </tr>
  66. <tr>
  67. <td>Purge Cache</td>
  68. <td><PurgeButton domain={this.props.domain} /></td>
  69. </tr>
  70. </table>
  71. </div>
  72. );
  73. }
  74. });
  75. module.exports = Settings;