SelectCollectionsModal.jsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import Modal from 'react-bootstrap/es/Modal';
  5. import * as toastr from 'toastr';
  6. import { createSubscribedElement } from '../../UnstatedUtils';
  7. import AppContainer from '../../../services/AppContainer';
  8. // import { toastSuccess, toastError } from '../../../util/apiNotification';
  9. const GROUPS_PAGE = [
  10. 'pages', 'revisions', 'tags', 'pagetagrelations',
  11. ];
  12. const GROUPS_USER = [
  13. 'users', 'externalaccounts', 'usergroups', 'usergrouprelations',
  14. ];
  15. const GROUPS_CONFIG = [
  16. 'configs', 'updateposts', 'globalnotificationsettings',
  17. ];
  18. const ALL_GROUPED_COLLECTIONS = GROUPS_PAGE.concat(GROUPS_USER).concat(GROUPS_CONFIG);
  19. class SelectCollectionsModal extends React.Component {
  20. constructor(props) {
  21. super(props);
  22. this.state = {
  23. selectedCollections: new Set(),
  24. };
  25. this.toggleCheckbox = this.toggleCheckbox.bind(this);
  26. this.checkAll = this.checkAll.bind(this);
  27. this.uncheckAll = this.uncheckAll.bind(this);
  28. this.export = this.export.bind(this);
  29. this.validateForm = this.validateForm.bind(this);
  30. }
  31. toggleCheckbox(e) {
  32. const { target } = e;
  33. const { name, checked } = target;
  34. this.setState((prevState) => {
  35. const selectedCollections = new Set(prevState.selectedCollections);
  36. if (checked) {
  37. selectedCollections.add(name);
  38. }
  39. else {
  40. selectedCollections.delete(name);
  41. }
  42. return { selectedCollections };
  43. });
  44. }
  45. checkAll() {
  46. this.setState({ selectedCollections: new Set(this.props.collections) });
  47. }
  48. uncheckAll() {
  49. this.setState({ selectedCollections: new Set() });
  50. }
  51. async export(e) {
  52. e.preventDefault();
  53. try {
  54. // TODO: use appContainer.apiv3.post
  55. const result = await this.props.appContainer.apiPost('/v3/export', { collections: Array.from(this.state.selectedCollections) });
  56. // TODO: toastSuccess, toastError
  57. if (!result.ok) {
  58. throw new Error('Error occured.');
  59. }
  60. // TODO: toastSuccess, toastError
  61. toastr.success(undefined, 'Export process has requested.', {
  62. closeButton: true,
  63. progressBar: true,
  64. newestOnTop: false,
  65. showDuration: '100',
  66. hideDuration: '100',
  67. timeOut: '1200',
  68. extendedTimeOut: '150',
  69. });
  70. this.props.onExportingRequested();
  71. this.props.onClose();
  72. this.setState({ selectedCollections: new Set() });
  73. }
  74. catch (err) {
  75. // TODO: toastSuccess, toastError
  76. toastr.error(err, 'Error', {
  77. closeButton: true,
  78. progressBar: true,
  79. newestOnTop: false,
  80. showDuration: '100',
  81. hideDuration: '100',
  82. timeOut: '3000',
  83. });
  84. }
  85. }
  86. validateForm() {
  87. return this.state.selectedCollections.size > 0;
  88. }
  89. renderWarnForUser() {
  90. // whether this.state.selectedCollections includes one of GROUPS_USER
  91. const isUserRelatedDataSelected = GROUPS_USER.some((collectionName) => {
  92. return this.state.selectedCollections.has(collectionName);
  93. });
  94. if (!isUserRelatedDataSelected) {
  95. return <></>;
  96. }
  97. const html = this.props.t('export_management.desc_password_seed');
  98. // eslint-disable-next-line react/no-danger
  99. return <div className="well well-sm" dangerouslySetInnerHTML={{ __html: html }}></div>;
  100. }
  101. renderGroups(groupList, color) {
  102. const collectionNames = groupList.filter((collectionName) => {
  103. return this.props.collections.includes(collectionName);
  104. });
  105. return this.renderCheckboxes(collectionNames, color);
  106. }
  107. renderOthers() {
  108. const collectionNames = this.props.collections.filter((collectionName) => {
  109. return !ALL_GROUPED_COLLECTIONS.includes(collectionName);
  110. });
  111. return this.renderCheckboxes(collectionNames);
  112. }
  113. renderCheckboxes(collectionNames, color) {
  114. const checkboxColor = color ? `checkbox-${color}` : 'checkbox-info';
  115. return (
  116. <div className={`row checkbox ${checkboxColor}`}>
  117. {collectionNames.map((collectionName) => {
  118. return (
  119. <div className="col-xs-6 my-1" key={collectionName}>
  120. <input
  121. type="checkbox"
  122. id={collectionName}
  123. name={collectionName}
  124. className="form-check-input"
  125. value={collectionName}
  126. checked={this.state.selectedCollections.has(collectionName)}
  127. onChange={this.toggleCheckbox}
  128. />
  129. <label className="text-capitalize form-check-label ml-3" htmlFor={collectionName}>
  130. {collectionName}
  131. </label>
  132. </div>
  133. );
  134. })}
  135. </div>
  136. );
  137. }
  138. render() {
  139. const { t } = this.props;
  140. return (
  141. <Modal show={this.props.isOpen} onHide={this.props.onClose}>
  142. <Modal.Header closeButton>
  143. <Modal.Title>{t('export_management.export_collections')}</Modal.Title>
  144. </Modal.Header>
  145. <form onSubmit={this.export}>
  146. <Modal.Body>
  147. <div className="row">
  148. <div className="col-sm-12">
  149. <button type="button" className="btn btn-sm btn-default mr-2" onClick={this.checkAll}>
  150. <i className="fa fa-check-square-o"></i> {t('export_management.check_all')}
  151. </button>
  152. <button type="button" className="btn btn-sm btn-default mr-2" onClick={this.uncheckAll}>
  153. <i className="fa fa-square-o"></i> {t('export_management.uncheck_all')}
  154. </button>
  155. </div>
  156. </div>
  157. <div className="row mt-4">
  158. <div className="col-xs-12">
  159. <legend>Page Collections</legend>
  160. { this.renderGroups(GROUPS_PAGE) }
  161. </div>
  162. </div>
  163. <div className="row mt-4">
  164. <div className="col-xs-12">
  165. <legend>User Collections</legend>
  166. { this.renderGroups(GROUPS_USER, 'danger') }
  167. { this.renderWarnForUser() }
  168. </div>
  169. </div>
  170. <div className="row mt-4">
  171. <div className="col-xs-12">
  172. <legend>Config Collections</legend>
  173. { this.renderGroups(GROUPS_CONFIG) }
  174. </div>
  175. </div>
  176. <div className="row mt-4">
  177. <div className="col-xs-12">
  178. <legend>Other Collections</legend>
  179. { this.renderOthers() }
  180. </div>
  181. </div>
  182. </Modal.Body>
  183. <Modal.Footer>
  184. <button type="button" className="btn btn-sm btn-default" onClick={this.props.onClose}>{t('export_management.cancel')}</button>
  185. <button type="submit" className="btn btn-sm btn-primary" disabled={!this.validateForm()}>{t('export_management.export')}</button>
  186. </Modal.Footer>
  187. </form>
  188. </Modal>
  189. );
  190. }
  191. }
  192. SelectCollectionsModal.propTypes = {
  193. t: PropTypes.func.isRequired, // i18next
  194. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  195. isOpen: PropTypes.bool.isRequired,
  196. onExportingRequested: PropTypes.func.isRequired,
  197. onClose: PropTypes.func.isRequired,
  198. collections: PropTypes.arrayOf(PropTypes.string).isRequired,
  199. };
  200. /**
  201. * Wrapper component for using unstated
  202. */
  203. const SelectCollectionsModalWrapper = (props) => {
  204. return createSubscribedElement(SelectCollectionsModal, props, [AppContainer]);
  205. };
  206. export default withTranslation()(SelectCollectionsModalWrapper);