Преглед изворни кода

Merge pull request #2556 from weseek/get-child-page-hitting-apiv3

Get child page hitting apiv3
itizawa пре 5 година
родитељ
комит
6bf0d4659e

+ 1 - 0
resource/locales/en_US/translation.json

@@ -299,6 +299,7 @@
     "label": {
     "label": {
       "Duplicate page": "Duplicate page",
       "Duplicate page": "Duplicate page",
       "New page name": "New page name",
       "New page name": "New page name",
+      "Fail to get subordinated pages": "Fail to get subordinated pages",
       "Current page name": "Current page name",
       "Current page name": "Current page name",
       "Duplicate with child": "Duplicate with child"
       "Duplicate with child": "Duplicate with child"
     }
     }

+ 1 - 0
resource/locales/ja_JP/translation.json

@@ -300,6 +300,7 @@
     "label": {
     "label": {
       "Duplicate page": "ページを複製する",
       "Duplicate page": "ページを複製する",
       "New page name": "複製後のページ名",
       "New page name": "複製後のページ名",
+      "Fail to get subordinated pages": "配下ページの取得に失敗しました",
       "Current page name": "現在のページ名",
       "Current page name": "現在のページ名",
       "Duplicate with child": "配下のページも一緒に複製する"
       "Duplicate with child": "配下のページも一緒に複製する"
     }
     }

+ 2 - 1
resource/locales/zh_CN/translation.json

