Steven Fukase 4 лет назад
Родитель
Сommit
3095f247ba
2 измененных файлов с 34 добавлено и 22 удалено
  1. 20 22
      src/client/js/components/PageCreateModal.jsx
  2. 14 0
      src/lib/util/path-utils.js

+ 20 - 22
src/client/js/components/PageCreateModal.jsx

@@ -6,19 +6,17 @@ import { Modal, ModalHeader, ModalBody } from 'reactstrap';
 
 import { withTranslation } from 'react-i18next';
 import { format } from 'date-fns';
-import urljoin from 'url-join';
 
-import { userPageRoot, isCreatablePage } from '@commons/util/path-utils';
+import { userPageRoot, isCreatablePage, joinAndRedirectToEditor } from '@commons/util/path-utils';
 import { pathUtils } from 'growi-commons';
 
 import AppContainer from '../services/AppContainer';
 import NavigationContainer from '../services/NavigationContainer';
 import { withUnstatedContainers } from './UnstatedUtils';
+import { toastError } from '../util/apiNotification';
 
 import PagePathAutoComplete from './PagePathAutoComplete';
 
-import { toastError } from '../util/apiNotification';
-
 const PageCreateModal = (props) => {
   const { t, appContainer, navigationContainer } = props;
 
@@ -72,21 +70,6 @@ const PageCreateModal = (props) => {
     setTemplate(value);
   }
 
-  /**
-   * join paths, check url, then redirect to edit page
-   */
-  function joinAndRedirect(...path) {
-
-    const joinedUrl = encodeURI(urljoin(...path));
-
-    if (!isCreatablePage(joinedUrl)) {
-      toastError(new Error('Invalid characters found.'));
-      return;
-    }
-
-    window.location.href = urljoin(joinedUrl, '#edit');
-  }
-
   /**
    * access today page
    */
@@ -95,14 +78,24 @@ const PageCreateModal = (props) => {
     if (tmpTodayInput1 === '') {
       tmpTodayInput1 = t('Memo');
     }
-    joinAndRedirect(userPageRootPath, tmpTodayInput1, now, todayInput2);
+    try {
+      joinAndRedirectToEditor(userPageRootPath, tmpTodayInput1, now, todayInput2);
+    }
+    catch (err) {
+      toastError(err);
+    }
   }
 
   /**
    * access input page
    */
   function createInputPage() {
-    joinAndRedirect(pageNameInput);
+    try {
+      joinAndRedirectToEditor(pageNameInput);
+    }
+    catch (err) {
+      toastError(err);
+    }
   }
 
   function ppacInputChangeHandler(value) {
@@ -118,7 +111,12 @@ const PageCreateModal = (props) => {
    */
   function createTemplatePage(e) {
     const pageName = (template === 'children') ? '_template' : '__template';
-    joinAndRedirect(pathname, pageName);
+    try {
+      joinAndRedirectToEditor(pathname, pageName);
+    }
+    catch (err) {
+      toastError(err);
+    }
   }
 
   function renderCreateTodayForm() {

+ 14 - 0
src/lib/util/path-utils.js

@@ -1,3 +1,4 @@
+const urljoin = require('url-join');
 const escapeStringRegexp = require('escape-string-regexp');
 
 /**
@@ -69,6 +70,18 @@ const isCreatablePage = (path) => {
   return isCreatable;
 };
 
+/**
+ * join paths, check url, then redirect to edit page
+ * @param {string} paths
+ */
+function joinAndRedirectToEditor(...paths) {
+  const joinedUrl = encodeURI(urljoin(...paths));
+  if (!isCreatablePage(joinedUrl)) {
+    return new Error('Invalid characters found.');
+  }
+  window.location.href = urljoin(joinedUrl, '#edit');
+}
+
 /**
  * return user path
  * @param {Object} user
@@ -116,6 +129,7 @@ module.exports = {
   isTrashPage,
   isUserPage,
   isCreatablePage,
+  joinAndRedirectToEditor,
   userPageRoot,
   convertToNewAffiliationPath,
   encodeSpaces,