import React, { useState } from 'react';
import PropTypes from 'prop-types';
import {
Modal, ModalHeader, ModalBody, ModalFooter,
} from 'reactstrap';
import { withTranslation } from 'react-i18next';
import { withUnstatedContainers } from './UnstatedUtils';
import AppContainer from '../services/AppContainer';
import PageContainer from '../services/PageContainer';
import PagePathAutoComplete from './PagePathAutoComplete';
import ApiErrorMessageList from './PageManagement/ApiErrorMessageList';
const PageDuplicateModal = (props) => {
const { t, appContainer, pageContainer } = props;
const config = appContainer.getConfig();
const isReachable = config.isSearchServiceReachable;
const { pageId, path } = pageContainer.state;
const { crowi } = appContainer.config;
const [pageNameInput, setPageNameInput] = useState(path);
const [errs, setErrs] = useState(null);
const [getSubordinatedError] = useState(null);
const [isDuplicateRecursively, setIsDuplicateRecursively] = useState(true);
const [isDuplicateRecursivelyWithoutExistPath, setIsDuplicateRecursivelyWithoutExistPath] = useState(true);
const [isDuplicateExist, setIsDuplicateExist] = useState([]);
function createSubordinatedList(value) {
// ToDo: get the duplicated list from sever
// below is psuedo code
// let duplicatedList = get.apiv3......
// duplicatedList = duplicatedList.map((value) =>
//
{value}: { t('modal_duplicate.label.Same page already exists') } ; )
// setIsDuplicateExist(duplicatedList);
// for now we use dummy path
setIsDuplicateExist(['/test146/test147', value]);
}
/**
* change pageNameInput for PagePathAutoComplete
* @param {string} value
*/
function ppacInputChangeHandler(value) {
createSubordinatedList(value);
setPageNameInput(value);
}
/**
* change pageNameInput
* @param {string} value
*/
function inputChangeHandler(value) {
createSubordinatedList(value);
setPageNameInput(value);
}
function changeIsDuplicateRecursivelyHandler() {
setIsDuplicateRecursively(!isDuplicateRecursively);
}
function changeIsDuplicateRecursivelyWithoutExistPathHandler() {
setIsDuplicateRecursivelyWithoutExistPath(!isDuplicateRecursivelyWithoutExistPath);
}
// function changeIsExistHandler() {
// setIsExist(true);
// }
async function duplicate() {
setErrs(null);
try {
const res = await appContainer.apiv3Post('/pages/duplicate', { pageId, pageNameInput });
const { page } = res.data;
window.location.href = encodeURI(`${page.path}?duplicated=${path}`);
}
catch (err) {
setErrs(err);
}
}
function ppacSubmitHandler() {
duplicate();
}
return (
{ t('modal_duplicate.label.Duplicate page') }
{path}
{crowi.url}
{isReachable
? (
)
: (
inputChangeHandler(e.target.value)}
required
/>
)}
{isDuplicateRecursively && isDuplicateExist.length !== 0 && isDuplicateExist}
{getSubordinatedError}
);
};
/**
* Wrapper component for using unstated
*/
const PageDuplicateModallWrapper = withUnstatedContainers(PageDuplicateModal, [AppContainer, PageContainer]);
PageDuplicateModal.propTypes = {
t: PropTypes.func.isRequired, // i18next
appContainer: PropTypes.instanceOf(AppContainer).isRequired,
pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
isOpen: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired,
};
export default withTranslation()(PageDuplicateModallWrapper);