| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- import React, {
- useState, useEffect, useCallback,
- } from 'react';
- import PropTypes from 'prop-types';
- import {
- Modal, ModalHeader, ModalBody, ModalFooter,
- } from 'reactstrap';
- import { withTranslation } from 'react-i18next';
- import { debounce } from 'throttle-debounce';
- import { withUnstatedContainers } from './UnstatedUtils';
- import { toastError } from '../util/apiNotification';
- 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 {
- t, appContainer, pageContainer,
- } = props;
- const { path } = pageContainer.state;
- const { crowi } = appContainer.config;
- const [pageNameInput, setPageNameInput] = useState(path);
- 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 [isRenameRecursivelyWithoutExistPath, setIsRenameRecursivelyWithoutExistPath] = useState(true);
- function changeIsRenameRecursivelyHandler() {
- SetIsRenameRecursively(!isRenameRecursively);
- }
- function changeIsRenameRecursivelyWithoutExistPathHandler() {
- setIsRenameRecursivelyWithoutExistPath(!isRenameRecursivelyWithoutExistPath);
- }
- function changeIsRenameRedirectHandler() {
- SetIsRenameRedirect(!isRenameRedirect);
- }
- function changeIsRenameMetadataHandler() {
- SetIsRenameMetadata(!isRenameMetadata);
- }
- const updateSubordinatedList = useCallback(async() => {
- try {
- const res = await appContainer.apiv3Get('/pages/subordinated-list', { path });
- const { subordinatedPaths } = res.data;
- setSubordinatedPages(subordinatedPaths);
- }
- catch (err) {
- setErrs(err);
- toastError(t('modal_rename.label.Fail to get subordinated pages'));
- }
- }, [appContainer, path, t]);
- useEffect(() => {
- if (props.isOpen) {
- updateSubordinatedList();
- }
- }, [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
- */
- function inputChangeHandler(value) {
- setErrs(null);
- setPageNameInput(value);
- }
- async function rename() {
- setErrs(null);
- try {
- const response = await pageContainer.rename(
- pageNameInput,
- isRenameRecursively,
- isRenameRedirect,
- isRenameMetadata,
- );
- const { page } = response.data;
- const url = new URL(page.path, 'https://dummy');
- url.searchParams.append('renamedFrom', path);
- if (isRenameRedirect) {
- url.searchParams.append('withRedirect', true);
- }
- window.location.href = `${url.pathname}${url.search}`;
- }
- catch (err) {
- setErrs(err);
- }
- }
- return (
- <Modal size="lg" isOpen={props.isOpen} toggle={props.onClose} autoFocus={false}>
- <ModalHeader tag="h4" toggle={props.onClose} className="bg-primary text-light">
- { t('modal_rename.label.Move/Rename page') }
- </ModalHeader>
- <ModalBody>
- <div className="form-group">
- <label>{ t('modal_rename.label.Current page name') }</label><br />
- <code>{ path }</code>
- </div>
- <div className="form-group">
- <label htmlFor="newPageName">{ t('modal_rename.label.New page name') }</label><br />
- <div className="input-group">
- <div className="input-group-prepend">
- <span className="input-group-text">{crowi.url}</span>
- </div>
- <form className="flex-fill" onSubmit={(e) => { e.preventDefault(); rename() }}>
- <input
- type="text"
- value={pageNameInput}
- className="form-control"
- onChange={e => inputChangeHandler(e.target.value)}
- required
- autoFocus
- />
- </form>
- </div>
- </div>
- <div className="custom-control custom-checkbox custom-checkbox-warning">
- <input
- className="custom-control-input"
- name="recursively"
- id="cbRenameRecursively"
- type="checkbox"
- checked={isRenameRecursively}
- onChange={changeIsRenameRecursivelyHandler}
- />
- <label className="custom-control-label" htmlFor="cbRenameRecursively">
- { 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' }}
- >
- <input
- className="custom-control-input"
- name="withoutExistRecursively"
- id="cbRenamewithoutExistRecursively"
- type="checkbox"
- checked={isRenameRecursivelyWithoutExistPath}
- onChange={changeIsRenameRecursivelyWithoutExistPathHandler}
- />
- <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">
- <input
- className="custom-control-input"
- name="create_redirect"
- id="cbRenameRedirect"
- type="checkbox"
- checked={isRenameRedirect}
- onChange={changeIsRenameRedirectHandler}
- />
- <label className="custom-control-label" htmlFor="cbRenameRedirect">
- { t('modal_rename.label.Redirect') }
- <p className="form-text text-muted mt-0">{ t('modal_rename.help.redirect') }</p>
- </label>
- </div>
- <div className="custom-control custom-checkbox custom-checkbox-primary">
- <input
- className="custom-control-input"
- name="remain_metadata"
- id="cbRenameMetadata"
- type="checkbox"
- checked={isRenameMetadata}
- onChange={changeIsRenameMetadataHandler}
- />
- <label className="custom-control-label" htmlFor="cbRenameMetadata">
- { t('modal_rename.label.Do not update metadata') }
- <p className="form-text text-muted mt-0">{ t('modal_rename.help.metadata') }</p>
- </label>
- </div>
- <div> {subordinatedError} </div>
- </ModalBody>
- <ModalFooter>
- <ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
- <button
- type="button"
- className="btn btn-primary"
- onClick={rename}
- disabled={(isRenameRecursively && !isRenameRecursivelyWithoutExistPath && existingPaths.length !== 0)}
- >Rename
- </button>
- </ModalFooter>
- </Modal>
- );
- };
- /**
- * Wrapper component for using unstated
- */
- const PageRenameModalWrapper = withUnstatedContainers(PageRenameModal, [AppContainer, PageContainer]);
- PageRenameModal.propTypes = {
- t: PropTypes.func.isRequired, // i18next
- appContainer: PropTypes.instanceOf(AppContainer).isRequired,
- pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
- isOpen: PropTypes.bool.isRequired,
- onClose: PropTypes.func.isRequired,
- path: PropTypes.string.isRequired,
- };
- export default withTranslation()(PageRenameModalWrapper);
|