You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReloadTimer.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import React, { Component } from 'react';
  2. import { translate } from '../../base/i18n';
  3. declare var AJS: Object;
  4. /**
  5. * Implements a React Component for the reload timer. Starts counter from
  6. * props.start, adds props.step to the current value on every props.interval
  7. * seconds until the current value reaches props.end. Also displays progress
  8. * bar.
  9. */
  10. class ReloadTimer extends Component {
  11. /**
  12. * ReloadTimer component's property types.
  13. *
  14. * @static
  15. */
  16. static propTypes = {
  17. /**
  18. * The end of the timer. When this.state.current reaches this value the
  19. * timer will stop and call onFinish function.
  20. *
  21. * @public
  22. * @type {number}
  23. */
  24. end: React.PropTypes.number,
  25. /**
  26. * The interval in sec for adding this.state.step to this.state.current.
  27. *
  28. * @public
  29. * @type {number}
  30. */
  31. interval: React.PropTypes.number,
  32. /**
  33. * The function that will be executed when timer finish (when
  34. * this.state.current === this.props.end)
  35. */
  36. onFinish: React.PropTypes.func,
  37. /**
  38. * The start of the timer. The initial value for this.state.current.
  39. *
  40. * @public
  41. * @type {number}
  42. */
  43. start: React.PropTypes.number,
  44. /**
  45. * The value which will be added to this.state.current on every step.
  46. *
  47. * @public
  48. * @type {number}
  49. */
  50. step: React.PropTypes.number,
  51. /**
  52. * The function to translate human-readable text.
  53. *
  54. * @public
  55. * @type {Function}
  56. */
  57. t: React.PropTypes.func
  58. }
  59. /**
  60. * Initializes a new ReloadTimer instance.
  61. *
  62. * @param {Object} props - The read-only properties with which the new
  63. * instance is to be initialized.
  64. * @public
  65. */
  66. constructor(props) {
  67. super(props);
  68. this.state = {
  69. /**
  70. * Current value(time) of the timer.
  71. *
  72. * @type {number}
  73. */
  74. current: this.props.start,
  75. /**
  76. * The absolute value of the time from the start of the timer until
  77. * the end of the timer.
  78. *
  79. * @type {number}
  80. */
  81. time: Math.abs(this.props.end - this.props.start)
  82. };
  83. }
  84. /**
  85. * React Component method that executes once component is mounted.
  86. *
  87. * @inheritdoc
  88. * @returns {void}
  89. * @protected
  90. */
  91. componentDidMount() {
  92. AJS.progressBars.update('#reloadProgressBar', 0);
  93. const intervalId
  94. = setInterval(
  95. () => {
  96. if (this.state.current === this.props.end) {
  97. clearInterval(intervalId);
  98. this.props.onFinish();
  99. } else {
  100. this.setState((prevState, props) => {
  101. return {
  102. current: prevState.current + props.step
  103. };
  104. });
  105. }
  106. },
  107. Math.abs(this.props.interval) * 1000);
  108. }
  109. /**
  110. * React Component method that executes once component is updated.
  111. *
  112. * @inheritdoc
  113. * @returns {void}
  114. * @protected
  115. */
  116. componentDidUpdate() {
  117. AJS.progressBars.update(
  118. '#reloadProgressBar',
  119. Math.abs(this.state.current - this.props.start)
  120. / this.state.time);
  121. }
  122. /**
  123. * Implements React's {@link Component#render()}.
  124. *
  125. * @inheritdoc
  126. * @returns {ReactElement|null}
  127. * @public
  128. */
  129. render() {
  130. const { t } = this.props;
  131. return (
  132. <div>
  133. <div
  134. className = 'aui-progress-indicator'
  135. id = 'reloadProgressBar'>
  136. <span className = 'aui-progress-indicator-value' />
  137. </div>
  138. <span className = 'reload_overlay_text'>
  139. {
  140. this.state.current
  141. }
  142. <span>
  143. { t('dialog.conferenceReloadTimeLeft') }
  144. </span>
  145. </span>
  146. </div>
  147. );
  148. }
  149. }
  150. export default translate(ReloadTimer);