SelectCollectionsModal.tsx 7.1 KB

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