PageDuplicateModal.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import React, {
  2. useState, useEffect, useCallback, useMemo, type JSX,
  3. } from 'react';
  4. import { useAtomValue } from 'jotai';
  5. import { useTranslation } from 'next-i18next';
  6. import {
  7. Modal, ModalHeader, ModalBody, ModalFooter,
  8. } from 'reactstrap';
  9. import { debounce } from 'throttle-debounce';
  10. import { apiv3Get, apiv3Post } from '~/client/util/apiv3-client';
  11. import { toastError } from '~/client/util/toastr';
  12. import { useSiteUrl } from '~/states/global';
  13. import { isSearchServiceReachableAtom } from '~/states/server-configurations';
  14. import { usePageDuplicateModalStatus, usePageDuplicateModalActions } from '~/states/ui/modal/page-duplicate';
  15. import DuplicatePathsTable from './DuplicatedPathsTable';
  16. import ApiErrorMessageList from './PageManagement/ApiErrorMessageList';
  17. import PagePathAutoComplete from './PagePathAutoComplete';
  18. const PageDuplicateModal = (): JSX.Element => {
  19. const { t } = useTranslation();
  20. const siteUrl = useSiteUrl();
  21. const isReachable = useAtomValue(isSearchServiceReachableAtom);
  22. const { isOpened, page, opts } = usePageDuplicateModalStatus();
  23. const { close: closeDuplicateModal } = usePageDuplicateModalActions();
  24. const [pageNameInput, setPageNameInput] = useState('');
  25. const [errs, setErrs] = useState(null);
  26. const [subordinatedPages, setSubordinatedPages] = useState([]);
  27. const [existingPaths, setExistingPaths] = useState<string[]>([]);
  28. const [isDuplicateRecursively, setIsDuplicateRecursively] = useState(true);
  29. const [isDuplicateRecursivelyWithoutExistPath, setIsDuplicateRecursivelyWithoutExistPath] = useState(true);
  30. const [onlyDuplicateUserRelatedResources, setOnlyDuplicateUserRelatedResources] = useState(false);
  31. const updateSubordinatedList = useCallback(async() => {
  32. if (page == null) {
  33. return;
  34. }
  35. const { path } = page;
  36. try {
  37. const res = await apiv3Get('/pages/subordinated-list', { path });
  38. setSubordinatedPages(res.data.subordinatedPages);
  39. }
  40. catch (err) {
  41. setErrs(err);
  42. toastError(t('modal_duplicate.label.Failed to get subordinated pages'));
  43. }
  44. }, [page, t]);
  45. const checkExistPaths = useCallback(async(fromPath, toPath) => {
  46. if (page == null) {
  47. return;
  48. }
  49. try {
  50. const res = await apiv3Get<{ existPaths: string[] }>('/page/exist-paths', { fromPath, toPath });
  51. const { existPaths } = res.data;
  52. setExistingPaths(existPaths);
  53. }
  54. catch (err) {
  55. setErrs(err);
  56. toastError(t('modal_rename.label.Failed to get exist path'));
  57. }
  58. }, [page, t]);
  59. const checkExistPathsDebounce = useMemo(() => {
  60. return debounce(1000, checkExistPaths);
  61. }, [checkExistPaths]);
  62. useEffect(() => {
  63. if (isOpened && page != null && pageNameInput !== page.path) {
  64. checkExistPathsDebounce(page.path, pageNameInput);
  65. }
  66. }, [isOpened, pageNameInput, subordinatedPages, checkExistPathsDebounce, page]);
  67. const ppacInputChangeHandler = useCallback((value: string) => {
  68. setErrs(null);
  69. setPageNameInput(value);
  70. }, []);
  71. /**
  72. * change pageNameInput
  73. * @param {string} value
  74. */
  75. function inputChangeHandler(value) {
  76. setErrs(null);
  77. setPageNameInput(value);
  78. }
  79. function changeIsDuplicateRecursivelyHandler() {
  80. setIsDuplicateRecursively(!isDuplicateRecursively);
  81. }
  82. useEffect(() => {
  83. if (page != null && isOpened) {
  84. updateSubordinatedList();
  85. setPageNameInput(page.path);
  86. }
  87. }, [isOpened, page, updateSubordinatedList]);
  88. const duplicate = useCallback(async() => {
  89. if (page == null) {
  90. return;
  91. }
  92. setErrs(null);
  93. const { pageId, path } = page;
  94. try {
  95. const { data } = await apiv3Post('/pages/duplicate', {
  96. pageId, pageNameInput, isRecursively: isDuplicateRecursively, onlyDuplicateUserRelatedResources,
  97. });
  98. const onDuplicated = opts?.onDuplicated;
  99. const fromPath = path;
  100. const toPath = data.page.path;
  101. if (onDuplicated != null) {
  102. onDuplicated(fromPath, toPath);
  103. }
  104. closeDuplicateModal();
  105. }
  106. catch (err) {
  107. setErrs(err);
  108. }
  109. }, [closeDuplicateModal, opts?.onDuplicated, isDuplicateRecursively, page, pageNameInput, onlyDuplicateUserRelatedResources]);
  110. useEffect(() => {
  111. if (isOpened) {
  112. return;
  113. }
  114. // reset states after the modal closed
  115. setTimeout(() => {
  116. setPageNameInput('');
  117. setErrs(null);
  118. setSubordinatedPages([]);
  119. setExistingPaths([]);
  120. setIsDuplicateRecursively(true);
  121. setIsDuplicateRecursivelyWithoutExistPath(false);
  122. }, 1000);
  123. }, [isOpened]);
  124. const renderBodyContent = () => {
  125. if (!isOpened || page == null) {
  126. return <></>;
  127. }
  128. const { path } = page;
  129. const isTargetPageDuplicate = existingPaths.includes(pageNameInput);
  130. return (
  131. <>
  132. <div className="mt-3"><label className="form-label">{t('modal_duplicate.label.Current page name')}</label><br />
  133. <code>{path}</code>
  134. </div>
  135. <div className="mt-3">
  136. <label className="form-label" htmlFor="duplicatePageName">{ t('modal_duplicate.label.New page name') }</label><br />
  137. <div className="input-group">
  138. <div>
  139. <span className="input-group-text">{siteUrl}</span>
  140. </div>
  141. <div className="flex-fill">
  142. {isReachable
  143. ? (
  144. <PagePathAutoComplete
  145. initializedPath={path}
  146. onSubmit={duplicate}
  147. onInputChange={ppacInputChangeHandler}
  148. autoFocus
  149. />
  150. )
  151. : (
  152. <input
  153. type="text"
  154. value={pageNameInput}
  155. className="form-control"
  156. onChange={e => inputChangeHandler(e.target.value)}
  157. required
  158. />
  159. )}
  160. </div>
  161. </div>
  162. </div>
  163. { isTargetPageDuplicate && (
  164. <p className="text-danger">Error: Target path is duplicated.</p>
  165. ) }
  166. <div className="form-check form-check-warning mt-3">
  167. <input
  168. className="form-check-input"
  169. name="recursively"
  170. id="cbDuplicateRecursively"
  171. type="checkbox"
  172. checked={isDuplicateRecursively}
  173. onChange={changeIsDuplicateRecursivelyHandler}
  174. />
  175. <label className="form-label form-check-label" htmlFor="cbDuplicateRecursively">
  176. { t('modal_duplicate.label.Recursively') }
  177. <p className="form-text text-muted my-0">{ t('modal_duplicate.help.recursive') }</p>
  178. </label>
  179. <div className="mt-3">
  180. {isDuplicateRecursively && existingPaths.length !== 0 && (
  181. <div className="form-check form-check-warning">
  182. <input
  183. className="form-check-input"
  184. name="withoutExistRecursively"
  185. id="cbDuplicatewithoutExistRecursively"
  186. type="checkbox"
  187. checked={isDuplicateRecursivelyWithoutExistPath}
  188. onChange={() => setIsDuplicateRecursivelyWithoutExistPath(!isDuplicateRecursivelyWithoutExistPath)}
  189. />
  190. <label className="form-label form-check-label" htmlFor="cbDuplicatewithoutExistRecursively">
  191. { t('modal_duplicate.label.Duplicate without exist path') }
  192. <p className="form-text text-muted my-0">{ t('modal_duplicate.help.recursive') }</p>
  193. </label>
  194. </div>
  195. )}
  196. </div>
  197. </div>
  198. <div className="form-check form-check-warning mt-2">
  199. <input
  200. className="form-check-input"
  201. id="cbOnlyDuplicateUserRelatedResources"
  202. type="checkbox"
  203. checked={onlyDuplicateUserRelatedResources}
  204. onChange={() => setOnlyDuplicateUserRelatedResources(!onlyDuplicateUserRelatedResources)}
  205. />
  206. <label className="form-label form-check-label" htmlFor="cbOnlyDuplicateUserRelatedResources">
  207. { t('modal_duplicate.label.Only duplicate user related pages') }
  208. <p className="form-text text-muted my-0">{ t('modal_duplicate.help.only_inherit_user_related_groups') }</p>
  209. </label>
  210. </div>
  211. <div className="mt-3">
  212. {isDuplicateRecursively && existingPaths.length !== 0 && (
  213. <DuplicatePathsTable existingPaths={existingPaths} fromPath={path} toPath={pageNameInput} />
  214. ) }
  215. </div>
  216. </>
  217. );
  218. };
  219. const renderFooterContent = () => {
  220. if (!isOpened || page == null) {
  221. return <></>;
  222. }
  223. const submitButtonEnabled = existingPaths.length === 0
  224. || (isDuplicateRecursively && isDuplicateRecursivelyWithoutExistPath);
  225. return (
  226. <>
  227. <ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
  228. <button
  229. type="button"
  230. className="btn btn-primary"
  231. data-testid="btn-duplicate"
  232. onClick={duplicate}
  233. disabled={!submitButtonEnabled}
  234. >
  235. { t('modal_duplicate.label.Duplicate page') }
  236. </button>
  237. </>
  238. );
  239. };
  240. return (
  241. <Modal size="lg" isOpen={isOpened} toggle={closeDuplicateModal} data-testid="page-duplicate-modal" className="grw-duplicate-page" autoFocus={false}>
  242. <ModalHeader tag="h4" toggle={closeDuplicateModal}>
  243. { t('modal_duplicate.label.Duplicate page') }
  244. </ModalHeader>
  245. <ModalBody>
  246. {renderBodyContent()}
  247. </ModalBody>
  248. <ModalFooter>
  249. {renderFooterContent()}
  250. </ModalFooter>
  251. </Modal>
  252. );
  253. };
  254. export default PageDuplicateModal;