RebuildIndex.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 { toastSuccess, toastError } from '../../../util/apiNotification';
  8. import ProgressBar from '../Common/ProgressBar';
  9. class RebuildIndex extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. isProcessing: false,
  14. isCompleted: false,
  15. total: 0,
  16. current: 0,
  17. skip: 0,
  18. };
  19. this.buildIndex = this.buildIndex.bind(this);
  20. }
  21. componentDidMount() {
  22. const socket = this.props.websocketContainer.getWebSocket();
  23. socket.on('admin:addPageProgress', (data) => {
  24. this.setState({
  25. isProcessing: true,
  26. ...data,
  27. });
  28. });
  29. socket.on('admin:finishAddPage', (data) => {
  30. this.setState({
  31. isProcessing: false,
  32. isCompleted: true,
  33. ...data,
  34. });
  35. });
  36. }
  37. async buildIndex() {
  38. const { appContainer } = this.props;
  39. const pageId = this.pageId;
  40. try {
  41. const res = await appContainer.apiPost('/admin/search/build', { page_id: pageId });
  42. if (!res.ok) {
  43. throw new Error(res.message);
  44. }
  45. this.setState({ isProcessing: true });
  46. toastSuccess('Rebuilding is requested');
  47. }
  48. catch (e) {
  49. toastError(e);
  50. }
  51. }
  52. renderProgressBar() {
  53. const {
  54. total, current, skip, isProcessing, isCompleted,
  55. } = this.state;
  56. const showProgressBar = isProcessing || isCompleted;
  57. if (!showProgressBar) {
  58. return null;
  59. }
  60. const header = isCompleted ? 'Completed' : `Processing.. (${skip} skips)`;
  61. return (
  62. <ProgressBar
  63. header={header}
  64. currentCount={current}
  65. totalCount={total}
  66. />
  67. );
  68. }
  69. render() {
  70. const { t } = this.props;
  71. return (
  72. <>
  73. <div className="row">
  74. <div className="col-xs-3 control-label"></div>
  75. <div className="col-xs-9">
  76. { this.renderProgressBar() }
  77. <button
  78. type="submit"
  79. className="btn btn-inverse"
  80. onClick={this.buildIndex}
  81. disabled={this.state.isProcessing}
  82. >
  83. { t('full_text_search_management.build_button') }
  84. </button>
  85. <p className="help-block">
  86. { t('full_text_search_management.rebuild_description_1') }<br />
  87. { t('full_text_search_management.rebuild_description_2') }<br />
  88. { t('full_text_search_management.rebuild_description_3') }<br />
  89. </p>
  90. </div>
  91. </div>
  92. </>
  93. );
  94. }
  95. }
  96. /**
  97. * Wrapper component for using unstated
  98. */
  99. const RebuildIndexWrapper = (props) => {
  100. return createSubscribedElement(RebuildIndex, props, [AppContainer, WebsocketContainer]);
  101. };
  102. RebuildIndex.propTypes = {
  103. t: PropTypes.func.isRequired, // i18next
  104. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  105. websocketContainer: PropTypes.instanceOf(WebsocketContainer).isRequired,
  106. };
  107. export default withTranslation()(RebuildIndexWrapper);