PageDuplicateModal.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import React, {
  2. useState, useEffect, useCallback, useMemo,
  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/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 updateSubordinatedList = useCallback(async() => {
  30. if (page == null) {
  31. return;
  32. }
  33. const { path } = page;
  34. try {
  35. const res = await apiv3Get('/pages/subordinated-list', { path });
  36. setSubordinatedPages(res.data.subordinatedPages);
  37. }
  38. catch (err) {
  39. setErrs(err);
  40. toastError(t('modal_duplicate.label.Failed to get subordinated pages'));
  41. }
  42. }, [page, t]);
  43. const checkExistPaths = useCallback(async(fromPath, toPath) => {
  44. if (page == null) {
  45. return;
  46. }
  47. try {
  48. const res = await apiv3Get<{ existPaths: string[] }>('/page/exist-paths', { fromPath, toPath });
  49. const { existPaths } = res.data;
  50. setExistingPaths(existPaths);
  51. }
  52. catch (err) {
  53. setErrs(err);
  54. toastError(t('modal_rename.label.Failed to get exist path'));
  55. }
  56. }, [page, t]);
  57. const checkExistPathsDebounce = useMemo(() => {
  58. return debounce(1000, checkExistPaths);
  59. }, [checkExistPaths]);
  60. useEffect(() => {
  61. if (isOpened && page != null && pageNameInput !== page.path) {
  62. checkExistPathsDebounce(page.path, pageNameInput);
  63. }
  64. }, [isOpened, pageNameInput, subordinatedPages, checkExistPathsDebounce, page]);
  65. /**
  66. * change pageNameInput for PagePathAutoComplete
  67. * @param {string} value
  68. */
  69. function ppacInputChangeHandler(value) {
  70. setErrs(null);
  71. setPageNameInput(value);
  72. }
  73. /**
  74. * change pageNameInput
  75. * @param {string} value
  76. */
  77. function inputChangeHandler(value) {
  78. setErrs(null);
  79. setPageNameInput(value);
  80. }
  81. function changeIsDuplicateRecursivelyHandler() {
  82. setIsDuplicateRecursively(!isDuplicateRecursively);
  83. }
  84. useEffect(() => {
  85. if (page != null && isOpened) {
  86. updateSubordinatedList();
  87. setPageNameInput(page.path);
  88. }
  89. }, [isOpened, page, updateSubordinatedList]);
  90. const duplicate = useCallback(async() => {
  91. if (page == null) {
  92. return;
  93. }
  94. setErrs(null);
  95. const { pageId, path } = page;
  96. try {
  97. const { data } = await apiv3Post('/pages/duplicate', { pageId, pageNameInput, isRecursively: isDuplicateRecursively });
  98. const onDuplicated = duplicateModalData?.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, duplicateModalData?.opts?.onDuplicated, isDuplicateRecursively, page, pageNameInput]);
  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="form-group"><label>{t('modal_duplicate.label.Current page name')}</label><br />
  133. <code>{path}</code>
  134. </div>
  135. <div className="form-group">
  136. <label htmlFor="duplicatePageName">{ t('modal_duplicate.label.New page name') }</label><br />
  137. <div className="input-group">
  138. <div className="input-group-prepend">
  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="custom-control custom-checkbox custom-checkbox-warning mb-3">
  167. <input
  168. className="custom-control-input"
  169. name="recursively"
  170. id="cbDuplicateRecursively"
  171. type="checkbox"
  172. checked={isDuplicateRecursively}
  173. onChange={changeIsDuplicateRecursivelyHandler}
  174. />
  175. <label className="custom-control-label" htmlFor="cbDuplicateRecursively">
  176. { t('modal_duplicate.label.Recursively') }
  177. <p className="form-text text-muted mt-0">{ t('modal_duplicate.help.recursive') }</p>
  178. </label>
  179. <div>
  180. {isDuplicateRecursively && existingPaths.length !== 0 && (
  181. <div className="custom-control custom-checkbox custom-checkbox-warning">
  182. <input
  183. className="custom-control-input"
  184. name="withoutExistRecursively"
  185. id="cbDuplicatewithoutExistRecursively"
  186. type="checkbox"
  187. checked={isDuplicateRecursivelyWithoutExistPath}
  188. onChange={() => setIsDuplicateRecursivelyWithoutExistPath(!isDuplicateRecursivelyWithoutExistPath)}
  189. />
  190. <label className="custom-control-label" htmlFor="cbDuplicatewithoutExistRecursively">
  191. { t('modal_duplicate.label.Duplicate without exist path') }
  192. </label>
  193. </div>
  194. )}
  195. </div>
  196. <div>
  197. {isDuplicateRecursively && existingPaths.length !== 0 && (
  198. <DuplicatePathsTable existingPaths={existingPaths} fromPath={path} toPath={pageNameInput} />
  199. ) }
  200. </div>
  201. </div>
  202. </>
  203. );
  204. };
  205. const renderFooterContent = () => {
  206. if (!isOpened || page == null) {
  207. return <></>;
  208. }
  209. const submitButtonEnabled = existingPaths.length === 0
  210. || (isDuplicateRecursively && isDuplicateRecursivelyWithoutExistPath);
  211. return (
  212. <>
  213. <ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
  214. <button
  215. type="button"
  216. className="btn btn-primary"
  217. data-testid="btn-duplicate"
  218. onClick={duplicate}
  219. disabled={!submitButtonEnabled}
  220. >
  221. { t('modal_duplicate.label.Duplicate page') }
  222. </button>
  223. </>
  224. );
  225. };
  226. return (
  227. <Modal size="lg" isOpen={isOpened} toggle={closeDuplicateModal} data-testid="page-duplicate-modal" className="grw-duplicate-page" autoFocus={false}>
  228. <ModalHeader tag="h4" toggle={closeDuplicateModal} className="bg-primary text-light">
  229. { t('modal_duplicate.label.Duplicate page') }
  230. </ModalHeader>
  231. <ModalBody>
  232. {renderBodyContent()}
  233. </ModalBody>
  234. <ModalFooter>
  235. {renderFooterContent()}
  236. </ModalFooter>
  237. </Modal>
  238. );
  239. };
  240. export default PageDuplicateModal;