ExportAsZip.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import React, { Fragment } 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 ExportPage extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. files: {},
  12. };
  13. this.createZipFile = this.createZipFile.bind(this);
  14. this.deleteZipFile = this.deleteZipFile.bind(this);
  15. }
  16. async componentDidMount() {
  17. const res = await this.props.appContainer.apiGet('/v3/export', {});
  18. this.setState({ files: res.files });
  19. }
  20. async createZipFile() {
  21. // TODO use appContainer.apiv3.post
  22. const res = await this.props.appContainer.apiPost('/v3/export/pages', {});
  23. // TODO toastSuccess, toastError
  24. this.setState((prevState) => {
  25. return {
  26. files: {
  27. ...prevState.files,
  28. [res.collection]: res.file,
  29. },
  30. };
  31. });
  32. }
  33. async deleteZipFile() {
  34. // TODO use appContainer.apiv3.delete
  35. // TODO toastSuccess, toastError
  36. }
  37. render() {
  38. // const { t } = this.props;
  39. return (
  40. <Fragment>
  41. <h2>Export Data as Zip</h2>
  42. <form className="my-5">
  43. {Object.keys(this.state.files).map((file) => {
  44. const disabled = file !== 'pages';
  45. return (
  46. <div className="form-check" key={file}>
  47. <input
  48. type="radio"
  49. id={file}
  50. name="collection"
  51. className="form-check-input"
  52. value={file}
  53. disabled={disabled}
  54. checked={!disabled}
  55. onChange={() => {}}
  56. />
  57. <label className={`form-check-label ml-3 ${disabled ? 'text-muted' : ''}`} htmlFor={file}>
  58. {file} ({this.state.files[file] || 'not found'})
  59. </label>
  60. </div>
  61. );
  62. })}
  63. </form>
  64. <button type="button" className="btn btn-sm btn-default" onClick={this.createZipFile}>Generate</button>
  65. <a href="/_api/v3/export/pages">
  66. <button type="button" className="btn btn-sm btn-primary ml-2">Download</button>
  67. </a>
  68. {/* <button type="button" className="btn btn-sm btn-danger ml-2" onClick={this.deleteZipFile}>Clear</button> */}
  69. </Fragment>
  70. );
  71. }
  72. }
  73. ExportPage.propTypes = {
  74. t: PropTypes.func.isRequired, // i18next
  75. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  76. isAclEnabled: PropTypes.bool,
  77. };
  78. /**
  79. * Wrapper component for using unstated
  80. */
  81. const ExportPageWrapper = (props) => {
  82. return createSubscribedElement(ExportPage, props, [AppContainer]);
  83. };
  84. export default withTranslation()(ExportPageWrapper);