GrowiArchiveSection.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. isTheSameVersion: null,
  17. };
  18. this.state = this.initialState;
  19. this.handleUpload = this.handleUpload.bind(this);
  20. this.discardData = this.discardData.bind(this);
  21. this.resetState = this.resetState.bind(this);
  22. this.handleVersion = this.handleVersion.bind(this);
  23. this.renderDefferentVersionAlert = this.renderDefferentVersionAlert.bind(this);
  24. }
  25. async componentWillMount() {
  26. // get uploaded file status
  27. const res = await this.props.appContainer.apiv3Get('/import/status');
  28. if (res.data.zipFileStat != null) {
  29. const { fileName, innerFileStats } = res.data.zipFileStat;
  30. this.setState({ fileName, innerFileStats });
  31. }
  32. }
  33. handleUpload({
  34. meta, fileName, innerFileStats,
  35. }) {
  36. this.setState({
  37. fileName,
  38. });
  39. }
  40. async discardData() {
  41. try {
  42. const { fileName } = this.state;
  43. await this.props.appContainer.apiv3Delete('/import/all');
  44. this.resetState();
  45. // TODO: toastSuccess, toastError
  46. toastr.success(undefined, `Deleted ${fileName}`, {
  47. closeButton: true,
  48. progressBar: true,
  49. newestOnTop: false,
  50. showDuration: '100',
  51. hideDuration: '100',
  52. timeOut: '1200',
  53. extendedTimeOut: '150',
  54. });
  55. }
  56. catch (err) {
  57. // TODO: toastSuccess, toastError
  58. toastr.error(err, 'Error', {
  59. closeButton: true,
  60. progressBar: true,
  61. newestOnTop: false,
  62. showDuration: '100',
  63. hideDuration: '100',
  64. timeOut: '3000',
  65. });
  66. }
  67. }
  68. handleVersion(err) {
  69. console.log(`isTheSameVersionA = ${this.state.isTheSameVersion}`);
  70. if (err === 'versions-are-not-met') {
  71. this.setState({
  72. isTheSameVersion: false,
  73. });
  74. console.log(`isTheSameVersionB = ${this.state.isTheSameVersion}`);
  75. }
  76. }
  77. renderDefferentVersionAlert() {
  78. const { t } = this.props;
  79. const { isTheSameVersion } = this.state;
  80. console.log(`isTheSameVersionD =${isTheSameVersion}`);
  81. return (
  82. <div className="alert alert-warning mt-3">
  83. {t('admin:importer_management.growi_settings.errors.different_versions')}
  84. </div>
  85. );
  86. }
  87. resetState() {
  88. this.setState(this.initialState);
  89. }
  90. render() {
  91. const { t } = this.props;
  92. const { isTheSameVersion } = this.state;
  93. console.log(`isTheSameVersion XXX =${isTheSameVersion}`);
  94. return (
  95. <Fragment>
  96. <h2>{t('admin:importer_management.import_growi_archive')}</h2>
  97. <div className="card well mb-4 small">
  98. <ul>
  99. <li>{t('admin:importer_management.skip_username_and_email_when_overlapped')}</li>
  100. <li>{t('admin:importer_management.prepare_new_account_for_migration')}</li>
  101. <li>
  102. <a
  103. href={`${t('admin:importer_management.admin_archive_data_import_guide_url')}`}
  104. target="_blank"
  105. rel="noopener noreferrer"
  106. >{t('admin:importer_management.archive_data_import_detail')}
  107. </a>
  108. </li>
  109. </ul>
  110. </div>
  111. {isTheSameVersion === false && this.renderDefferentVersionAlert()}
  112. {this.state.fileName != null ? (
  113. <div className="px-4">
  114. <ImportForm
  115. fileName={this.state.fileName}
  116. innerFileStats={this.state.innerFileStats}
  117. onDiscard={this.discardData}
  118. />
  119. </div>
  120. )
  121. : (
  122. <UploadForm
  123. onUpload={this.handleUpload}
  124. onVersion={this.handleVersion}
  125. />
  126. )}
  127. </Fragment>
  128. );
  129. }
  130. }
  131. GrowiArchiveSection.propTypes = {
  132. t: PropTypes.func.isRequired, // i18next
  133. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  134. };
  135. /**
  136. * Wrapper component for using unstated
  137. */
  138. const GrowiArchiveSectionWrapper = withUnstatedContainers(GrowiArchiveSection, [AppContainer]);
  139. export default withTranslation()(GrowiArchiveSectionWrapper);