PageDuplicateModal.jsx 7.9 KB

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