PageDuplicateModal.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. const ppacInputChangeHandler = useCallback((value: string) => {
  66. setErrs(null);
  67. setPageNameInput(value);
  68. }, []);
  69. /**
  70. * change pageNameInput
  71. * @param {string} value
  72. */
  73. function inputChangeHandler(value) {
  74. setErrs(null);
  75. setPageNameInput(value);
  76. }
  77. function changeIsDuplicateRecursivelyHandler() {
  78. setIsDuplicateRecursively(!isDuplicateRecursively);
  79. }
  80. useEffect(() => {
  81. if (page != null && isOpened) {
  82. updateSubordinatedList();
  83. setPageNameInput(page.path);
  84. }
  85. }, [isOpened, page, updateSubordinatedList]);
  86. const duplicate = useCallback(async() => {
  87. if (page == null) {
  88. return;
  89. }
  90. setErrs(null);
  91. const { pageId, path } = page;
  92. try {
  93. const { data } = await apiv3Post('/pages/duplicate', { pageId, pageNameInput, isRecursively: isDuplicateRecursively });
  94. const onDuplicated = duplicateModalData?.opts?.onDuplicated;
  95. const fromPath = path;
  96. const toPath = data.page.path;
  97. if (onDuplicated != null) {
  98. onDuplicated(fromPath, toPath);
  99. }
  100. closeDuplicateModal();
  101. }
  102. catch (err) {
  103. setErrs(err);
  104. }
  105. }, [closeDuplicateModal, duplicateModalData?.opts?.onDuplicated, isDuplicateRecursively, page, pageNameInput]);
  106. useEffect(() => {
  107. if (isOpened) {
  108. return;
  109. }
  110. // reset states after the modal closed
  111. setTimeout(() => {
  112. setPageNameInput('');
  113. setErrs(null);
  114. setSubordinatedPages([]);
  115. setExistingPaths([]);
  116. setIsDuplicateRecursively(true);
  117. setIsDuplicateRecursivelyWithoutExistPath(false);
  118. }, 1000);
  119. }, [isOpened]);
  120. const renderBodyContent = () => {
  121. if (!isOpened || page == null) {
  122. return <></>;
  123. }
  124. const { path } = page;
  125. const isTargetPageDuplicate = existingPaths.includes(pageNameInput);
  126. return (
  127. <>
  128. <div><label className="form-label">{t('modal_duplicate.label.Current page name')}</label><br />
  129. <code>{path}</code>
  130. </div>
  131. <div>
  132. <label className="form-label" htmlFor="duplicatePageName">{ t('modal_duplicate.label.New page name') }</label><br />
  133. <div className="input-group">
  134. <div>
  135. <span className="input-group-text">{siteUrl}</span>
  136. </div>
  137. <div className="flex-fill">
  138. {isReachable
  139. ? (
  140. <PagePathAutoComplete
  141. initializedPath={path}
  142. onSubmit={duplicate}
  143. onInputChange={ppacInputChangeHandler}
  144. autoFocus
  145. />
  146. )
  147. : (
  148. <input
  149. type="text"
  150. value={pageNameInput}
  151. className="form-control"
  152. onChange={e => inputChangeHandler(e.target.value)}
  153. required
  154. />
  155. )}
  156. </div>
  157. </div>
  158. </div>
  159. { isTargetPageDuplicate && (
  160. <p className="text-danger">Error: Target path is duplicated.</p>
  161. ) }
  162. <div className="form-check form-check-warning mb-3">
  163. <input
  164. className="form-check-input"
  165. name="recursively"
  166. id="cbDuplicateRecursively"
  167. type="checkbox"
  168. checked={isDuplicateRecursively}
  169. onChange={changeIsDuplicateRecursivelyHandler}
  170. />
  171. <label className="form-label form-check-label" htmlFor="cbDuplicateRecursively">
  172. { t('modal_duplicate.label.Recursively') }
  173. <p className="form-text text-muted mt-0">{ t('modal_duplicate.help.recursive') }</p>
  174. </label>
  175. <div>
  176. {isDuplicateRecursively && existingPaths.length !== 0 && (
  177. <div className="form-check form-check-warning">
  178. <input
  179. className="form-check-input"
  180. name="withoutExistRecursively"
  181. id="cbDuplicatewithoutExistRecursively"
  182. type="checkbox"
  183. checked={isDuplicateRecursivelyWithoutExistPath}
  184. onChange={() => setIsDuplicateRecursivelyWithoutExistPath(!isDuplicateRecursivelyWithoutExistPath)}
  185. />
  186. <label className="form-label form-check-label" htmlFor="cbDuplicatewithoutExistRecursively">
  187. { t('modal_duplicate.label.Duplicate without exist path') }
  188. </label>
  189. </div>
  190. )}
  191. </div>
  192. <div>
  193. {isDuplicateRecursively && existingPaths.length !== 0 && (
  194. <DuplicatePathsTable existingPaths={existingPaths} fromPath={path} toPath={pageNameInput} />
  195. ) }
  196. </div>
  197. </div>
  198. </>
  199. );
  200. };
  201. const renderFooterContent = () => {
  202. if (!isOpened || page == null) {
  203. return <></>;
  204. }
  205. const submitButtonEnabled = existingPaths.length === 0
  206. || (isDuplicateRecursively && isDuplicateRecursivelyWithoutExistPath);
  207. return (
  208. <>
  209. <ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
  210. <button
  211. type="button"
  212. className="btn btn-primary"
  213. data-testid="btn-duplicate"
  214. onClick={duplicate}
  215. disabled={!submitButtonEnabled}
  216. >
  217. { t('modal_duplicate.label.Duplicate page') }
  218. </button>
  219. </>
  220. );
  221. };
  222. return (
  223. <Modal size="lg" isOpen={isOpened} toggle={closeDuplicateModal} data-testid="page-duplicate-modal" className="grw-duplicate-page" autoFocus={false}>
  224. <ModalHeader tag="h4" toggle={closeDuplicateModal} className="bg-primary text-light">
  225. { t('modal_duplicate.label.Duplicate page') }
  226. </ModalHeader>
  227. <ModalBody>
  228. {renderBodyContent()}
  229. </ModalBody>
  230. <ModalFooter>
  231. {renderFooterContent()}
  232. </ModalFooter>
  233. </Modal>
  234. );
  235. };
  236. export default PageDuplicateModal;