RebuildIndexControls.jsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { createSubscribedElement } from '../../UnstatedUtils';
  5. import AppContainer from '../../../services/AppContainer';
  6. import WebsocketContainer from '../../../services/WebsocketContainer';
  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.websocketContainer.getWebSocket();
  22. socket.on('admin:addPageProgress', (data) => {
  23. this.setState({
  24. ...data,
  25. });
  26. });
  27. socket.on('admin:finishAddPage', (data) => {
  28. this.setState({
  29. ...data,
  30. });
  31. });
  32. }
  33. renderProgressBar() {
  34. const {
  35. isRebuildingProcessing, isRebuildingCompleted,
  36. } = this.props;
  37. const {
  38. total, current, skip,
  39. } = this.state;
  40. const showProgressBar = isRebuildingProcessing || isRebuildingCompleted;
  41. if (!showProgressBar) {
  42. return null;
  43. }
  44. const header = isRebuildingCompleted ? 'Completed' : `Processing.. (${skip} skips)`;
  45. return (
  46. <ProgressBar
  47. header={header}
  48. currentCount={current}
  49. totalCount={total}
  50. />
  51. );
  52. }
  53. render() {
  54. const { t, isNormalized, isRebuildingProcessing } = this.props;
  55. const isEnabled = isNormalized && !isRebuildingProcessing;
  56. return (
  57. <>
  58. { this.renderProgressBar() }
  59. <button
  60. type="submit"
  61. className="btn btn-primary"
  62. onClick={() => { this.props.onRebuildingRequested() }}
  63. disabled={!isEnabled}
  64. >
  65. { t('full_text_search_management.rebuild_button') }
  66. </button>
  67. <p className="form-text text-muted">
  68. { t('full_text_search_management.rebuild_description_1') }<br />
  69. { t('full_text_search_management.rebuild_description_2') }<br />
  70. </p>
  71. </>
  72. );
  73. }
  74. }
  75. /**
  76. * Wrapper component for using unstated
  77. */
  78. const RebuildIndexControlsWrapper = (props) => {
  79. return createSubscribedElement(RebuildIndexControls, props, [AppContainer, WebsocketContainer]);
  80. };
  81. RebuildIndexControls.propTypes = {
  82. t: PropTypes.func.isRequired, // i18next
  83. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  84. websocketContainer: PropTypes.instanceOf(WebsocketContainer).isRequired,
  85. isRebuildingProcessing: PropTypes.bool.isRequired,
  86. isRebuildingCompleted: PropTypes.bool.isRequired,
  87. isNormalized: PropTypes.bool,
  88. onRebuildingRequested: PropTypes.func.isRequired,
  89. };
  90. export default withTranslation()(RebuildIndexControlsWrapper);