PageDuplicateModal.jsx 7.5 KB

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