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

name, comment, err handle, async

mizozobu 6 лет назад
Родитель
Сommit
9136c441d3

+ 20 - 15
src/client/js/components/GroupDeleteModal/GroupDeleteModal.jsx

@@ -17,6 +17,7 @@ class GroupDeleteModal extends React.Component {
   constructor(props) {
     super(props);
 
+    // actionName master constants
     this.actionForPages = {
       public: 'public',
       delete: 'delete',
@@ -41,7 +42,7 @@ class GroupDeleteModal extends React.Component {
       deleteGroupId: '',
       deleteGroupName: '',
       groups: [],
-      actionForPages: '',
+      actionName: '',
       selectedGroupId: '',
     };
 
@@ -59,6 +60,8 @@ class GroupDeleteModal extends React.Component {
   componentDidMount() {
     this.retrieveUserGroupRelations();
 
+    // bootstrap and this jQuery opens/hides modal.
+    // let React handle it in the future.
     $('#admin-delete-user-group-modal').on('show.bs.modal', (button) => {
       const data = $(button.relatedTarget);
       const deleteGroupId = data.data('user-group-id');
@@ -80,8 +83,8 @@ class GroupDeleteModal extends React.Component {
   }
 
   changeActionHandler(e) {
-    const actionForPages = e.target.value;
-    this.setState({ actionForPages });
+    const actionName = e.target.value;
+    this.setState({ actionName });
   }
 
   changeGroupHandler(e) {
@@ -101,12 +104,12 @@ class GroupDeleteModal extends React.Component {
     return (
       <FormGroup className="grant-selector m-b-0">
         <FormControl
-          name="actionForPages"
+          name="actionName"
           componentClass="select"
           placeholder="select"
           bsClass={bsClassName}
           className="btn-group-sm selectpicker"
-          value={this.state.actionForPages}
+          value={this.state.actionName}
           onChange={this.changeActionHandler}
         >
           <option value="" disabled>{t('user_group_management.choose_action')}</option>
@@ -119,23 +122,25 @@ class GroupDeleteModal extends React.Component {
   renderGroupSelector() {
     const { t } = this.props;
 
-    const options = this.state.groups.map((group) => {
-      if (group._id !== this.state.deleteGroupId) {
-        const dataContent = `<i class="icon icon-fw icon-organization"></i> ${this.getGroupName(group)}`;
-        return <option key={group._id} value={group._id} data-content={dataContent}>{this.getGroupName(group)}</option>;
-      }
+    const groups = this.state.groups.filter((group) => {
+      return group._id !== this.state.deleteGroupId;
+    });
 
-      return;
+    const options = groups.map((group) => {
+      const dataContent = `<i class="icon icon-fw icon-organization"></i> ${this.getGroupName(group)}`;
+      return <option key={group._id} value={group._id} data-content={dataContent}>{this.getGroupName(group)}</option>;
     });
 
+    const defaultOptionText = groups.length === 0 ? t('user_group_management.no_groups') : t('user_group_management.select_group');
+
     return (
       <select
         name="selectedGroupId"
-        className={this.state.actionForPages === this.actionForPages.transfer ? '' : 'd-none'}
+        className={this.state.actionName === this.actionForPages.transfer ? '' : 'd-none'}
         value={this.state.selectedGroupId}
         onChange={this.changeGroupHandler}
       >
-        <option value="" disabled>{this.state.groups.length === 0 ? t('user_group_management.no_groups') : t('user_group_management.select_group')}</option>
+        <option value="" disabled>{defaultOptionText}</option>
         {options}
       </select>
     );
@@ -144,10 +149,10 @@ class GroupDeleteModal extends React.Component {
   disableSubmit() {
     let isDisabled = false;
 
-    if (this.state.actionForPages === '') {
+    if (this.state.actionName === '') {
       isDisabled = true;
     }
-    else if (this.state.actionForPages === this.actionForPages.transfer) {
+    else if (this.state.actionName === this.actionForPages.transfer) {
       isDisabled = this.state.selectedGroupId === '';
     }
 

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

@@ -1349,6 +1349,9 @@ module.exports = function(crowi) {
       page.grantedGroup = selectedGroupId;
       await page.save();
     }
+    else {
+      throw new Error('Cannot find the group to which private pages belong to. _id: ', selectedGroupId);
+    }
   };
 
   /**

+ 14 - 23
src/server/models/user-group.js

@@ -90,33 +90,24 @@ class UserGroup {
   }
 
   // グループの完全削除
-  static removeCompletelyById(deleteGroupId, action, selectedGroupId) {
+  static async removeCompletelyById(deleteGroupId, action, selectedGroupId) {
     const PageGroupRelation = mongoose.model('PageGroupRelation');
     const UserGroupRelation = mongoose.model('UserGroupRelation');
     const Page = mongoose.model('Page');
 
-    let deletedGroup;
-    return this.findById(deleteGroupId)
-      .then((userGroupData) => {
-        if (userGroupData == null) {
-          throw new Error('UserGroup data is not exists. id:', deleteGroupId);
-        }
-        return userGroupData.remove();
-      })
-      .then((removedUserGroupData) => {
-        deletedGroup = removedUserGroupData;
-      })
-      // remove relations
-      .then(() => {
-        return Promise.all([
-          UserGroupRelation.removeAllByUserGroup(deletedGroup),
-          PageGroupRelation.removeAllByUserGroup(deletedGroup),
-          Page.handlePrivatePagesForDeletedGroup(deletedGroup, action, selectedGroupId),
-        ]);
-      })
-      .then(() => {
-        return deletedGroup;
-      });
+    const groupToDelete = await this.findById(deleteGroupId);
+    if (groupToDelete == null) {
+      throw new Error('UserGroup data is not exists. id:', deleteGroupId);
+    }
+    const deletedGroup = await groupToDelete.remove();
+
+    await Promise.all([
+      UserGroupRelation.removeAllByUserGroup(deletedGroup),
+      PageGroupRelation.removeAllByUserGroup(deletedGroup),
+      Page.handlePrivatePagesForDeletedGroup(deletedGroup, action, selectedGroupId),
+    ]);
+
+    return deletedGroup;
   }
 
   // グループ生成(名前が要る)

+ 2 - 2
src/server/routes/admin.js

@@ -781,10 +781,10 @@ module.exports = function(crowi, app) {
 
   // app.post('/_api/admin/user-group/delete' , admin.userGroup.removeCompletely);
   actions.userGroup.removeCompletely = async(req, res) => {
-    const { deleteGroupId, actionForPages, selectedGroupId } = req.body;
+    const { deleteGroupId, actionName, selectedGroupId } = req.body;
 
     try {
-      await UserGroup.removeCompletelyById(deleteGroupId, actionForPages, selectedGroupId);
+      await UserGroup.removeCompletelyById(deleteGroupId, actionName, selectedGroupId);
       req.flash('successMessage', '削除しました');
     }
     catch (err) {