ExportPage.jsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import React, { Fragment } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import * as toastr from 'toastr';
  5. import { createSubscribedElement } from '../../UnstatedUtils';
  6. import AppContainer from '../../../services/AppContainer';
  7. import WebsocketContainer from '../../../services/WebsocketContainer';
  8. // import { toastSuccess, toastError } from '../../../util/apiNotification';
  9. import ExportZipFormModal from './ExportZipFormModal';
  10. import ZipFileTable from './ZipFileTable';
  11. import ExportingProgressBar from './ExportingProgressBar';
  12. class ExportPage extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. collections: [],
  17. zipFileStats: [],
  18. progressList: [],
  19. isExportModalOpen: false,
  20. isExporting: false,
  21. isExported: false,
  22. };
  23. this.onZipFileStatAdd = this.onZipFileStatAdd.bind(this);
  24. this.onZipFileStatRemove = this.onZipFileStatRemove.bind(this);
  25. this.openExportModal = this.openExportModal.bind(this);
  26. this.closeExportModal = this.closeExportModal.bind(this);
  27. this.exportingRequestedHandler = this.exportingRequestedHandler.bind(this);
  28. }
  29. async componentWillMount() {
  30. // TODO:: use apiv3.get
  31. // eslint-disable-next-line no-unused-vars
  32. const [{ collections }, { status }] = await Promise.all([
  33. this.props.appContainer.apiGet('/v3/mongo/collections', {}),
  34. this.props.appContainer.apiGet('/v3/export/status', {}),
  35. ]);
  36. // TODO: toastSuccess, toastError
  37. const { zipFileStats, isExporting, progressList } = status;
  38. this.setState({
  39. collections: ['pages', 'revisions'],
  40. zipFileStats,
  41. isExporting,
  42. progressList,
  43. }); // FIXME: delete this line and uncomment the line below
  44. // this.setState({ collections, zipFileStats, isExporting });
  45. this.setupWebsocketEventHandler();
  46. }
  47. setupWebsocketEventHandler() {
  48. const socket = this.props.websocketContainer.getWebSocket();
  49. socket.on('admin:onProgressForExport', ({ currentCount, totalCount, progressList }) => {
  50. const isExporting = currentCount !== totalCount;
  51. this.setState({ isExporting, progressList });
  52. });
  53. socket.on('admin:onTerminateForExport', ({ currentCount, totalCount, progressList }) => {
  54. this.setState({
  55. isExporting: false,
  56. isExported: true,
  57. progressList,
  58. });
  59. });
  60. }
  61. onZipFileStatAdd(newStat) {
  62. this.setState((prevState) => {
  63. return {
  64. zipFileStats: [...prevState.zipFileStats, newStat],
  65. };
  66. });
  67. }
  68. async onZipFileStatRemove(fileName) {
  69. try {
  70. await this.props.appContainer.apiDelete(`/v3/export/${fileName}`, {});
  71. this.setState((prevState) => {
  72. return {
  73. zipFileStats: prevState.zipFileStats.filter(stat => stat.fileName !== fileName),
  74. };
  75. });
  76. // TODO: toastSuccess, toastError
  77. toastr.success(undefined, `Deleted ${fileName}`, {
  78. closeButton: true,
  79. progressBar: true,
  80. newestOnTop: false,
  81. showDuration: '100',
  82. hideDuration: '100',
  83. timeOut: '1200',
  84. extendedTimeOut: '150',
  85. });
  86. }
  87. catch (err) {
  88. // TODO: toastSuccess, toastError
  89. toastr.error(err, 'Error', {
  90. closeButton: true,
  91. progressBar: true,
  92. newestOnTop: false,
  93. showDuration: '100',
  94. hideDuration: '100',
  95. timeOut: '3000',
  96. });
  97. }
  98. }
  99. openExportModal() {
  100. this.setState({ isExportModalOpen: true });
  101. }
  102. closeExportModal() {
  103. this.setState({ isExportModalOpen: false });
  104. }
  105. /**
  106. * @params {object} export status data
  107. */
  108. exportingRequestedHandler(status) {
  109. const { zipFileStats, isExporting, progressList } = status;
  110. this.setState({ zipFileStats, isExporting, progressList });
  111. }
  112. renderProgressBars() {
  113. return this.state.progressList.map((progressData) => {
  114. const { collectionName, currentCount, totalCount } = progressData;
  115. return (
  116. <div className="px-3 w-50" key={collectionName}>
  117. <ExportingProgressBar
  118. collectionName={collectionName}
  119. currentCount={currentCount}
  120. totalCount={totalCount}
  121. />
  122. </div>
  123. );
  124. });
  125. }
  126. render() {
  127. const { t } = this.props;
  128. const showExportingData = (this.state.isExported || this.state.isExporting) && (this.state.progressList != null);
  129. return (
  130. <Fragment>
  131. <div className="alert alert-warning">
  132. <i className="icon-exclamation"></i> { t('export_management.beta_warning') }
  133. </div>
  134. <h2>{t('Export Data')}</h2>
  135. <button type="button" className="btn btn-default" onClick={this.openExportModal}>{t('export_management.create_new_exported_data')}</button>
  136. { showExportingData && (
  137. <div className="mt-5">
  138. <h3>{t('export_management.exporting_data_list')}</h3>
  139. { this.renderProgressBars() }
  140. </div>
  141. ) }
  142. <div className="mt-5">
  143. <h3>{t('export_management.exported_data_list')}</h3>
  144. <ZipFileTable
  145. zipFileStats={this.state.zipFileStats}
  146. onZipFileStatRemove={this.onZipFileStatRemove}
  147. />
  148. </div>
  149. <ExportZipFormModal
  150. isOpen={this.state.isExportModalOpen}
  151. onExportingRequested={this.exportingRequestedHandler}
  152. onClose={this.closeExportModal}
  153. collections={this.state.collections}
  154. />
  155. </Fragment>
  156. );
  157. }
  158. }
  159. ExportPage.propTypes = {
  160. t: PropTypes.func.isRequired, // i18next
  161. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  162. websocketContainer: PropTypes.instanceOf(WebsocketContainer).isRequired,
  163. };
  164. /**
  165. * Wrapper component for using unstated
  166. */
  167. const ExportPageFormWrapper = (props) => {
  168. return createSubscribedElement(ExportPage, props, [AppContainer, WebsocketContainer]);
  169. };
  170. export default withTranslation()(ExportPageFormWrapper);