RebuildIndexControls.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { withUnstatedContainers } from '../../UnstatedUtils';
  5. import AppContainer from '../../../services/AppContainer';
  6. import AdminSocketIoContainer from '../../../services/AdminSocketIoContainer';
  7. import ProgressBar from '../Common/ProgressBar';
  8. class RebuildIndexControls extends React.Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. total: 0,
  13. current: 0,
  14. skip: 0,
  15. };
  16. }
  17. componentDidMount() {
  18. this.initWebSockets();
  19. }
  20. initWebSockets() {
  21. const socket = this.props.adminSocketIoContainer.getSocket();
  22. socket.on('addPageProgress', (data) => {
  23. this.setState({
  24. total: data.totalCount,
  25. current: data.count,
  26. skip: data.skipped,
  27. });
  28. });
  29. socket.on('finishAddPage', (data) => {
  30. this.setState({
  31. total: data.totalCount,
  32. current: data.count,
  33. skip: data.skipped,
  34. });
  35. });
  36. }
  37. renderProgressBar() {
  38. const {
  39. isRebuildingProcessing, isRebuildingCompleted,
  40. } = this.props;
  41. const {
  42. total, current, skip,
  43. } = this.state;
  44. const showProgressBar = isRebuildingProcessing || isRebuildingCompleted;
  45. if (!showProgressBar) {
  46. return null;
  47. }
  48. function getCompletedLabel() {
  49. const completedLabel = skip === 0 ? 'Completed' : `Done (${skip} skips)`;
  50. return completedLabel;
  51. }
  52. function getSkipLabel() {
  53. return `Processing.. (${skip} skips)`;
  54. }
  55. const header = isRebuildingCompleted ? getCompletedLabel() : getSkipLabel();
  56. return (
  57. <ProgressBar
  58. header={header}
  59. currentCount={current}
  60. totalCount={total}
  61. />
  62. );
  63. }
  64. render() {
  65. const { t, isNormalized, isRebuildingProcessing } = this.props;
  66. const isEnabled = isNormalized && !isRebuildingProcessing;
  67. return (
  68. <>
  69. { this.renderProgressBar() }
  70. <button
  71. type="submit"
  72. className="btn btn-primary"
  73. onClick={() => { this.props.onRebuildingRequested() }}
  74. disabled={!isEnabled}
  75. >
  76. { t('full_text_search_management.rebuild_button') }
  77. </button>
  78. <p className="form-text text-muted">
  79. { t('full_text_search_management.rebuild_description_1') }<br />
  80. { t('full_text_search_management.rebuild_description_2') }<br />
  81. </p>
  82. </>
  83. );
  84. }
  85. }
  86. /**
  87. * Wrapper component for using unstated
  88. */
  89. const RebuildIndexControlsWrapper = withUnstatedContainers(RebuildIndexControls, [AppContainer, AdminSocketIoContainer]);
  90. RebuildIndexControls.propTypes = {
  91. t: PropTypes.func.isRequired, // i18next
  92. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  93. adminSocketIoContainer: PropTypes.instanceOf(AdminSocketIoContainer).isRequired,
  94. isRebuildingProcessing: PropTypes.bool.isRequired,
  95. isRebuildingCompleted: PropTypes.bool.isRequired,
  96. isNormalized: PropTypes.bool,
  97. onRebuildingRequested: PropTypes.func.isRequired,
  98. };
  99. export default withTranslation()(RebuildIndexControlsWrapper);