RebuildIndexControls.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { useTranslation } from 'react-i18next';
  4. import AdminSocketIoContainer from '~/client/services/AdminSocketIoContainer';
  5. import { withUnstatedContainers } from '../../UnstatedUtils';
  6. import LabeledProgressBar from '../Common/LabeledProgressBar';
  7. class RebuildIndexControls extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. total: 0,
  12. current: 0,
  13. skip: 0,
  14. };
  15. }
  16. componentDidMount() {
  17. this.initWebSockets();
  18. }
  19. initWebSockets() {
  20. const socket = this.props.adminSocketIoContainer.getSocket();
  21. socket.on('addPageProgress', (data) => {
  22. this.setState({
  23. total: data.totalCount,
  24. current: data.count,
  25. skip: data.skipped,
  26. });
  27. });
  28. socket.on('finishAddPage', (data) => {
  29. this.setState({
  30. total: data.totalCount,
  31. current: data.count,
  32. skip: data.skipped,
  33. });
  34. });
  35. }
  36. renderProgressBar() {
  37. const {
  38. isRebuildingProcessing, isRebuildingCompleted,
  39. } = this.props;
  40. const {
  41. total, current, skip,
  42. } = this.state;
  43. const showProgressBar = isRebuildingProcessing || isRebuildingCompleted;
  44. if (!showProgressBar) {
  45. return null;
  46. }
  47. function getCompletedLabel() {
  48. const completedLabel = skip === 0 ? 'Completed' : `Done (${skip} skips)`;
  49. return completedLabel;
  50. }
  51. function getSkipLabel() {
  52. return `Processing.. (${skip} skips)`;
  53. }
  54. const header = isRebuildingCompleted ? getCompletedLabel() : getSkipLabel();
  55. return (
  56. <LabeledProgressBar
  57. header={header}
  58. currentCount={current}
  59. errorsCount={skip}
  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. const RebuildIndexControlsFC = (props) => {
  87. const { t } = useTranslation();
  88. return <RebuildIndexControls t={t} {...props} />;
  89. };
  90. /**
  91. * Wrapper component for using unstated
  92. */
  93. const RebuildIndexControlsWrapper = withUnstatedContainers(RebuildIndexControlsFC, [AdminSocketIoContainer]);
  94. RebuildIndexControls.propTypes = {
  95. t: PropTypes.func.isRequired, // i18next
  96. adminSocketIoContainer: PropTypes.instanceOf(AdminSocketIoContainer).isRequired,
  97. isRebuildingProcessing: PropTypes.bool.isRequired,
  98. isRebuildingCompleted: PropTypes.bool.isRequired,
  99. isNormalized: PropTypes.bool,
  100. onRebuildingRequested: PropTypes.func.isRequired,
  101. };
  102. export default RebuildIndexControlsWrapper;