ExportPage.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 ExportZipFormModal from './ExportZipFormModal';
  6. import ZipFileTable from './ZipFileTable';
  7. import { createSubscribedElement } from '../../UnstatedUtils';
  8. import AppContainer from '../../../services/AppContainer';
  9. // import { toastSuccess, toastError } from '../../../util/apiNotification';
  10. class ExportPage extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. collections: [],
  15. zipFileStats: [],
  16. isExportModalOpen: false,
  17. };
  18. this.onZipFileStatAdd = this.onZipFileStatAdd.bind(this);
  19. this.onZipFileStatRemove = this.onZipFileStatRemove.bind(this);
  20. this.openExportModal = this.openExportModal.bind(this);
  21. this.closeExportModal = this.closeExportModal.bind(this);
  22. }
  23. async componentDidMount() {
  24. // TODO:: use apiv3.get
  25. // eslint-disable-next-line no-unused-vars
  26. const [{ collections }, { zipFileStats }] = await Promise.all([
  27. this.props.appContainer.apiGet('/v3/mongo/collections', {}),
  28. this.props.appContainer.apiGet('/v3/export/status', {}),
  29. ]);
  30. // TODO: toastSuccess, toastError
  31. this.setState({ collections: ['pages', 'revisions'], zipFileStats }); // FIXME: delete this line and uncomment the line below
  32. // this.setState({ collections, zipFileStats });
  33. }
  34. onZipFileStatAdd(newStat) {
  35. this.setState((prevState) => {
  36. return {
  37. zipFileStats: [...prevState.zipFileStats, newStat],
  38. };
  39. });
  40. }
  41. async onZipFileStatRemove(fileName) {
  42. try {
  43. await this.props.appContainer.apiRequest('delete', `/v3/export/${fileName}`, {});
  44. this.setState((prevState) => {
  45. return {
  46. zipFileStats: prevState.zipFileStats.filter(stat => stat.fileName !== fileName),
  47. };
  48. });
  49. // TODO: toastSuccess, toastError
  50. toastr.success(undefined, `Deleted ${fileName}`, {
  51. closeButton: true,
  52. progressBar: true,
  53. newestOnTop: false,
  54. showDuration: '100',
  55. hideDuration: '100',
  56. timeOut: '1200',
  57. extendedTimeOut: '150',
  58. });
  59. }
  60. catch (err) {
  61. // TODO: toastSuccess, toastError
  62. toastr.error(err, 'Error', {
  63. closeButton: true,
  64. progressBar: true,
  65. newestOnTop: false,
  66. showDuration: '100',
  67. hideDuration: '100',
  68. timeOut: '3000',
  69. });
  70. }
  71. }
  72. openExportModal() {
  73. this.setState({ isExportModalOpen: true });
  74. }
  75. closeExportModal() {
  76. this.setState({ isExportModalOpen: false });
  77. }
  78. render() {
  79. const { t } = this.props;
  80. return (
  81. <Fragment>
  82. <div className="alert alert-warning">
  83. <i className="icon-exclamation"></i> { t('export_management.beta_warning') }
  84. </div>
  85. <h2>{t('Export Data')}</h2>
  86. <button type="button" className="btn btn-default" onClick={this.openExportModal}>{t('export_management.create_new_exported_data')}</button>
  87. <div className="mt-5">
  88. <h3>{t('export_management.exported_data_list')}</h3>
  89. <ZipFileTable
  90. zipFileStats={this.state.zipFileStats}
  91. onZipFileStatRemove={this.onZipFileStatRemove}
  92. />
  93. </div>
  94. <ExportZipFormModal
  95. isOpen={this.state.isExportModalOpen}
  96. onClose={this.closeExportModal}
  97. collections={this.state.collections}
  98. zipFileStats={this.state.zipFileStats}
  99. onZipFileStatAdd={this.onZipFileStatAdd}
  100. />
  101. </Fragment>
  102. );
  103. }
  104. }
  105. ExportPage.propTypes = {
  106. t: PropTypes.func.isRequired, // i18next
  107. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  108. };
  109. /**
  110. * Wrapper component for using unstated
  111. */
  112. const ExportPageFormWrapper = (props) => {
  113. return createSubscribedElement(ExportPage, props, [AppContainer]);
  114. };
  115. export default withTranslation()(ExportPageFormWrapper);