SelectCollectionsModal.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 * as toastr from 'toastr';
  7. import { apiPost } from '~/client/util/apiv1-client';
  8. // import { toastSuccess, toastError } from '~/client/util/toastr';
  9. const GROUPS_PAGE = [
  10. 'pages', 'revisions', 'tags', 'pagetagrelations', 'pageredirects', 'comments', 'sharelinks',
  11. ];
  12. const GROUPS_USER = [
  13. 'users', 'externalaccounts', 'usergroups', 'usergrouprelations',
  14. 'useruisettings', 'editorsettings', 'bookmarks', 'subscriptions',
  15. 'inappnotificationsettings',
  16. ];
  17. const GROUPS_CONFIG = [
  18. 'configs', 'updateposts', 'globalnotificationsettings', 'slackappintegrations',
  19. ];
  20. const ALL_GROUPED_COLLECTIONS = GROUPS_PAGE.concat(GROUPS_USER).concat(GROUPS_CONFIG);
  21. type Props = {
  22. isOpen: boolean,
  23. onExportingRequested: () => void,
  24. onClose: () => void,
  25. collections: string[],
  26. isAllChecked?: boolean,
  27. };
  28. const SelectCollectionsModal = (props: Props): JSX.Element => {
  29. const { t } = useTranslation();
  30. const {
  31. isOpen, onExportingRequested, onClose, collections, isAllChecked,
  32. } = props;
  33. const [selectedCollections, setSelectedCollections] = useState<Set<string>>(new Set());
  34. const toggleCheckbox = useCallback((e) => {
  35. const { target } = e;
  36. const { name, checked } = target;
  37. setSelectedCollections((prevState) => {
  38. const selectedCollections = new Set(prevState);
  39. if (checked) {
  40. selectedCollections.add(name);
  41. }
  42. else {
  43. selectedCollections.delete(name);
  44. }
  45. return selectedCollections;
  46. });
  47. }, []);
  48. const checkAll = useCallback(() => {
  49. setSelectedCollections(new Set(collections));
  50. }, [collections]);
  51. const uncheckAll = useCallback(() => {
  52. setSelectedCollections(new Set());
  53. }, []);
  54. const doExport = useCallback(async(e) => {
  55. e.preventDefault();
  56. try {
  57. // TODO: use apiv3Post
  58. const result = await apiPost<any>('/v3/export', { collections: Array.from(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. onExportingRequested();
  74. onClose();
  75. uncheckAll();
  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. }, [onClose, onExportingRequested, selectedCollections, uncheckAll]);
  89. const validateForm = useCallback(() => {
  90. return selectedCollections.size > 0;
  91. }, [selectedCollections.size]);
  92. const renderWarnForUser = useCallback(() => {
  93. // whether selectedCollections includes one of GROUPS_USER
  94. const isUserRelatedDataSelected = GROUPS_USER.some((collectionName) => {
  95. return selectedCollections.has(collectionName);
  96. });
  97. if (!isUserRelatedDataSelected) {
  98. return <></>;
  99. }
  100. const html = 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. }, [selectedCollections, t]);
  104. const renderCheckboxes = useCallback((collectionNames, color?) => {
  105. const checkboxColor = color ? `custom-checkbox-${color}` : 'custom-checkbox-info';
  106. return (
  107. <div className={`custom-control custom-checkbox ${checkboxColor}`}>
  108. <div className="row">
  109. {collectionNames.map((collectionName) => {
  110. return (
  111. <div className="col-sm-6 my-1" key={collectionName}>
  112. <input
  113. type="checkbox"
  114. className="custom-control-input"
  115. id={collectionName}
  116. name={collectionName}
  117. value={collectionName}
  118. checked={selectedCollections.has(collectionName)}
  119. onChange={toggleCheckbox}
  120. />
  121. <label className="text-capitalize custom-control-label ml-3" htmlFor={collectionName}>
  122. {collectionName}
  123. </label>
  124. </div>
  125. );
  126. })}
  127. </div>
  128. </div>
  129. );
  130. }, [selectedCollections, toggleCheckbox]);
  131. const renderGroups = useCallback((groupList, color?) => {
  132. const collectionNames = groupList.filter((collectionName) => {
  133. return collections.includes(collectionName);
  134. });
  135. return renderCheckboxes(collectionNames, color);
  136. }, [collections, renderCheckboxes]);
  137. const renderOthers = useCallback(() => {
  138. const collectionNames = collections.filter((collectionName) => {
  139. return !ALL_GROUPED_COLLECTIONS.includes(collectionName);
  140. });
  141. return renderCheckboxes(collectionNames);
  142. }, [collections, renderCheckboxes]);
  143. useEffect(() => {
  144. if (isAllChecked) checkAll();
  145. }, [isAllChecked, checkAll]);
  146. return (
  147. <Modal isOpen={isOpen} toggle={onClose}>
  148. <ModalHeader tag="h4" toggle={onClose} className="bg-info text-light">
  149. {t('admin:export_management.export_collections')}
  150. </ModalHeader>
  151. <form onSubmit={doExport}>
  152. <ModalBody>
  153. <div className="row">
  154. <div className="col-sm-12">
  155. <button type="button" className="btn btn-sm btn-outline-secondary mr-2" onClick={checkAll}>
  156. <i className="fa fa-check-square-o"></i> {t('admin:export_management.check_all')}
  157. </button>
  158. <button type="button" className="btn btn-sm btn-outline-secondary mr-2" onClick={uncheckAll}>
  159. <i className="fa fa-square-o"></i> {t('admin:export_management.uncheck_all')}
  160. </button>
  161. </div>
  162. </div>
  163. <div className="row mt-4">
  164. <div className="col-sm-12">
  165. <h3 className="admin-setting-header">MongoDB Page Collections</h3>
  166. {renderGroups(GROUPS_PAGE)}
  167. </div>
  168. </div>
  169. <div className="row mt-4">
  170. <div className="col-sm-12">
  171. <h3 className="admin-setting-header">MongoDB User Collections</h3>
  172. {renderGroups(GROUPS_USER, 'danger')}
  173. {renderWarnForUser()}
  174. </div>
  175. </div>
  176. <div className="row mt-4">
  177. <div className="col-sm-12">
  178. <h3 className="admin-setting-header">MongoDB Config Collections</h3>
  179. {renderGroups(GROUPS_CONFIG)}
  180. </div>
  181. </div>
  182. <div className="row mt-4">
  183. <div className="col-sm-12">
  184. <h3 className="admin-setting-header">MongoDB Other Collections</h3>
  185. {renderOthers()}
  186. </div>
  187. </div>
  188. </ModalBody>
  189. <ModalFooter>
  190. <button type="button" className="btn btn-sm btn-outline-secondary" onClick={onClose}>{t('admin:export_management.cancel')}</button>
  191. <button type="submit" className="btn btn-sm btn-primary" disabled={!validateForm()}>{t('admin:export_management.export')}</button>
  192. </ModalFooter>
  193. </form>
  194. </Modal>
  195. );
  196. };
  197. export default SelectCollectionsModal;