GrowiZipUploadForm.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { createSubscribedElement } from '../../UnstatedUtils';
  5. import AppContainer from '../../../services/AppContainer';
  6. // import { toastSuccess, toastError } from '../../../util/apiNotification';
  7. class GrowiZipUploadForm extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.inputRef = React.createRef();
  11. this.changeFileName = this.changeFileName.bind(this);
  12. this.uploadZipFile = this.uploadZipFile.bind(this);
  13. this.validateForm = this.validateForm.bind(this);
  14. }
  15. changeFileName(e) {
  16. // to trigger rerender at onChange event
  17. // eslint-disable-next-line react/no-unused-state
  18. this.setState({ dummy: e.target.files[0].name });
  19. }
  20. async uploadZipFile(e) {
  21. e.preventDefault();
  22. const formData = new FormData();
  23. formData.append('_csrf', this.props.appContainer.csrfToken);
  24. formData.append('file', this.inputRef.current.files[0]);
  25. // TODO: use appContainer.apiv3.post
  26. const { data } = await this.props.appContainer.apiPost('/v3/import/upload', formData);
  27. this.props.onUpload(data);
  28. // TODO: toastSuccess, toastError
  29. }
  30. validateForm() {
  31. return (
  32. this.inputRef.current // null check
  33. && this.inputRef.current.files[0] // null check
  34. && /\.zip$/.test(this.inputRef.current.files[0].name) // validate extension
  35. );
  36. }
  37. render() {
  38. const { t } = this.props;
  39. return (
  40. <form className="form-horizontal" onSubmit={this.uploadZipFile}>
  41. <fieldset>
  42. <div className="form-group">
  43. <label htmlFor="file" className="col-xs-3 control-label">{t('importer_management.growi_settings.zip_file')}</label>
  44. <div className="col-xs-6">
  45. <input
  46. type="file"
  47. name="file"
  48. className="form-control-file"
  49. ref={this.inputRef}
  50. onChange={this.changeFileName}
  51. />
  52. </div>
  53. </div>
  54. <div className="form-group">
  55. <div className="col-xs-offset-3 col-xs-6">
  56. <button type="submit" className="btn btn-primary" disabled={!this.validateForm()}>
  57. {t('importer_management.growi_settings.upload')}
  58. </button>
  59. </div>
  60. </div>
  61. </fieldset>
  62. </form>
  63. );
  64. }
  65. }
  66. GrowiZipUploadForm.propTypes = {
  67. t: PropTypes.func.isRequired, // i18next
  68. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  69. onUpload: PropTypes.func.isRequired,
  70. };
  71. /**
  72. * Wrapper component for using unstated
  73. */
  74. const GrowiZipUploadFormWrapper = (props) => {
  75. return createSubscribedElement(GrowiZipUploadForm, props, [AppContainer]);
  76. };
  77. export default withTranslation()(GrowiZipUploadFormWrapper);