ExportArchiveDataPage.jsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 { toastSuccess, toastError } from '../../../util/apiNotification';
  7. import AppContainer from '../../services/AppContainer';
  8. import WebsocketContainer from '../../services/WebsocketContainer';
  9. import ProgressBar from './Common/ProgressBar';
  10. import SelectCollectionsModal from './ExportArchiveData/SelectCollectionsModal';
  11. import ArchiveFilesTable from './ExportArchiveData/ArchiveFilesTable';
  12. class ExportArchiveDataPage 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. isZipping: false,
  22. isExported: false,
  23. };
  24. this.onZipFileStatAdd = this.onZipFileStatAdd.bind(this);
  25. this.onZipFileStatRemove = this.onZipFileStatRemove.bind(this);
  26. this.openExportModal = this.openExportModal.bind(this);
  27. this.closeExportModal = this.closeExportModal.bind(this);
  28. this.exportingRequestedHandler = this.exportingRequestedHandler.bind(this);
  29. }
  30. async componentWillMount() {
  31. // TODO:: use apiv3.get
  32. // eslint-disable-next-line no-unused-vars
  33. const [{ collections }, { status }] = await Promise.all([
  34. this.props.appContainer.apiGet('/v3/mongo/collections', {}),
  35. this.props.appContainer.apiGet('/v3/export/status', {}),
  36. ]);
  37. // TODO: toastSuccess, toastError
  38. const { zipFileStats, isExporting, progressList } = status;
  39. this.setState({
  40. collections,
  41. zipFileStats,
  42. isExporting,
  43. progressList,
  44. });
  45. this.setupWebsocketEventHandler();
  46. }
  47. setupWebsocketEventHandler() {
  48. const socket = this.props.websocketContainer.getWebSocket();
  49. // websocket event
  50. socket.on('admin:onProgressForExport', ({ currentCount, totalCount, progressList }) => {
  51. this.setState({
  52. isExporting: true,
  53. progressList,
  54. });
  55. });
  56. // websocket event
  57. socket.on('admin:onStartZippingForExport', () => {
  58. this.setState({
  59. isZipping: true,
  60. });
  61. });
  62. // websocket event
  63. socket.on('admin:onTerminateForExport', ({ addedZipFileStat }) => {
  64. const zipFileStats = this.state.zipFileStats.concat([addedZipFileStat]);
  65. this.setState({
  66. isExporting: false,
  67. isZipping: false,
  68. isExported: true,
  69. zipFileStats,
  70. });
  71. // TODO: toastSuccess, toastError
  72. toastr.success(undefined, `New Archive Data '${addedZipFileStat.fileName}' is added`, {
  73. closeButton: true,
  74. progressBar: true,
  75. newestOnTop: false,
  76. showDuration: '100',
  77. hideDuration: '100',
  78. timeOut: '1200',
  79. extendedTimeOut: '150',
  80. });
  81. });
  82. }
  83. onZipFileStatAdd(newStat) {
  84. this.setState((prevState) => {
  85. return {
  86. zipFileStats: [...prevState.zipFileStats, newStat],
  87. };
  88. });
  89. }
  90. async onZipFileStatRemove(fileName) {
  91. try {
  92. await this.props.appContainer.apiDelete(`/v3/export/${fileName}`, {});
  93. this.setState((prevState) => {
  94. return {
  95. zipFileStats: prevState.zipFileStats.filter(stat => stat.fileName !== fileName),
  96. };
  97. });
  98. // TODO: toastSuccess, toastError
  99. toastr.success(undefined, `Deleted ${fileName}`, {
  100. closeButton: true,
  101. progressBar: true,
  102. newestOnTop: false,
  103. showDuration: '100',
  104. hideDuration: '100',
  105. timeOut: '1200',
  106. extendedTimeOut: '150',
  107. });
  108. }
  109. catch (err) {
  110. // TODO: toastSuccess, toastError
  111. toastr.error(err, 'Error', {
  112. closeButton: true,
  113. progressBar: true,
  114. newestOnTop: false,
  115. showDuration: '100',
  116. hideDuration: '100',
  117. timeOut: '3000',
  118. });
  119. }
  120. }
  121. openExportModal() {
  122. this.setState({ isExportModalOpen: true });
  123. }
  124. closeExportModal() {
  125. this.setState({ isExportModalOpen: false });
  126. }
  127. /**
  128. * event handler invoked when export process was requested successfully
  129. */
  130. exportingRequestedHandler() {
  131. }
  132. renderProgressBarsForCollections() {
  133. const cols = this.state.progressList.map((progressData) => {
  134. const { collectionName, currentCount, totalCount } = progressData;
  135. return (
  136. <div className="col-md-6" key={collectionName}>
  137. <ProgressBar
  138. header={collectionName}
  139. currentCount={currentCount}
  140. totalCount={totalCount}
  141. />
  142. </div>
  143. );
  144. });
  145. return <div className="row px-3">{cols}</div>;
  146. }
  147. renderProgressBarForZipping() {
  148. const { isZipping, isExported } = this.state;
  149. const showZippingBar = isZipping || isExported;
  150. if (!showZippingBar) {
  151. return <></>;
  152. }
  153. return (
  154. <div className="row px-3">
  155. <div className="col-md-12" key="progressBarForZipping">
  156. <ProgressBar
  157. header="Zip Files"
  158. currentCount={1}
  159. totalCount={1}
  160. isInProgress={isZipping}
  161. />
  162. </div>
  163. </div>
  164. );
  165. }
  166. render() {
  167. const { t } = this.props;
  168. const { isExporting, isExported, progressList } = this.state;
  169. const showExportingData = (isExported || isExporting) && (progressList != null);
  170. return (
  171. <Fragment>
  172. <h2>{t('Export Archive Data')}</h2>
  173. <button type="button" className="btn btn-default" disabled={isExporting} onClick={this.openExportModal}>
  174. {t('export_management.create_new_archive_data')}
  175. </button>
  176. { showExportingData && (
  177. <div className="mt-5">
  178. <h3>{t('export_management.exporting_collection_list')}</h3>
  179. { this.renderProgressBarsForCollections() }
  180. { this.renderProgressBarForZipping() }
  181. </div>
  182. ) }
  183. <div className="mt-5">
  184. <h3>{t('export_management.exported_data_list')}</h3>
  185. <ArchiveFilesTable
  186. zipFileStats={this.state.zipFileStats}
  187. onZipFileStatRemove={this.onZipFileStatRemove}
  188. />
  189. </div>
  190. <SelectCollectionsModal
  191. isOpen={this.state.isExportModalOpen}
  192. onExportingRequested={this.exportingRequestedHandler}
  193. onClose={this.closeExportModal}
  194. collections={this.state.collections}
  195. />
  196. </Fragment>
  197. );
  198. }
  199. }
  200. ExportArchiveDataPage.propTypes = {
  201. t: PropTypes.func.isRequired, // i18next
  202. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  203. websocketContainer: PropTypes.instanceOf(WebsocketContainer).isRequired,
  204. };
  205. /**
  206. * Wrapper component for using unstated
  207. */
  208. const ExportArchiveDataPageWrapper = (props) => {
  209. return createSubscribedElement(ExportArchiveDataPage, props, [AppContainer, WebsocketContainer]);
  210. };
  211. export default withTranslation()(ExportArchiveDataPageWrapper);