PageDuplicateModal.jsx 7.7 KB

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