SelectCollectionsModal.jsx 7.7 KB

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