@@ -296,7 +296,8 @@
 	"modal_duplicate": {
 	"modal_duplicate": {
 		"label": {
 		"label": {
 			"Duplicate page": "Duplicate page",
 			"Duplicate page": "Duplicate page",
-			"New page name": "New page name",
+      "New page name": "New page name",
+      "Fail to get subordinated pages": "Fail to get subordinated pages",
 			"Current page name": "Current page name",
 			"Current page name": "Current page name",
 			"Duplicate with child": "Duplicate with child"
 			"Duplicate with child": "Duplicate with child"
 		}
 		}

+ 0 - 2
src/client/js/components/Page/PageManagement.jsx

@@ -18,7 +18,6 @@ const PageManagement = (props) => {
 
 
   const { currentUser } = appContainer;
   const { currentUser } = appContainer;
   const isTopPagePath = isTopPage(path);
   const isTopPagePath = isTopPage(path);
-
   const [isPageRenameModalShown, setIsPageRenameModalShown] = useState(false);
   const [isPageRenameModalShown, setIsPageRenameModalShown] = useState(false);
   const [isPageDuplicateModalShown, setIsPageDuplicateModalShown] = useState(false);
   const [isPageDuplicateModalShown, setIsPageDuplicateModalShown] = useState(false);
   const [isPageTemplateModalShown, setIsPageTempleteModalShown] = useState(false);
   const [isPageTemplateModalShown, setIsPageTempleteModalShown] = useState(false);
@@ -35,7 +34,6 @@ const PageManagement = (props) => {
   function openPageDuplicateModalHandler() {
   function openPageDuplicateModalHandler() {
     setIsPageDuplicateModalShown(true);
     setIsPageDuplicateModalShown(true);
   }
   }
-
   function closePageDuplicateModalHandler() {
   function closePageDuplicateModalHandler() {
     setIsPageDuplicateModalShown(false);
     setIsPageDuplicateModalShown(false);
   }
   }

+ 23 - 8
src/client/js/components/PageDuplicateModal.jsx

@@ -1,4 +1,4 @@
-import React, { useState } from 'react';
+import React, { useState, useEffect, useCallback } from 'react';
 import PropTypes from 'prop-types';
 import PropTypes from 'prop-types';
 
 
 import {
 import {
@@ -25,11 +25,11 @@ const PageDuplicateModal = (props) => {
   const [pageNameInput, setPageNameInput] = useState(path);
   const [pageNameInput, setPageNameInput] = useState(path);
   const [errorCode, setErrorCode] = useState(null);
   const [errorCode, setErrorCode] = useState(null);
   const [errorMessage, setErrorMessage] = useState(null);
   const [errorMessage, setErrorMessage] = useState(null);
+  const [subordinatedPaths, setSubordinatedPaths] = useState([]);
+  const [getSubordinatedError, setGetSuborinatedError] = useState(null);
 
 
   const [isDuplicateRecursively, setIsDuplicateRecursively] = useState(true);
   const [isDuplicateRecursively, setIsDuplicateRecursively] = useState(true);
 
 
-  const duplicatedNewPaths = ['/hoge', '/hoge/hoge', '/test/test/test'];
-
   /**
   /**
    * change pageNameInput for PagePathAutoComplete
    * change pageNameInput for PagePathAutoComplete
    * @param {string} value
    * @param {string} value
@@ -50,6 +50,22 @@ const PageDuplicateModal = (props) => {
     setIsDuplicateRecursively(!isDuplicateRecursively);
     setIsDuplicateRecursively(!isDuplicateRecursively);
   }
   }
 
 
+  const getSubordinatedList = useCallback(async() => {
+    try {
+      const res = await appContainer.apiv3Get('/pages/subordinated-list', { path });
+      setSubordinatedPaths(res.data.resultPaths);
+    }
+    catch (err) {
+      setGetSuborinatedError(t('modal_duplicate.label.Fail to get subordinated pages'));
+    }
+  }, [appContainer, path, t]);
+
+  useEffect(() => {
+    if (props.isOpen) {
+      getSubordinatedList();
+    }
+  }, [props.isOpen, getSubordinatedList]);
+
   async function duplicate() {
   async function duplicate() {
     try {
     try {
       setErrorCode(null);
       setErrorCode(null);
@@ -74,9 +90,8 @@ const PageDuplicateModal = (props) => {
         { t('modal_duplicate.label.Duplicate page') }
         { t('modal_duplicate.label.Duplicate page') }
       </ModalHeader>
       </ModalHeader>
       <ModalBody>
       <ModalBody>
-        <div className="form-group">
-          <label>{ t('modal_duplicate.label.Current page name') }</label><br />
-          <code>{ path }</code>
+        <div className="form-group"><label>{t('modal_duplicate.label.Current page name')}</label><br />
+          <code>{path}</code>
         </div>
         </div>
         <div className="form-group">
         <div className="form-group">
           <label htmlFor="duplicatePageName">{ t('modal_duplicate.label.New page name') }</label><br />
           <label htmlFor="duplicatePageName">{ t('modal_duplicate.label.New page name') }</label><br />
@@ -120,9 +135,10 @@ const PageDuplicateModal = (props) => {
           </label>
           </label>
           <div>
           <div>
             <ul>
             <ul>
-              {isDuplicateRecursively && duplicatedNewPaths.map(duplicatedNewPath => <li>{duplicatedNewPath}</li>)}
+              {isDuplicateRecursively && subordinatedPaths.map(duplicatedNewPath => <li key={duplicatedNewPath}>{duplicatedNewPath}</li>)}
             </ul>
             </ul>
           </div>
           </div>
+          <div> {getSubordinatedError} </div>
         </div>
         </div>
       </ModalBody>
       </ModalBody>
       <ModalFooter>
       <ModalFooter>
@@ -130,7 +146,6 @@ const PageDuplicateModal = (props) => {
         <button type="button" className="btn btn-primary" onClick={duplicate}>Duplicate page</button>
         <button type="button" className="btn btn-primary" onClick={duplicate}>Duplicate page</button>
       </ModalFooter>
       </ModalFooter>
     </Modal>
     </Modal>
-
   );
   );
 };
 };
 
 

+ 20 - 0
src/server/routes/apiv3/pages.js

@@ -13,6 +13,7 @@ const router = express.Router();
  *    name: Pages
  *    name: Pages
  */
  */
 module.exports = (crowi) => {
 module.exports = (crowi) => {
+  const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
   const loginRequired = require('../../middlewares/login-required')(crowi, true);
   const loginRequired = require('../../middlewares/login-required')(crowi, true);
   const adminRequired = require('../../middlewares/admin-required')(crowi);
   const adminRequired = require('../../middlewares/admin-required')(crowi);
   const csrf = require('../../middlewares/csrf')(crowi);
   const csrf = require('../../middlewares/csrf')(crowi);
@@ -82,5 +83,24 @@ module.exports = (crowi) => {
     }
     }
   });
   });
 
 
+  router.get('/subordinated-list', accessTokenParser, loginRequired, async(req, res) => {
+    const { path } = req.query;
+
+    try {
+      const pageData = await Page.findByPath(path);
+
+      const result = await Page.findManageableListWithDescendants(pageData, req.user);
+
+      const resultPaths = result.map(element => element.path);
+
+      return res.apiv3({ resultPaths });
+    }
+    catch (err) {
+      res.code = 'unknown';
+      logger.error('Failed to find the path', err);
+      return res.apiv3Err(err, 500);
+    }
+
+  });
   return router;
   return router;
 };
 };