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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. current: this.props.start,
  62. time: Math.abs(this.props.end - this.props.start)
  63. };
  64. }
  65. /**
  66. * React Component method that executes once component is mounted.
  67. *
  68. * @inheritdoc
  69. * @returns {void}
  70. * @protected
  71. */
  72. componentDidMount() {
  73. AJS.progressBars.update('#reloadProgressBar', 0);
  74. const intervalId
  75. = setInterval(
  76. () => {
  77. if (this.state.current === this.props.end) {
  78. clearInterval(intervalId);
  79. this.props.onFinish();
  80. } else {
  81. this.setState((prevState, props) => {
  82. return {
  83. current: prevState.current + props.step
  84. };
  85. });
  86. }
  87. },
  88. Math.abs(this.props.interval) * 1000);
  89. }
  90. /**
  91. * React Component method that executes once component is updated.
  92. *
  93. * @inheritdoc
  94. * @returns {void}
  95. * @protected
  96. */
  97. componentDidUpdate() {
  98. AJS.progressBars.update(
  99. '#reloadProgressBar',
  100. Math.abs(this.state.current - this.props.start)
  101. / this.state.time);
  102. }
  103. /**
  104. * Implements React's {@link Component#render()}.
  105. *
  106. * @inheritdoc
  107. * @returns {ReactElement|null}
  108. * @public
  109. */
  110. render() {
  111. return (
  112. <div>
  113. <div
  114. className = 'aui-progress-indicator'
  115. id = 'reloadProgressBar'>
  116. <span className = 'aui-progress-indicator-value' />
  117. </div>
  118. <span
  119. className = 'reload_overlay_text'
  120. id = 'reloadSeconds'>
  121. {
  122. this.state.current
  123. }
  124. <span data-i18n = 'dialog.conferenceReloadTimeLeft' />
  125. </span>
  126. </div>
  127. );
  128. }
  129. }