SelectCollectionsModal.tsx 6.8 KB

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