SelectCollectionsModal.jsx 7.7 KB

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