GrowiZipImportSection.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 GrowiZipUploadForm from './GrowiZipUploadForm';
  6. import GrowiZipImportForm from './GrowiZipImportForm';
  7. import { createSubscribedElement } from '../../UnstatedUtils';
  8. import AppContainer from '../../../services/AppContainer';
  9. // import { toastSuccess, toastError } from '../../../util/apiNotification';
  10. class GrowiZipImportSection extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.initialState = {
  14. fileName: '',
  15. fileStats: [],
  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. handleUpload({ meta, fileName, fileStats }) {
  23. this.setState({
  24. fileName,
  25. fileStats,
  26. });
  27. }
  28. async discardData() {
  29. try {
  30. const { fileName } = this.state;
  31. await this.props.appContainer.apiDelete(`/v3/import/${this.state.fileName}`, {});
  32. this.resetState();
  33. // TODO: toastSuccess, toastError
  34. toastr.success(undefined, `Deleted ${fileName}`, {
  35. closeButton: true,
  36. progressBar: true,
  37. newestOnTop: false,
  38. showDuration: '100',
  39. hideDuration: '100',
  40. timeOut: '1200',
  41. extendedTimeOut: '150',
  42. });
  43. }
  44. catch (err) {
  45. // TODO: toastSuccess, toastError
  46. toastr.error(err, 'Error', {
  47. closeButton: true,
  48. progressBar: true,
  49. newestOnTop: false,
  50. showDuration: '100',
  51. hideDuration: '100',
  52. timeOut: '3000',
  53. });
  54. }
  55. }
  56. resetState() {
  57. this.setState(this.initialState);
  58. }
  59. render() {
  60. const { t } = this.props;
  61. return (
  62. <Fragment>
  63. <legend>{t('importer_management.import_form_growi')}</legend>
  64. <div className="alert alert-warning">
  65. <i className="icon-exclamation"></i> { t('importer_management.beta_warning') }
  66. </div>
  67. <div className="well well-sm small">
  68. <ul>
  69. <li>{t('importer_management.growi_settings.overwrite_documents')}</li>
  70. </ul>
  71. </div>
  72. {this.state.fileName ? (
  73. <Fragment>
  74. <GrowiZipImportForm
  75. fileName={this.state.fileName}
  76. fileStats={this.state.fileStats}
  77. onDiscard={this.discardData}
  78. onPostImport={this.resetState}
  79. />
  80. </Fragment>
  81. ) : (
  82. <GrowiZipUploadForm
  83. onUpload={this.handleUpload}
  84. />
  85. )}
  86. </Fragment>
  87. );
  88. }
  89. }
  90. GrowiZipImportSection.propTypes = {
  91. t: PropTypes.func.isRequired, // i18next
  92. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  93. };
  94. /**
  95. * Wrapper component for using unstated
  96. */
  97. const GrowiZipImportSectionWrapper = (props) => {
  98. return createSubscribedElement(GrowiZipImportSection, props, [AppContainer]);
  99. };
  100. export default withTranslation()(GrowiZipImportSectionWrapper);