PageRenameModal.jsx 8.7 KB

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