PageDuplicateModal.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import React, {
  2. useState, useEffect, useCallback, useMemo,
  3. } from 'react';
  4. import {
  5. Modal, ModalHeader, ModalBody, ModalFooter,
  6. } from 'reactstrap';
  7. import { useTranslation } from 'react-i18next';
  8. import { debounce } from 'throttle-debounce';
  9. import { apiv3Get, apiv3Post } from '~/client/util/apiv3-client';
  10. import { toastError } from '~/client/util/apiNotification';
  11. import { usePageDuplicateModal } from '~/stores/modal';
  12. import { useIsSearchServiceReachable, useSiteUrl } from '~/stores/context';
  13. import PagePathAutoComplete from './PagePathAutoComplete';
  14. import ApiErrorMessageList from './PageManagement/ApiErrorMessageList';
  15. import DuplicatePathsTable from './DuplicatedPathsTable';
  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 (page != null && pageNameInput !== page.path) {
  62. checkExistPathsDebounce(page.path, pageNameInput);
  63. }
  64. }, [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. if (page == null) {
  125. return <></>;
  126. }
  127. const { path } = page;
  128. const isTargetPageDuplicate = existingPaths.includes(pageNameInput);
  129. const submitButtonEnabled = existingPaths.length === 0
  130. || (isDuplicateRecursively && isDuplicateRecursivelyWithoutExistPath);
  131. return (
  132. <Modal size="lg" isOpen={isOpened} toggle={closeDuplicateModal} className="grw-duplicate-page" autoFocus={false}>
  133. <ModalHeader tag="h4" toggle={closeDuplicateModal} className="bg-primary text-light">
  134. { t('modal_duplicate.label.Duplicate page') }
  135. </ModalHeader>
  136. <ModalBody>
  137. <div className="form-group"><label>{t('modal_duplicate.label.Current page name')}</label><br />
  138. <code>{path}</code>
  139. </div>
  140. <div className="form-group">
  141. <label htmlFor="duplicatePageName">{ t('modal_duplicate.label.New page name') }</label><br />
  142. <div className="input-group">
  143. <div className="input-group-prepend">
  144. <span className="input-group-text">{siteUrl}</span>
  145. </div>
  146. <div className="flex-fill">
  147. {isReachable
  148. ? (
  149. <PagePathAutoComplete
  150. initializedPath={path}
  151. onSubmit={duplicate}
  152. onInputChange={ppacInputChangeHandler}
  153. autoFocus
  154. />
  155. )
  156. : (
  157. <input
  158. type="text"
  159. value={pageNameInput}
  160. className="form-control"
  161. onChange={e => inputChangeHandler(e.target.value)}
  162. required
  163. />
  164. )}
  165. </div>
  166. </div>
  167. </div>
  168. { isTargetPageDuplicate && (
  169. <p className="text-danger">Error: Target path is duplicated.</p>
  170. ) }
  171. <div className="custom-control custom-checkbox custom-checkbox-warning mb-3">
  172. <input
  173. className="custom-control-input"
  174. name="recursively"
  175. id="cbDuplicateRecursively"
  176. type="checkbox"
  177. checked={isDuplicateRecursively}
  178. onChange={changeIsDuplicateRecursivelyHandler}
  179. />
  180. <label className="custom-control-label" htmlFor="cbDuplicateRecursively">
  181. { t('modal_duplicate.label.Recursively') }
  182. <p className="form-text text-muted mt-0">{ t('modal_duplicate.help.recursive') }</p>
  183. </label>
  184. <div>
  185. {isDuplicateRecursively && existingPaths.length !== 0 && (
  186. <div className="custom-control custom-checkbox custom-checkbox-warning">
  187. <input
  188. className="custom-control-input"
  189. name="withoutExistRecursively"
  190. id="cbDuplicatewithoutExistRecursively"
  191. type="checkbox"
  192. checked={isDuplicateRecursivelyWithoutExistPath}
  193. onChange={() => setIsDuplicateRecursivelyWithoutExistPath(!isDuplicateRecursivelyWithoutExistPath)}
  194. />
  195. <label className="custom-control-label" htmlFor="cbDuplicatewithoutExistRecursively">
  196. { t('modal_duplicate.label.Duplicate without exist path') }
  197. </label>
  198. </div>
  199. )}
  200. </div>
  201. <div>
  202. {isDuplicateRecursively && existingPaths.length !== 0 && (
  203. <DuplicatePathsTable existingPaths={existingPaths} fromPath={path} toPath={pageNameInput} />
  204. ) }
  205. </div>
  206. </div>
  207. </ModalBody>
  208. <ModalFooter>
  209. <ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
  210. <button
  211. type="button"
  212. className="btn btn-primary"
  213. onClick={duplicate}
  214. disabled={!submitButtonEnabled}
  215. >
  216. { t('modal_duplicate.label.Duplicate page') }
  217. </button>
  218. </ModalFooter>
  219. </Modal>
  220. );
  221. };
  222. export default PageDuplicateModal;