PageRenameModal.jsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import React, {
  2. useState, useEffect, useCallback,
  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 '../util/apiNotification';
  12. import AppContainer from '../services/AppContainer';
  13. import PageContainer from '../services/PageContainer';
  14. import ApiErrorMessageList from './PageManagement/ApiErrorMessageList';
  15. import ComparePathsTable from './ComparePathsTable';
  16. import DuplicatedPathsTable from './DuplicatedPathsTable';
  17. const PageRenameModal = (props) => {
  18. const {
  19. t, appContainer, pageContainer,
  20. } = props;
  21. const { 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 [existingPaths, setExistingPaths] = useState([]);
  27. const [isRenameRecursively, SetIsRenameRecursively] = useState(true);
  28. const [isRenameRedirect, SetIsRenameRedirect] = useState(false);
  29. const [isRenameMetadata, SetIsRenameMetadata] = useState(false);
  30. const [subordinatedError] = useState(null);
  31. const [isRenameRecursivelyWithoutExistPath, setIsRenameRecursivelyWithoutExistPath] = useState(true);
  32. function changeIsRenameRecursivelyHandler() {
  33. SetIsRenameRecursively(!isRenameRecursively);
  34. }
  35. function changeIsRenameRecursivelyWithoutExistPathHandler() {
  36. setIsRenameRecursivelyWithoutExistPath(!isRenameRecursivelyWithoutExistPath);
  37. }
  38. function changeIsRenameRedirectHandler() {
  39. SetIsRenameRedirect(!isRenameRedirect);
  40. }
  41. function changeIsRenameMetadataHandler() {
  42. SetIsRenameMetadata(!isRenameMetadata);
  43. }
  44. const updateSubordinatedList = useCallback(async() => {
  45. try {
  46. const res = await appContainer.apiv3Get('/pages/subordinated-list', { path });
  47. const { subordinatedPaths } = res.data;
  48. setSubordinatedPages(subordinatedPaths);
  49. }
  50. catch (err) {
  51. setErrs(err);
  52. toastError(t('modal_rename.label.Fail to get subordinated pages'));
  53. }
  54. }, [appContainer, path, t]);
  55. useEffect(() => {
  56. if (props.isOpen) {
  57. updateSubordinatedList();
  58. }
  59. }, [props.isOpen, updateSubordinatedList]);
  60. const checkExistPaths = async(newParentPath) => {
  61. try {
  62. const res = await appContainer.apiv3Get('/page/exist-paths', { fromPath: path, toPath: newParentPath });
  63. const { existPaths } = res.data;
  64. setExistingPaths(existPaths);
  65. }
  66. catch (err) {
  67. setErrs(err);
  68. toastError(t('modal_rename.label.Fail to get exist path'));
  69. }
  70. };
  71. // eslint-disable-next-line react-hooks/exhaustive-deps
  72. const checkExistPathsDebounce = useCallback(
  73. debounce(1000, checkExistPaths), [],
  74. );
  75. useEffect(() => {
  76. if (pageNameInput !== path) {
  77. checkExistPathsDebounce(pageNameInput, subordinatedPages);
  78. }
  79. }, [pageNameInput, subordinatedPages, path, checkExistPathsDebounce]);
  80. /**
  81. * change pageNameInput
  82. * @param {string} value
  83. */
  84. function inputChangeHandler(value) {
  85. setErrs(null);
  86. setPageNameInput(value);
  87. }
  88. async function rename() {
  89. setErrs(null);
  90. try {
  91. const response = await pageContainer.rename(
  92. pageNameInput,
  93. isRenameRecursively,
  94. isRenameRedirect,
  95. isRenameMetadata,
  96. );
  97. const { page } = response.data;
  98. const url = new URL(page.path, 'https://dummy');
  99. url.searchParams.append('renamedFrom', path);
  100. if (isRenameRedirect) {
  101. url.searchParams.append('withRedirect', true);
  102. }
  103. window.location.href = `${url.pathname}${url.search}`;
  104. }
  105. catch (err) {
  106. setErrs(err);
  107. }
  108. }
  109. return (
  110. <Modal size="lg" isOpen={props.isOpen} toggle={props.onClose} autoFocus={false}>
  111. <ModalHeader tag="h4" toggle={props.onClose} className="bg-primary text-light">
  112. { t('modal_rename.label.Move/Rename page') }
  113. </ModalHeader>
  114. <ModalBody>
  115. <div className="form-group">
  116. <label>{ t('modal_rename.label.Current page name') }</label><br />
  117. <code>{ path }</code>
  118. </div>
  119. <div className="form-group">
  120. <label htmlFor="newPageName">{ t('modal_rename.label.New page name') }</label><br />
  121. <div className="input-group">
  122. <div className="input-group-prepend">
  123. <span className="input-group-text">{crowi.url}</span>
  124. </div>
  125. <form className="flex-fill" onSubmit={(e) => { e.preventDefault(); rename() }}>
  126. <input
  127. type="text"
  128. value={pageNameInput}
  129. className="form-control"
  130. onChange={e => inputChangeHandler(e.target.value)}
  131. required
  132. autoFocus
  133. />
  134. </form>
  135. </div>
  136. </div>
  137. <div className="custom-control custom-checkbox custom-checkbox-warning">
  138. <input
  139. className="custom-control-input"
  140. name="recursively"
  141. id="cbRenameRecursively"
  142. type="checkbox"
  143. checked={isRenameRecursively}
  144. onChange={changeIsRenameRecursivelyHandler}
  145. />
  146. <label className="custom-control-label" htmlFor="cbRenameRecursively">
  147. { t('modal_rename.label.Recursively') }
  148. <p className="form-text text-muted mt-0">{ t('modal_rename.help.recursive') }</p>
  149. </label>
  150. {existingPaths.length !== 0 && (
  151. <div
  152. className="custom-control custom-checkbox custom-checkbox-warning"
  153. style={{ display: isRenameRecursively ? '' : 'none' }}
  154. >
  155. <input
  156. className="custom-control-input"
  157. name="withoutExistRecursively"
  158. id="cbRenamewithoutExistRecursively"
  159. type="checkbox"
  160. checked={isRenameRecursivelyWithoutExistPath}
  161. onChange={changeIsRenameRecursivelyWithoutExistPathHandler}
  162. />
  163. <label className="custom-control-label" htmlFor="cbRenamewithoutExistRecursively">
  164. { t('modal_rename.label.Rename without exist path') }
  165. </label>
  166. </div>
  167. )}
  168. {isRenameRecursively && <ComparePathsTable subordinatedPages={subordinatedPages} newPagePath={pageNameInput} />}
  169. {isRenameRecursively && existingPaths.length !== 0 && <DuplicatedPathsTable existingPaths={existingPaths} oldPagePath={pageNameInput} />}
  170. </div>
  171. <div className="custom-control custom-checkbox custom-checkbox-success">
  172. <input
  173. className="custom-control-input"
  174. name="create_redirect"
  175. id="cbRenameRedirect"
  176. type="checkbox"
  177. checked={isRenameRedirect}
  178. onChange={changeIsRenameRedirectHandler}
  179. />
  180. <label className="custom-control-label" htmlFor="cbRenameRedirect">
  181. { t('modal_rename.label.Redirect') }
  182. <p className="form-text text-muted mt-0">{ t('modal_rename.help.redirect') }</p>
  183. </label>
  184. </div>
  185. <div className="custom-control custom-checkbox custom-checkbox-primary">
  186. <input
  187. className="custom-control-input"
  188. name="remain_metadata"
  189. id="cbRenameMetadata"
  190. type="checkbox"
  191. checked={isRenameMetadata}
  192. onChange={changeIsRenameMetadataHandler}
  193. />
  194. <label className="custom-control-label" htmlFor="cbRenameMetadata">
  195. { t('modal_rename.label.Do not update metadata') }
  196. <p className="form-text text-muted mt-0">{ t('modal_rename.help.metadata') }</p>
  197. </label>
  198. </div>
  199. <div> {subordinatedError} </div>
  200. </ModalBody>
  201. <ModalFooter>
  202. <ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
  203. <button
  204. type="button"
  205. className="btn btn-primary"
  206. onClick={rename}
  207. disabled={(isRenameRecursively && !isRenameRecursivelyWithoutExistPath && existingPaths.length !== 0)}
  208. >Rename
  209. </button>
  210. </ModalFooter>
  211. </Modal>
  212. );
  213. };
  214. /**
  215. * Wrapper component for using unstated
  216. */
  217. const PageRenameModalWrapper = withUnstatedContainers(PageRenameModal, [AppContainer, PageContainer]);
  218. PageRenameModal.propTypes = {
  219. t: PropTypes.func.isRequired, // i18next
  220. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  221. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  222. isOpen: PropTypes.bool.isRequired,
  223. onClose: PropTypes.func.isRequired,
  224. path: PropTypes.string.isRequired,
  225. };
  226. export default withTranslation()(PageRenameModalWrapper);