Просмотр исходного кода

move the method for update tag

yusuketk 7 лет назад
Родитель
Сommit
ef4aca44a3

+ 7 - 7
src/client/js/app.js

@@ -68,7 +68,7 @@ let pagePath;
 let pageContent = '';
 let markdown = '';
 let slackChannels;
-let tagsForNewPage = [];
+let pageTags = [];
 if (mainContent !== null) {
   pageId = mainContent.getAttribute('data-page-id') || null;
   pageRevisionId = mainContent.getAttribute('data-page-revision-id');
@@ -115,11 +115,11 @@ if (isEnabledPlugins) {
 }
 
 /**
- * receive tags for new page from EditTagModal
+ * receive tags from EditTagModal
  * @param {Array} tagData new tags
  */
-const setTagsForNewPage = function(tagData) {
-  tagsForNewPage = tagData;
+const setTagData = function(tagData) {
+  pageTags = tagData;
 };
 
 /**
@@ -200,7 +200,7 @@ const saveWithShortcut = function(markdown) {
   // get options
   const options = componentInstances.savePageControls.getCurrentOptionsToSave();
   options.socketClientId = socketClientId;
-  options.tagsForNewPage = tagsForNewPage;
+  options.pageTags = pageTags;
 
   if (editorMode === 'hackmd') {
     // set option to sync
@@ -238,7 +238,7 @@ const saveWithSubmitButton = function(submitOpts) {
   // get options
   const options = componentInstances.savePageControls.getCurrentOptionsToSave();
   options.socketClientId = socketClientId;
-  options.tagsForNewPage = tagsForNewPage;
+  options.pageTags = pageTags;
 
   // set 'submitOpts.overwriteScopesOfDescendants' to options
   options.overwriteScopesOfDescendants = submitOpts ? !!submitOpts.overwriteScopesOfDescendants : false;
@@ -308,7 +308,7 @@ if (pageId) {
 }
 if (pagePath) {
   componentMappings.page = <Page crowi={crowi} crowiRenderer={crowiRenderer} markdown={markdown} pagePath={pagePath} onSaveWithShortcut={saveWithShortcut} />;
-  componentMappings['revision-path'] = <RevisionPath pageId={pageId} pagePath={pagePath} crowi={crowi} sendTagData={setTagsForNewPage} />;
+  componentMappings['revision-path'] = <RevisionPath pageId={pageId} pagePath={pagePath} crowi={crowi} sendTagData={setTagData} />;
   componentMappings['revision-url'] = <RevisionUrl pageId={pageId} pagePath={pagePath} />;
 }
 

+ 3 - 31
src/client/js/components/Page/EditTagModal.jsx

@@ -4,7 +4,6 @@ import Button from 'react-bootstrap/es/Button';
 import OverlayTrigger from 'react-bootstrap/es/OverlayTrigger';
 import Tooltip from 'react-bootstrap/es/Tooltip';
 import Modal from 'react-bootstrap/es/Modal';
-import * as toastr from 'toastr';
 import PageTagForm from '../PageTagForm';
 
 export default class EditTagModal extends React.Component {
@@ -46,36 +45,9 @@ export default class EditTagModal extends React.Component {
     this.setState({ isOpenModal: true });
   }
 
-  async handleSubmit() {
-    if (this.props.pageId) {
-      try {
-        const res = await this.props.crowi.apiPost('/pages.updateTags', { pageId: this.props.pageId, newPageTags: this.state.newPageTags });
-        this.setState({ currentPageTags: res.nextTags, isOpenModal: false });
-        toastr.success(undefined, 'Updated tags successfully', {
-          closeButton: true,
-          progressBar: true,
-          newestOnTop: false,
-          showDuration: '100',
-          hideDuration: '100',
-          timeOut: '1200',
-          extendedTimeOut: '150',
-        });
-      }
-      catch (err) {
-        toastr.error(err, 'Error occured on updating tags', {
-          closeButton: true,
-          progressBar: true,
-          newestOnTop: false,
-          showDuration: '100',
-          hideDuration: '100',
-          timeOut: '3000',
-        });
-      }
-    }
-    else {
-      this.props.sendTagData(this.state.newPageTags); // for setting tag when create new page
-      this.setState({ currentPageTags: this.state.newPageTags, isOpenModal: false });
-    }
+  handleSubmit() {
+    this.props.sendTagData(this.state.newPageTags); // for setting tag data
+    this.setState({ currentPageTags: this.state.newPageTags, isOpenModal: false });
   }
 
   render() {

+ 9 - 3
src/server/models/page.js

@@ -976,7 +976,7 @@ module.exports = function(crowi) {
     const redirectTo = options.redirectTo || null;
     const grantUserGroupId = options.grantUserGroupId || null;
     const socketClientId = options.socketClientId || null;
-    const tags = options.tags || null;
+    const pageTags = options.pageTags || null;
 
     // sanitize path
     path = crowi.xss.process(path); // eslint-disable-line no-param-reassign
@@ -1003,8 +1003,8 @@ module.exports = function(crowi) {
     await validateAppliedScope(user, grant, grantUserGroupId);
     page.applyScope(user, grant, grantUserGroupId);
 
-    if (tags != null) {
-      page.updateTags(tags);
+    if (pageTags != null) {
+      page.updateTags(pageTags);
     }
 
     let savedPage = await page.save();
@@ -1028,6 +1028,7 @@ module.exports = function(crowi) {
     const grantUserGroupId = options.grantUserGroupId || null;
     const isSyncRevisionToHackmd = options.isSyncRevisionToHackmd;
     const socketClientId = options.socketClientId || null;
+    const pageTags = options.pageTags || null;
 
     await validateAppliedScope(user, grant, grantUserGroupId);
     pageData.applyScope(user, grant, grantUserGroupId);
@@ -1047,6 +1048,11 @@ module.exports = function(crowi) {
     if (socketClientId != null) {
       pageEvent.emit('update', savedPage, user, socketClientId);
     }
+
+    if (pageTags != null) {
+      savedPage.updateTags(pageTags);
+    }
+
     return savedPage;
   };
 

+ 0 - 1
src/server/routes/index.js

@@ -201,7 +201,6 @@ module.exports = function(crowi, app) {
   app.post('/_api/pages.revertRemove' , loginRequired(crowi, app) , csrf, page.api.revertRemove); // (Avoid from API Token)
   app.post('/_api/pages.unlink'       , loginRequired(crowi, app) , csrf, page.api.unlink); // (Avoid from API Token)
   app.post('/_api/pages.duplicate'    , accessTokenParser, loginRequired(crowi, app), csrf, page.api.duplicate);
-  app.post('/_api/pages.updateTags'   , accessTokenParser, loginRequired(crowi, app), csrf, page.api.updateTags);
   app.get('/_api/tags.search'         , accessTokenParser, loginRequired(crowi, app, false), tag.api.search);
   app.get('/_api/tags.get'            , accessTokenParser, loginRequired(crowi, app, false), tag.api.get);
   app.get('/_api/comments.get'        , accessTokenParser , loginRequired(crowi, app, false) , comment.api.get);

+ 5 - 27
src/server/routes/page.js

@@ -537,6 +537,7 @@ module.exports = function(crowi, app) {
    * @apiParam {String} body
    * @apiParam {String} path
    * @apiParam {String} grant
+   * @apiParam {Array} pageTags
    */
   api.create = async function(req, res) {
     const body = req.body.body || null;
@@ -547,7 +548,7 @@ module.exports = function(crowi, app) {
     const isSlackEnabled = !!req.body.isSlackEnabled; // cast to boolean
     const slackChannels = req.body.slackChannels || null;
     const socketClientId = req.body.socketClientId || undefined;
-    const tags = req.body.tagsForNewPage || undefined;
+    const pageTags = req.body.pageTags || undefined;
 
     if (body === null || pagePath === null) {
       return res.json(ApiResponse.error('Parameters body and path are required.'));
@@ -560,7 +561,7 @@ module.exports = function(crowi, app) {
     }
 
     const options = {
-      grant, grantUserGroupId, overwriteScopesOfDescendants, socketClientId, tags,
+      grant, grantUserGroupId, overwriteScopesOfDescendants, socketClientId, pageTags,
     };
     const createdPage = await Page.create(pagePath, body, req.user, options);
 
@@ -613,6 +614,7 @@ module.exports = function(crowi, app) {
     const slackChannels = req.body.slackChannels || null;
     const isSyncRevisionToHackmd = !!req.body.isSyncRevisionToHackmd; // cast to boolean
     const socketClientId = req.body.socketClientId || undefined;
+    const pageTags = req.body.pageTags || undefined;
 
     if (pageId === null || pageBody === null) {
       return res.json(ApiResponse.error('page_id and body are required.'));
@@ -630,7 +632,7 @@ module.exports = function(crowi, app) {
       return res.json(ApiResponse.error('Posted param "revisionId" is outdated.', 'outdated'));
     }
 
-    const options = { isSyncRevisionToHackmd, socketClientId };
+    const options = { isSyncRevisionToHackmd, socketClientId, pageTags };
     if (grant != null) {
       options.grant = grant;
     }
@@ -671,30 +673,6 @@ module.exports = function(crowi, app) {
     }
   };
 
-  /**
-   * @api {post} /pages.updateTags update page tags
-   * @apiName UpdateTags
-   * @apiGroup Page
-   *
-   * @apiParam {ObjectId} pageId
-   * @apiParam {Array} newPageTags
-   */
-  api.updateTags = async function(req, res) {
-    const pageId = req.body.pageId;
-    const newPageTags = req.body.newPageTags;
-    const result = {};
-    try {
-      const page = await Page.findOne({ _id: pageId });
-      // update page tag
-      result.nextTags = await page.updateTags(newPageTags);
-    }
-    catch (err) {
-      return res.json(ApiResponse.error(err));
-    }
-
-    return res.json(ApiResponse.success(result));
-  };
-
   /**
    * @api {get} /pages.get Get page data
    * @apiName GetPage