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

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