|
|
@@ -1,4 +1,6 @@
|
|
|
-import React, { useState, useEffect, useCallback } from 'react';
|
|
|
+import React, {
|
|
|
+ useState, useEffect, useCallback,
|
|
|
+} from 'react';
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
import {
|
|
|
@@ -7,6 +9,7 @@ import {
|
|
|
|
|
|
import { withTranslation } from 'react-i18next';
|
|
|
|
|
|
+import { debounce } from 'throttle-debounce';
|
|
|
import { withUnstatedContainers } from './UnstatedUtils';
|
|
|
import { toastError } from '../util/apiNotification';
|
|
|
|
|
|
@@ -14,6 +17,8 @@ import AppContainer from '../services/AppContainer';
|
|
|
import PageContainer from '../services/PageContainer';
|
|
|
import ApiErrorMessageList from './PageManagement/ApiErrorMessageList';
|
|
|
import ComparePathsTable from './ComparePathsTable';
|
|
|
+import DuplicatedPathsTable from './DuplicatedPathsTable';
|
|
|
+
|
|
|
|
|
|
const PageRenameModal = (props) => {
|
|
|
const {
|
|
|
@@ -29,19 +34,19 @@ const PageRenameModal = (props) => {
|
|
|
const [errs, setErrs] = useState(null);
|
|
|
|
|
|
const [subordinatedPages, setSubordinatedPages] = useState([]);
|
|
|
+ const [existingPaths, setExistingPaths] = useState([]);
|
|
|
const [isRenameRecursively, SetIsRenameRecursively] = useState(true);
|
|
|
const [isRenameRedirect, SetIsRenameRedirect] = useState(false);
|
|
|
const [isRenameMetadata, SetIsRenameMetadata] = useState(false);
|
|
|
const [subordinatedError] = useState(null);
|
|
|
- const [isDuplicateRecursivelyWithoutExistPath, setIsDuplicateRecursivelyWithoutExistPath] = useState(true);
|
|
|
-
|
|
|
+ const [isRenameRecursivelyWithoutExistPath, setIsRenameRecursivelyWithoutExistPath] = useState(true);
|
|
|
|
|
|
function changeIsRenameRecursivelyHandler() {
|
|
|
SetIsRenameRecursively(!isRenameRecursively);
|
|
|
}
|
|
|
|
|
|
- function changeIsDuplicateRecursivelyWithoutExistPathHandler() {
|
|
|
- setIsDuplicateRecursivelyWithoutExistPath(!isDuplicateRecursivelyWithoutExistPath);
|
|
|
+ function changeIsRenameRecursivelyWithoutExistPathHandler() {
|
|
|
+ setIsRenameRecursivelyWithoutExistPath(!isRenameRecursivelyWithoutExistPath);
|
|
|
}
|
|
|
|
|
|
function changeIsRenameRedirectHandler() {
|
|
|
@@ -60,7 +65,7 @@ const PageRenameModal = (props) => {
|
|
|
}
|
|
|
catch (err) {
|
|
|
setErrs(err);
|
|
|
- toastError(t('modal_duplicate.label.Fail to get subordinated pages'));
|
|
|
+ toastError(t('modal_rename.label.Fail to get subordinated pages'));
|
|
|
}
|
|
|
}, [appContainer, path, t]);
|
|
|
|
|
|
@@ -70,6 +75,30 @@ const PageRenameModal = (props) => {
|
|
|
}
|
|
|
}, [props.isOpen, updateSubordinatedList]);
|
|
|
|
|
|
+
|
|
|
+ const checkExistPaths = async(newParentPath) => {
|
|
|
+ try {
|
|
|
+ const res = await appContainer.apiv3Get('/page/exist-paths', { fromPath: path, toPath: newParentPath });
|
|
|
+ const { existPaths } = res.data;
|
|
|
+ setExistingPaths(existPaths);
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ setErrs(err);
|
|
|
+ toastError(t('modal_rename.label.Fail to get exist path'));
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
+ const checkExistPathsDebounce = useCallback(
|
|
|
+ debounce(1000, checkExistPaths), [],
|
|
|
+ );
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (pageNameInput !== path) {
|
|
|
+ checkExistPathsDebounce(pageNameInput, subordinatedPages);
|
|
|
+ }
|
|
|
+ }, [pageNameInput, subordinatedPages, path, checkExistPathsDebounce]);
|
|
|
+
|
|
|
/**
|
|
|
* change pageNameInput
|
|
|
* @param {string} value
|
|
|
@@ -144,6 +173,7 @@ const PageRenameModal = (props) => {
|
|
|
{ t('modal_rename.label.Recursively') }
|
|
|
<p className="form-text text-muted mt-0">{ t('modal_rename.help.recursive') }</p>
|
|
|
</label>
|
|
|
+ {existingPaths.length !== 0 && (
|
|
|
<div
|
|
|
className="custom-control custom-checkbox custom-checkbox-warning"
|
|
|
style={{ display: isRenameRecursively ? '' : 'none' }}
|
|
|
@@ -151,16 +181,18 @@ const PageRenameModal = (props) => {
|
|
|
<input
|
|
|
className="custom-control-input"
|
|
|
name="withoutExistRecursively"
|
|
|
- id="cbDuplicatewithoutExistRecursively"
|
|
|
+ id="cbRenamewithoutExistRecursively"
|
|
|
type="checkbox"
|
|
|
- checked={isDuplicateRecursivelyWithoutExistPath}
|
|
|
- onChange={changeIsDuplicateRecursivelyWithoutExistPathHandler}
|
|
|
+ checked={isRenameRecursivelyWithoutExistPath}
|
|
|
+ onChange={changeIsRenameRecursivelyWithoutExistPathHandler}
|
|
|
/>
|
|
|
- <label className="custom-control-label" htmlFor="cbDuplicatewithoutExistRecursively">
|
|
|
- { t('modal_duplicate.label.Duplicate without exist path') }
|
|
|
+ <label className="custom-control-label" htmlFor="cbRenamewithoutExistRecursively">
|
|
|
+ { t('modal_rename.label.Rename without exist path') }
|
|
|
</label>
|
|
|
</div>
|
|
|
+)}
|
|
|
{isRenameRecursively && <ComparePathsTable subordinatedPages={subordinatedPages} newPagePath={pageNameInput} />}
|
|
|
+ {isRenameRecursively && existingPaths.length !== 0 && <DuplicatedPathsTable existingPaths={existingPaths} oldPagePath={pageNameInput} />}
|
|
|
</div>
|
|
|
|
|
|
<div className="custom-control custom-checkbox custom-checkbox-success">
|
|
|
@@ -196,7 +228,13 @@ const PageRenameModal = (props) => {
|
|
|
</ModalBody>
|
|
|
<ModalFooter>
|
|
|
<ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
|
|
|
- <button type="button" className="btn btn-primary" onClick={rename}>Rename</button>
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ className="btn btn-primary"
|
|
|
+ onClick={rename}
|
|
|
+ disabled={(isRenameRecursively && !isRenameRecursivelyWithoutExistPath && existingPaths.length !== 0)}
|
|
|
+ >Rename
|
|
|
+ </button>
|
|
|
</ModalFooter>
|
|
|
</Modal>
|
|
|
);
|