PageDuplicateModal.tsx 9.5 KB

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