SelectCollectionsModal.jsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 { createSubscribedElement } 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 ? `checkbox-${color}` : 'checkbox-info';
  117. return (
  118. <div className={`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. 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 form-check-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}>
  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-light 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-light 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. {/* FIXME: use something instead of <legend> */}
  163. <legend>Page Collections</legend>
  164. {this.renderGroups(GROUPS_PAGE)}
  165. </div>
  166. </div>
  167. <div className="row mt-4">
  168. <div className="col-sm-12">
  169. {/* FIXME: use something instead of <legend> */}
  170. <legend>User Collections</legend>
  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. {/* FIXME: use something instead of <legend> */}
  178. <legend>Config Collections</legend>
  179. {this.renderGroups(GROUPS_CONFIG)}
  180. </div>
  181. </div>
  182. <div className="row mt-4">
  183. <div className="col-sm-12">
  184. {/* FIXME: use something instead of <legend> */}
  185. <legend>Other Collections</legend>
  186. {this.renderOthers()}
  187. </div>
  188. </div>
  189. </ModalBody>
  190. <ModalFooter>
  191. <button type="button" className="btn btn-sm btn-light" onClick={this.props.onClose}>{t('export_management.cancel')}</button>
  192. <button type="submit" className="btn btn-sm btn-primary" disabled={!this.validateForm()}>{t('export_management.export')}</button>
  193. </ModalFooter>
  194. </form>
  195. </Modal>
  196. );
  197. }
  198. }
  199. SelectCollectionsModal.propTypes = {
  200. t: PropTypes.func.isRequired, // i18next
  201. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  202. isOpen: PropTypes.bool.isRequired,
  203. onExportingRequested: PropTypes.func.isRequired,
  204. onClose: PropTypes.func.isRequired,
  205. collections: PropTypes.arrayOf(PropTypes.string).isRequired,
  206. };
  207. /**
  208. * Wrapper component for using unstated
  209. */
  210. const SelectCollectionsModalWrapper = (props) => {
  211. return createSubscribedElement(SelectCollectionsModal, props, [AppContainer]);
  212. };
  213. export default withTranslation()(SelectCollectionsModalWrapper);