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

can set tag when create new page

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

+ 8 - 1
src/client/js/app.js

@@ -68,6 +68,7 @@ let pagePath;
 let pageContent = '';
 let markdown = '';
 let slackChannels;
+let tagsForNewPage = [];
 if (mainContent !== null) {
   pageId = mainContent.getAttribute('data-page-id') || null;
   pageRevisionId = mainContent.getAttribute('data-page-revision-id');
@@ -191,6 +192,7 @@ const saveWithShortcut = function(markdown) {
   // get options
   const options = componentInstances.savePageControls.getCurrentOptionsToSave();
   options.socketClientId = socketClientId;
+  options.tagsForNewPage = tagsForNewPage;
 
   if (editorMode === 'hackmd') {
     // set option to sync
@@ -228,6 +230,7 @@ const saveWithSubmitButton = function(submitOpts) {
   // get options
   const options = componentInstances.savePageControls.getCurrentOptionsToSave();
   options.socketClientId = socketClientId;
+  options.tagsForNewPage = tagsForNewPage;
 
   // set 'submitOpts.overwriteScopesOfDescendants' to options
   options.overwriteScopesOfDescendants = submitOpts ? !!submitOpts.overwriteScopesOfDescendants : false;
@@ -271,6 +274,10 @@ if (!pageRevisionId && draft != null) {
   markdown = draft;
 }
 
+const setTagData = function(tagData) {
+  tagsForNewPage = tagData;
+};
+
 /**
  * define components
  *  key: id of element
@@ -297,7 +304,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} />;
+  componentMappings['revision-path'] = <RevisionPath pageId={pageId} pagePath={pagePath} crowi={crowi} sendTagData={setTagData} />;
   componentMappings['revision-url'] = <RevisionUrl pageId={pageId} pagePath={pagePath} />;
 }
 

+ 28 - 21
src/client/js/components/Page/EditTagModal.jsx

@@ -47,28 +47,34 @@ export default class EditTagModal extends React.Component {
   }
 
   async handleSubmit() {
-    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',
-      });
+    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',
+        });
+      }
     }
-    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 });
     }
   }
 
@@ -116,4 +122,5 @@ EditTagModal.propTypes = {
   crowi: PropTypes.object.isRequired,
   pageId: PropTypes.string,
   style: PropTypes.object,
+  sendTagData: PropTypes.func,
 };

+ 2 - 1
src/client/js/components/Page/RevisionPath.js

@@ -126,7 +126,7 @@ export default class RevisionPath extends React.Component {
         <a href="#edit" className="btn btn-default btn-edit" style={editButtonStyle}>
           <i className="icon-note" />
         </a>
-        <EditTagModal crowi={this.props.crowi} pageId={this.props.pageId} style={tagButtonStyle} />
+        <EditTagModal crowi={this.props.crowi} pageId={this.props.pageId} style={tagButtonStyle} sendTagData={this.props.sendTagData} />
       </span>
     );
   }
@@ -137,4 +137,5 @@ RevisionPath.propTypes = {
   pageId: PropTypes.string,
   pagePath: PropTypes.string.isRequired,
   crowi: PropTypes.object.isRequired,
+  sendTagData: PropTypes.func,
 };

+ 2 - 0
src/server/models/page.js

@@ -976,6 +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;
 
     // sanitize path
     path = crowi.xss.process(path); // eslint-disable-line no-param-reassign
@@ -1001,6 +1002,7 @@ module.exports = function(crowi) {
 
     await validateAppliedScope(user, grant, grantUserGroupId);
     page.applyScope(user, grant, grantUserGroupId);
+    page.updateTags(tags);
 
     let savedPage = await page.save();
     const newRevision = Revision.prepareRevision(savedPage, body, null, user, { format });

+ 2 - 1
src/server/routes/page.js

@@ -547,6 +547,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;
 
     if (body === null || pagePath === null) {
       return res.json(ApiResponse.error('Parameters body and path are required.'));
@@ -559,7 +560,7 @@ module.exports = function(crowi, app) {
     }
 
     const options = {
-      grant, grantUserGroupId, overwriteScopesOfDescendants, socketClientId,
+      grant, grantUserGroupId, overwriteScopesOfDescendants, socketClientId, tags,
     };
     const createdPage = await Page.create(pagePath, body, req.user, options);