PageRenameModal.jsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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}>
  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. />
  133. </form>
  134. </div>
  135. </div>
  136. <div className="custom-control custom-checkbox custom-checkbox-warning">
  137. <input
  138. className="custom-control-input"
  139. name="recursively"
  140. id="cbRenameRecursively"
  141. type="checkbox"
  142. checked={isRenameRecursively}
  143. onChange={changeIsRenameRecursivelyHandler}
  144. />
  145. <label className="custom-control-label" htmlFor="cbRenameRecursively">
  146. { t('modal_rename.label.Recursively') }
  147. <p className="form-text text-muted mt-0">{ t('modal_rename.help.recursive') }</p>
  148. </label>
  149. {existingPaths.length !== 0 && (
  150. <div
  151. className="custom-control custom-checkbox custom-checkbox-warning"
  152. style={{ display: isRenameRecursively ? '' : 'none' }}
  153. >
  154. <input
  155. className="custom-control-input"
  156. name="withoutExistRecursively"
  157. id="cbRenamewithoutExistRecursively"
  158. type="checkbox"
  159. checked={isRenameRecursivelyWithoutExistPath}
  160. onChange={changeIsRenameRecursivelyWithoutExistPathHandler}
  161. />
  162. <label className="custom-control-label" htmlFor="cbRenamewithoutExistRecursively">
  163. { t('modal_rename.label.Rename without exist path') }
  164. </label>
  165. </div>
  166. )}
  167. {isRenameRecursively && <ComparePathsTable subordinatedPages={subordinatedPages} newPagePath={pageNameInput} />}
  168. {isRenameRecursively && existingPaths.length !== 0 && <DuplicatedPathsTable existingPaths={existingPaths} oldPagePath={pageNameInput} />}
  169. </div>
  170. <div className="custom-control custom-checkbox custom-checkbox-success">
  171. <input
  172. className="custom-control-input"
  173. name="create_redirect"
  174. id="cbRenameRedirect"
  175. type="checkbox"
  176. checked={isRenameRedirect}
  177. onChange={changeIsRenameRedirectHandler}
  178. />
  179. <label className="custom-control-label" htmlFor="cbRenameRedirect">
  180. { t('modal_rename.label.Redirect') }
  181. <p className="form-text text-muted mt-0">{ t('modal_rename.help.redirect') }</p>
  182. </label>
  183. </div>
  184. <div className="custom-control custom-checkbox custom-checkbox-primary">
  185. <input
  186. className="custom-control-input"
  187. name="remain_metadata"
  188. id="cbRenameMetadata"
  189. type="checkbox"
  190. checked={isRenameMetadata}
  191. onChange={changeIsRenameMetadataHandler}
  192. />
  193. <label className="custom-control-label" htmlFor="cbRenameMetadata">
  194. { t('modal_rename.label.Do not update metadata') }
  195. <p className="form-text text-muted mt-0">{ t('modal_rename.help.metadata') }</p>
  196. </label>
  197. </div>
  198. <div> {subordinatedError} </div>
  199. </ModalBody>
  200. <ModalFooter>
  201. <ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
  202. <button
  203. type="button"
  204. className="btn btn-primary"
  205. onClick={rename}
  206. disabled={(isRenameRecursively && !isRenameRecursivelyWithoutExistPath && existingPaths.length !== 0)}
  207. >Rename
  208. </button>
  209. </ModalFooter>
  210. </Modal>
  211. );
  212. };
  213. /**
  214. * Wrapper component for using unstated
  215. */
  216. const PageRenameModalWrapper = withUnstatedContainers(PageRenameModal, [AppContainer, PageContainer]);
  217. PageRenameModal.propTypes = {
  218. t: PropTypes.func.isRequired, // i18next
  219. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  220. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  221. isOpen: PropTypes.bool.isRequired,
  222. onClose: PropTypes.func.isRequired,
  223. path: PropTypes.string.isRequired,
  224. };
  225. export default withTranslation()(PageRenameModalWrapper);