PageDuplicateModal.jsx 7.8 KB

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