RebuildIndexControls.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. const header = isRebuildingCompleted ? 'Completed' : `Processing.. (${skip} skips)`;
  49. return (
  50. <ProgressBar
  51. header={header}
  52. currentCount={current}
  53. totalCount={total}
  54. />
  55. );
  56. }
  57. render() {
  58. const { t, isNormalized, isRebuildingProcessing } = this.props;
  59. const isEnabled = isNormalized && !isRebuildingProcessing;
  60. return (
  61. <>
  62. { this.renderProgressBar() }
  63. <button
  64. type="submit"
  65. className="btn btn-primary"
  66. onClick={() => { this.props.onRebuildingRequested() }}
  67. disabled={!isEnabled}
  68. >
  69. { t('full_text_search_management.rebuild_button') }
  70. </button>
  71. <p className="form-text text-muted">
  72. { t('full_text_search_management.rebuild_description_1') }<br />
  73. { t('full_text_search_management.rebuild_description_2') }<br />
  74. </p>
  75. </>
  76. );
  77. }
  78. }
  79. /**
  80. * Wrapper component for using unstated
  81. */
  82. const RebuildIndexControlsWrapper = withUnstatedContainers(RebuildIndexControls, [AppContainer, AdminSocketIoContainer]);
  83. RebuildIndexControls.propTypes = {
  84. t: PropTypes.func.isRequired, // i18next
  85. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  86. adminSocketIoContainer: PropTypes.instanceOf(AdminSocketIoContainer).isRequired,
  87. isRebuildingProcessing: PropTypes.bool.isRequired,
  88. isRebuildingCompleted: PropTypes.bool.isRequired,
  89. isNormalized: PropTypes.bool,
  90. onRebuildingRequested: PropTypes.func.isRequired,
  91. };
  92. export default withTranslation()(RebuildIndexControlsWrapper);