import React, { useState } from 'react'; import PropTypes from 'prop-types'; import { Modal, ModalHeader, ModalBody } from 'reactstrap'; import { withTranslation } from 'react-i18next'; import { format } from 'date-fns'; import { pagePathUtils, pathUtils } from '@growi/core'; import AppContainer from '~/client/services/AppContainer'; import { withUnstatedContainers } from './UnstatedUtils'; import { toastError } from '~/client/util/apiNotification'; import { usePageCreateModalOpened } from '~/stores/ui'; import PagePathAutoComplete from './PagePathAutoComplete'; const { userPageRoot, isCreatablePage, generateEditorPath, } = pagePathUtils; const PageCreateModal = (props) => { const { t, appContainer } = props; const { data: isPageCreateModalOpened, mutate: mutatePageCreateModalOpened } = usePageCreateModalOpened(); const config = appContainer.getConfig(); const isReachable = config.isSearchServiceReachable; const pathname = decodeURI(window.location.pathname); const userPageRootPath = userPageRoot(appContainer.currentUser); const pageNameInputInitialValue = isCreatablePage(pathname) ? pathUtils.addTrailingSlash(pathname) : '/'; const now = format(new Date(), 'yyyy/MM/dd'); const [todayInput1, setTodayInput1] = useState(t('Memo')); const [todayInput2, setTodayInput2] = useState(''); const [pageNameInput, setPageNameInput] = useState(pageNameInputInitialValue); const [template, setTemplate] = useState(null); function transitBySubmitEvent(e, transitHandler) { // prevent page transition by submit e.preventDefault(); transitHandler(); } /** * change todayInput1 * @param {string} value */ function onChangeTodayInput1Handler(value) { setTodayInput1(value); } /** * change todayInput2 * @param {string} value */ function onChangeTodayInput2Handler(value) { setTodayInput2(value); } /** * change pageNameInput * @param {string} value */ function onChangePageNameInputHandler(value) { setPageNameInput(value); } /** * change template * @param {string} value */ function onChangeTemplateHandler(value) { setTemplate(value); } /** * join path, check if creatable, then redirect * @param {string} paths */ async function redirectToEditor(...paths) { try { const editorPath = await generateEditorPath(...paths); window.location.href = editorPath; } catch (err) { toastError(err); } } /** * access today page */ function createTodayPage() { let tmpTodayInput1 = todayInput1; if (tmpTodayInput1 === '') { tmpTodayInput1 = t('Memo'); } redirectToEditor(userPageRootPath, tmpTodayInput1, now, todayInput2); } /** * access input page */ function createInputPage() { redirectToEditor(pageNameInput); } function ppacInputChangeHandler(value) { setPageNameInput(value); } function ppacSubmitHandler() { createInputPage(); } /** * access template page */ function createTemplatePage(e) { const pageName = (template === 'children') ? '_template' : '__template'; redirectToEditor(pathname, pageName); } function renderCreateTodayForm() { return (