Settings.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. var newValue = this.props.setting.timestamp * 1000 <= Date.now();
  14. DomainStore.settingChange(this.props.domain, 'development_mode', newValue ? 'on' : 'off');
  15. },
  16. render: function() {
  17. var active = this.props.setting.timestamp * 1000 > Date.now();
  18. if(this.state.toggling) {
  19. return <button className="btn btn-warning" disabled>{active ? 'Disabling...' : 'Enabling...'}</button>
  20. }
  21. else if(active) {
  22. var date = new Date(this.props.setting.timestamp*1000);
  23. return (
  24. <div>
  25. <button className='btn btn-success' onClick={this.toggleDevMode}>On</button>
  26. <span> Active until {dateToString(date)}</span>
  27. </div>
  28. );
  29. }
  30. else {
  31. return <button className='btn btn-default' onClick={this.toggleDevMode}>Off</button>
  32. }
  33. }
  34. });
  35. var SSLToggle = React.createClass({
  36. options: [
  37. {value: 'off', name: 'Off'},
  38. {value: 'flexible', name: 'Flexible'},
  39. {value: 'full', name: 'Full'},
  40. {value: 'full_strict', name: 'Full (Strict)'}
  41. ],
  42. getInitialState: function() {
  43. return {toggling: false};
  44. },
  45. componentWillReceiveProps: function() {
  46. this.setState({toggling: false});
  47. },
  48. setSSLMode: function(event) {
  49. this.setState({toggling: true});
  50. var unToggle = function() {
  51. this.setState({toggling: false});
  52. }.bind(this);
  53. DomainStore.settingChange(this.props.domain, 'ssl', event.target.value).then(unToggle, unToggle);
  54. },
  55. render: function() {
  56. var options = this.options.map(function(opt) {
  57. return <option key={opt.value} value={opt.value}>{opt.name}</option>
  58. });
  59. var updating = <option>Updating...</option>
  60. return (
  61. <select
  62. className="form-control ssl"
  63. disabled={this.state.toggling}
  64. onChange={this.setSSLMode}
  65. value={this.props.setting.value}
  66. >
  67. {this.state.toggling ? updating : options}
  68. </select>
  69. )
  70. }
  71. });
  72. var PurgeButton = React.createClass({
  73. getInitialState: function() {
  74. return {purging: false, failed: false};
  75. },
  76. purgeCache: function() {
  77. var self = this;
  78. this.setState({purging: true});
  79. var reset = function() {
  80. this.setState({purging: false, failed: false});
  81. }.bind(this);
  82. DomainStore.purgeCache(this.props.domain).then(function(data) {
  83. setTimeout(reset, 5*1000);
  84. }, function(error) {
  85. self.setState({failed: true});
  86. setTimeout(reset, 10*1000);
  87. });
  88. },
  89. render: function() {
  90. if(this.state.failed) {
  91. return <button className="btn btn-danger" disabled>Purge failed</button>
  92. }
  93. else if(this.state.purging) {
  94. return <button className="btn btn-warning" disabled>Purging...</button>
  95. }
  96. else {
  97. return <button className="btn btn-success" onClick={this.purgeCache}>Purge</button>
  98. }
  99. }
  100. });
  101. var Settings = React.createClass({
  102. settingMap: {
  103. 'development_mode': {
  104. title: 'Development Mode',
  105. component: DevModeToggle
  106. },
  107. 'ssl': {
  108. title: 'SSL Mode',
  109. component: SSLToggle
  110. }
  111. },
  112. render: function() {
  113. if(this.props.settings.count() == 0) {
  114. return <div className="alert alert-info">Loading...</div>
  115. }
  116. var settings = this.props.settings.map(function(setting) {
  117. var opts = this.settingMap[setting.id.val()];
  118. if(opts != undefined) {
  119. return (
  120. <tr key={setting.id.val()}>
  121. <td>{opts.title}</td>
  122. <td><opts.component domain={this.props.domain} setting={setting.val()} /></td>
  123. </tr>
  124. )
  125. }
  126. return null;
  127. }.bind(this));
  128. return (
  129. <div>
  130. <table className="table">
  131. <tbody>
  132. <tr>
  133. <td>Purge Cache</td>
  134. <td><PurgeButton domain={this.props.domain} /></td>
  135. </tr>
  136. {settings}
  137. </tbody>
  138. </table>
  139. </div>
  140. );
  141. }
  142. });
  143. module.exports = Settings;