PageRenameModal.jsx 8.8 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 PageContainer from '~/client/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, revisionId, pageId } = 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 appContainer.apiv3Put('/pages/rename', {
  92. revisionId,
  93. pageId,
  94. isRenameRecursively,
  95. isRenameRedirect,
  96. isRenameMetadata,
  97. pageNameInput,
  98. path,
  99. });
  100. const { page } = response.data;
  101. const url = new URL(page.path, 'https://dummy');
  102. url.searchParams.append('renamedFrom', path);
  103. if (isRenameRedirect) {
  104. url.searchParams.append('withRedirect', true);
  105. }
  106. window.location.href = `${url.pathname}${url.search}`;
  107. }
  108. catch (err) {
  109. setErrs(err);
  110. }
  111. }
  112. return (
  113. <Modal size="lg" isOpen={props.isOpen} toggle={props.onClose} autoFocus={false}>
  114. <ModalHeader tag="h4" toggle={props.onClose} className="bg-primary text-light">
  115. { t('modal_rename.label.Move/Rename page') }
  116. </ModalHeader>
  117. <ModalBody>
  118. <div className="form-group">
  119. <label>{ t('modal_rename.label.Current page name') }</label><br />
  120. <code>{ path }</code>
  121. </div>
  122. <div className="form-group">
  123. <label htmlFor="newPageName">{ t('modal_rename.label.New page name') }</label><br />
  124. <div className="input-group">
  125. <div className="input-group-prepend">
  126. <span className="input-group-text">{crowi.url}</span>
  127. </div>
  128. <form className="flex-fill" onSubmit={(e) => { e.preventDefault(); rename() }}>
  129. <input
  130. type="text"
  131. value={pageNameInput}
  132. className="form-control"
  133. onChange={e => inputChangeHandler(e.target.value)}
  134. required
  135. autoFocus
  136. />
  137. </form>
  138. </div>
  139. </div>
  140. <div className="custom-control custom-checkbox custom-checkbox-warning">
  141. <input
  142. className="custom-control-input"
  143. name="recursively"
  144. id="cbRenameRecursively"
  145. type="checkbox"
  146. checked={isRenameRecursively}
  147. onChange={changeIsRenameRecursivelyHandler}
  148. />
  149. <label className="custom-control-label" htmlFor="cbRenameRecursively">
  150. { t('modal_rename.label.Recursively') }
  151. <p className="form-text text-muted mt-0">{ t('modal_rename.help.recursive') }</p>
  152. </label>
  153. {existingPaths.length !== 0 && (
  154. <div
  155. className="custom-control custom-checkbox custom-checkbox-warning"
  156. style={{ display: isRenameRecursively ? '' : 'none' }}
  157. >
  158. <input
  159. className="custom-control-input"
  160. name="withoutExistRecursively"
  161. id="cbRenamewithoutExistRecursively"
  162. type="checkbox"
  163. checked={isRenameRecursivelyWithoutExistPath}
  164. onChange={changeIsRenameRecursivelyWithoutExistPathHandler}
  165. />
  166. <label className="custom-control-label" htmlFor="cbRenamewithoutExistRecursively">
  167. { t('modal_rename.label.Rename without exist path') }
  168. </label>
  169. </div>
  170. )}
  171. {isRenameRecursively && <ComparePathsTable subordinatedPages={subordinatedPages} newPagePath={pageNameInput} />}
  172. {isRenameRecursively && existingPaths.length !== 0 && <DuplicatedPathsTable existingPaths={existingPaths} oldPagePath={pageNameInput} />}
  173. </div>
  174. <div className="custom-control custom-checkbox custom-checkbox-success">
  175. <input
  176. className="custom-control-input"
  177. name="create_redirect"
  178. id="cbRenameRedirect"
  179. type="checkbox"
  180. checked={isRenameRedirect}
  181. onChange={changeIsRenameRedirectHandler}
  182. />
  183. <label className="custom-control-label" htmlFor="cbRenameRedirect">
  184. { t('modal_rename.label.Redirect') }
  185. <p className="form-text text-muted mt-0">{ t('modal_rename.help.redirect') }</p>
  186. </label>
  187. </div>
  188. <div className="custom-control custom-checkbox custom-checkbox-primary">
  189. <input
  190. className="custom-control-input"
  191. name="remain_metadata"
  192. id="cbRenameMetadata"
  193. type="checkbox"
  194. checked={isRenameMetadata}
  195. onChange={changeIsRenameMetadataHandler}
  196. />
  197. <label className="custom-control-label" htmlFor="cbRenameMetadata">
  198. { t('modal_rename.label.Do not update metadata') }
  199. <p className="form-text text-muted mt-0">{ t('modal_rename.help.metadata') }</p>
  200. </label>
  201. </div>
  202. <div> {subordinatedError} </div>
  203. </ModalBody>
  204. <ModalFooter>
  205. <ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
  206. <button
  207. type="button"
  208. className="btn btn-primary"
  209. onClick={rename}
  210. disabled={(isRenameRecursively && !isRenameRecursivelyWithoutExistPath && existingPaths.length !== 0)}
  211. >Rename
  212. </button>
  213. </ModalFooter>
  214. </Modal>
  215. );
  216. };
  217. /**
  218. * Wrapper component for using unstated
  219. */
  220. const PageRenameModalWrapper = withUnstatedContainers(PageRenameModal, [AppContainer, PageContainer]);
  221. PageRenameModal.propTypes = {
  222. t: PropTypes.func.isRequired, // i18next
  223. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  224. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  225. isOpen: PropTypes.bool.isRequired,
  226. onClose: PropTypes.func.isRequired,
  227. path: PropTypes.string.isRequired,
  228. };
  229. export default withTranslation()(PageRenameModalWrapper);