PageDuplicateModal.jsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import React, {
  2. useState, useEffect, useCallback, useMemo,
  3. } from 'react';
  4. import PropTypes from 'prop-types';
  5. import {
  6. Modal, ModalHeader, ModalBody, ModalFooter,
  7. } from 'reactstrap';
  8. import { withTranslation } from 'react-i18next';
  9. import { debounce } from 'throttle-debounce';
  10. import { withUnstatedContainers } from './UnstatedUtils';
  11. import { toastError } from '~/client/util/apiNotification';
  12. import { usePageDuplicateModal } from '~/stores/modal';
  13. import AppContainer from '~/client/services/AppContainer';
  14. import { apiv3Get } from '~/client/util/apiv3-client';
  15. import PagePathAutoComplete from './PagePathAutoComplete';
  16. import ApiErrorMessageList from './PageManagement/ApiErrorMessageList';
  17. import ComparePathsTable from './ComparePathsTable';
  18. import DuplicatePathsTable from './DuplicatedPathsTable';
  19. const LIMIT_FOR_LIST = 10;
  20. const PageDuplicateModal = (props) => {
  21. const {
  22. t, appContainer,
  23. } = props;
  24. const config = appContainer.getConfig();
  25. const isReachable = config.isSearchServiceReachable;
  26. const { crowi } = appContainer.config;
  27. const { data: duplicateModalData, close: closeDuplicateModal } = usePageDuplicateModal();
  28. const { isOpened, page } = duplicateModalData;
  29. const { pageId, path } = page;
  30. const [pageNameInput, setPageNameInput] = useState(path);
  31. const [errs, setErrs] = useState(null);
  32. const [subordinatedPages, setSubordinatedPages] = useState([]);
  33. const [isDuplicateRecursively, setIsDuplicateRecursively] = useState(true);
  34. const [isDuplicateRecursivelyWithoutExistPath, setIsDuplicateRecursivelyWithoutExistPath] = useState(true);
  35. const [existingPaths, setExistingPaths] = useState([]);
  36. const checkExistPaths = useCallback(async(newParentPath) => {
  37. if (page == null) {
  38. return;
  39. }
  40. try {
  41. const res = await apiv3Get('/page/exist-paths', { fromPath: path, toPath: newParentPath });
  42. const { existPaths } = res.data;
  43. setExistingPaths(existPaths);
  44. }
  45. catch (err) {
  46. setErrs(err);
  47. toastError(t('modal_rename.label.Failed to get exist path'));
  48. }
  49. }, [page, path, t]);
  50. const checkExistPathsDebounce = useMemo(() => {
  51. return debounce(1000, checkExistPaths);
  52. }, [checkExistPaths]);
  53. useEffect(() => {
  54. if (pageId != null && path != null && pageNameInput !== path) {
  55. checkExistPathsDebounce(pageNameInput);
  56. }
  57. }, [pageNameInput, subordinatedPages, checkExistPathsDebounce, pageId, path]);
  58. useEffect(() => {
  59. if (pageId != null && path != null && pageNameInput !== path) {
  60. checkExistPathsDebounce(pageNameInput);
  61. }
  62. }, [pageNameInput, subordinatedPages, path, pageId, checkExistPathsDebounce]);
  63. /**
  64. * change pageNameInput for PagePathAutoComplete
  65. * @param {string} value
  66. */
  67. function ppacInputChangeHandler(value) {
  68. setErrs(null);
  69. setPageNameInput(value);
  70. }
  71. /**
  72. * change pageNameInput
  73. * @param {string} value
  74. */
  75. function inputChangeHandler(value) {
  76. setErrs(null);
  77. setPageNameInput(value);
  78. }
  79. function changeIsDuplicateRecursivelyHandler() {
  80. setIsDuplicateRecursively(!isDuplicateRecursively);
  81. }
  82. const getSubordinatedList = useCallback(async() => {
  83. try {
  84. const res = await appContainer.apiv3Get('/pages/subordinated-list', { path, limit: LIMIT_FOR_LIST });
  85. setSubordinatedPages(res.data.subordinatedPages);
  86. }
  87. catch (err) {
  88. setErrs(err);
  89. toastError(t('modal_duplicate.label.Failed to get subordinated pages'));
  90. }
  91. }, [appContainer, path, t]);
  92. useEffect(() => {
  93. if (isOpened) {
  94. getSubordinatedList();
  95. setPageNameInput(path);
  96. }
  97. }, [isOpened, getSubordinatedList, path]);
  98. function changeIsDuplicateRecursivelyWithoutExistPathHandler() {
  99. setIsDuplicateRecursivelyWithoutExistPath(!isDuplicateRecursivelyWithoutExistPath);
  100. }
  101. async function duplicate() {
  102. setErrs(null);
  103. try {
  104. const { data } = await appContainer.apiv3Post('/pages/duplicate', { pageId, pageNameInput, isRecursively: isDuplicateRecursively });
  105. const onDuplicated = duplicateModalData.opts?.onDuplicated;
  106. const fromPath = path;
  107. const toPath = data.page.path;
  108. if (onDuplicated != null) {
  109. onDuplicated(fromPath, toPath);
  110. }
  111. closeDuplicateModal();
  112. }
  113. catch (err) {
  114. setErrs(err);
  115. }
  116. }
  117. function ppacSubmitHandler() {
  118. duplicate();
  119. }
  120. return (
  121. <Modal size="lg" isOpen={isOpened} toggle={closeDuplicateModal} className="grw-duplicate-page" autoFocus={false}>
  122. <ModalHeader tag="h4" toggle={closeDuplicateModal} className="bg-primary text-light">
  123. { t('modal_duplicate.label.Duplicate page') }
  124. </ModalHeader>
  125. <ModalBody>
  126. <div className="form-group"><label>{t('modal_duplicate.label.Current page name')}</label><br />
  127. <code>{path}</code>
  128. </div>
  129. <div className="form-group">
  130. <label htmlFor="duplicatePageName">{ t('modal_duplicate.label.New page name') }</label><br />
  131. <div className="input-group">
  132. <div className="input-group-prepend">
  133. <span className="input-group-text">{crowi.url}</span>
  134. </div>
  135. <div className="flex-fill">
  136. {isReachable
  137. ? (
  138. <PagePathAutoComplete
  139. initializedPath={path}
  140. onSubmit={ppacSubmitHandler}
  141. onInputChange={ppacInputChangeHandler}
  142. autoFocus
  143. />
  144. )
  145. : (
  146. <input
  147. type="text"
  148. value={pageNameInput}
  149. className="form-control"
  150. onChange={e => inputChangeHandler(e.target.value)}
  151. required
  152. />
  153. )}
  154. </div>
  155. </div>
  156. </div>
  157. <div className="custom-control custom-checkbox custom-checkbox-warning mb-3">
  158. <input
  159. className="custom-control-input"
  160. name="recursively"
  161. id="cbDuplicateRecursively"
  162. type="checkbox"
  163. checked={isDuplicateRecursively}
  164. onChange={changeIsDuplicateRecursivelyHandler}
  165. />
  166. <label className="custom-control-label" htmlFor="cbDuplicateRecursively">
  167. { t('modal_duplicate.label.Recursively') }
  168. <p className="form-text text-muted mt-0">{ t('modal_duplicate.help.recursive') }</p>
  169. </label>
  170. <div>
  171. {isDuplicateRecursively && existingPaths.length !== 0 && (
  172. <div className="custom-control custom-checkbox custom-checkbox-warning">
  173. <input
  174. className="custom-control-input"
  175. name="withoutExistRecursively"
  176. id="cbDuplicatewithoutExistRecursively"
  177. type="checkbox"
  178. checked={isDuplicateRecursivelyWithoutExistPath}
  179. onChange={changeIsDuplicateRecursivelyWithoutExistPathHandler}
  180. />
  181. <label className="custom-control-label" htmlFor="cbDuplicatewithoutExistRecursively">
  182. { t('modal_duplicate.label.Duplicate without exist path') }
  183. </label>
  184. </div>
  185. )}
  186. </div>
  187. <div>
  188. {isDuplicateRecursively && path != null && <ComparePathsTable path={path} subordinatedPages={subordinatedPages} newPagePath={pageNameInput} />}
  189. {isDuplicateRecursively && existingPaths.length !== 0 && <DuplicatePathsTable existingPaths={existingPaths} oldPagePath={pageNameInput} />}
  190. </div>
  191. </div>
  192. </ModalBody>
  193. <ModalFooter>
  194. <ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
  195. <button
  196. type="button"
  197. className="btn btn-primary"
  198. onClick={duplicate}
  199. disabled={(isDuplicateRecursively && !isDuplicateRecursivelyWithoutExistPath && existingPaths.length !== 0)}
  200. >
  201. { t('modal_duplicate.label.Duplicate page') }
  202. </button>
  203. </ModalFooter>
  204. </Modal>
  205. );
  206. };
  207. /**
  208. * Wrapper component for using unstated
  209. */
  210. const PageDuplicateModallWrapper = withUnstatedContainers(PageDuplicateModal, [AppContainer]);
  211. PageDuplicateModal.propTypes = {
  212. t: PropTypes.func.isRequired, // i18next
  213. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  214. };
  215. export default withTranslation()(PageDuplicateModallWrapper);