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.1KB

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