GrowiArchiveSection.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 { withUnstatedContainers } from '../../UnstatedUtils';
  6. import AppContainer from '../../../services/AppContainer';
  7. // import { toastSuccess, toastError } from '../../../util/apiNotification';
  8. import UploadForm from './GrowiArchive/UploadForm';
  9. import ImportForm from './GrowiArchive/ImportForm';
  10. class GrowiArchiveSection extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.initialState = {
  14. fileName: null,
  15. innerFileStats: null,
  16. };
  17. this.state = this.initialState;
  18. this.handleUpload = this.handleUpload.bind(this);
  19. this.discardData = this.discardData.bind(this);
  20. this.resetState = this.resetState.bind(this);
  21. }
  22. async componentWillMount() {
  23. // get uploaded file status
  24. const res = await this.props.appContainer.apiv3Get('/import/status');
  25. if (res.data.zipFileStat != null) {
  26. const { fileName, innerFileStats } = res.data.zipFileStat;
  27. this.setState({ fileName, innerFileStats });
  28. }
  29. }
  30. handleUpload({ meta, fileName, innerFileStats }) {
  31. this.setState({
  32. fileName,
  33. innerFileStats,
  34. });
  35. }
  36. async discardData() {
  37. try {
  38. const { fileName } = this.state;
  39. await this.props.appContainer.apiv3Delete('/import/all');
  40. this.resetState();
  41. // TODO: toastSuccess, toastError
  42. toastr.success(undefined, `Deleted ${fileName}`, {
  43. closeButton: true,
  44. progressBar: true,
  45. newestOnTop: false,
  46. showDuration: '100',
  47. hideDuration: '100',
  48. timeOut: '1200',
  49. extendedTimeOut: '150',
  50. });
  51. }
  52. catch (err) {
  53. // TODO: toastSuccess, toastError
  54. toastr.error(err, 'Error', {
  55. closeButton: true,
  56. progressBar: true,
  57. newestOnTop: false,
  58. showDuration: '100',
  59. hideDuration: '100',
  60. timeOut: '3000',
  61. });
  62. }
  63. }
  64. resetState() {
  65. this.setState(this.initialState);
  66. }
  67. render() {
  68. const { t } = this.props;
  69. return (
  70. <Fragment>
  71. <h2>{t('admin:importer_management.import_growi_archive')}</h2>
  72. {this.state.fileName != null ? (
  73. <div className="px-4">
  74. <ImportForm
  75. fileName={this.state.fileName}
  76. innerFileStats={this.state.innerFileStats}
  77. onDiscard={this.discardData}
  78. />
  79. </div>
  80. )
  81. : (
  82. <UploadForm
  83. onUpload={this.handleUpload}
  84. />
  85. )}
  86. </Fragment>
  87. );
  88. }
  89. }
  90. GrowiArchiveSection.propTypes = {
  91. t: PropTypes.func.isRequired, // i18next
  92. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  93. };
  94. /**
  95. * Wrapper component for using unstated
  96. */
  97. const GrowiArchiveSectionWrapper = withUnstatedContainers(GrowiArchiveSection, [AppContainer]);
  98. export default withTranslation()(GrowiArchiveSectionWrapper);