SelectCollectionsModal.jsx 7.5 KB

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