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

Merge pull request #1439 from weseek/fetch-user-not-subscribed

Fetch user not subscribed
Yuki Takei 6 лет назад
Родитель
Сommit
eddc5b956f

+ 13 - 10
src/client/js/components/Admin/UserGroupDetail/UserGroupUserFormByInput.jsx

@@ -16,8 +16,7 @@ class UserGroupUserFormByInput extends React.Component {
 
     this.state = {
       input: '',
-      // TDOO GW-665 fetch users
-      applicableUsers: ['hoge', 'huga'],
+      applicableUsers: [],
       isLoading: false,
       searchError: null,
     };
@@ -32,7 +31,7 @@ class UserGroupUserFormByInput extends React.Component {
     this.onKeyDown = this.onKeyDown.bind(this);
     this.renderMenuItemChildren = this.renderMenuItemChildren.bind(this);
 
-    this.searchUserDebounce = debounce(1000, this.searchUser);
+    this.searhApplicableUsersDebounce = debounce(1000, this.searhApplicableUsers);
   }
 
   /**
@@ -41,7 +40,6 @@ class UserGroupUserFormByInput extends React.Component {
    */
   onInputChange(input) {
     this.setState({ input });
-    // this.props.onInputChange(text);
     if (input === '') {
       this.setState({ applicableUsers: [] });
     }
@@ -66,9 +64,14 @@ class UserGroupUserFormByInput extends React.Component {
     return this.state.input !== '';
   }
 
-  searchUser() {
-    // TODO GW-665 fetch users
-    this.setState({ isLoading: false });
+  async searhApplicableUsers() {
+    try {
+      const users = await this.props.userGroupDetailContainer.fetchApplicableUsers(this.state.input);
+      this.setState({ applicableUsers: users, isLoading: false });
+    }
+    catch (err) {
+      toastError(err);
+    }
   }
 
   /**
@@ -86,7 +89,7 @@ class UserGroupUserFormByInput extends React.Component {
     }
 
     this.setState({ isLoading: true });
-    this.searchUserDebounce();
+    this.searhApplicableUsersDebounce();
   }
 
   onKeyDown(event) {
@@ -125,7 +128,7 @@ class UserGroupUserFormByInput extends React.Component {
             isLoading={this.state.isLoading}
             labelKey="name"
             minLength={0}
-            options={this.state.applicableUsers} // Search result (Some page names)
+            options={this.state.applicableUsers} // Search result
             emptyLabel={this.getEmptyLabel()}
             searchText={(this.state.isLoading ? 'Searching...' : this.getEmptyLabel())}
             align="left"
@@ -137,7 +140,7 @@ class UserGroupUserFormByInput extends React.Component {
             caseSensitive={false}
           />
         </div>
-        <button type="submit" className="btn btn-sm btn-success" disabled={!this.validateForm()}>{ t('add') }</button>
+        <button type="submit" className="btn btn-sm btn-success" disabled={!this.validateForm()}>{t('add')}</button>
       </form>
     );
   }

+ 23 - 0
src/client/js/services/UserGroupDetailContainer.js

@@ -99,6 +99,29 @@ export default class UserGroupDetailContainer extends Container {
     await this.setState({ isUserGroupUserModalOpen: false });
   }
 
+  /**
+   * search user for invitation
+   * @param {string} username username of the user to be searched
+   */
+  async fetchApplicableUsers(searchWord) {
+    const res = await this.appContainer.apiv3.get(`/user-groups/${this.state.userGroup._id}/unrelated-users`, {
+      searchWord,
+      // TODO GW-716 switch value
+      isForwardMatch: false,
+      isAlsoNameSearched: false,
+      isAlsoMailSearched: false,
+    });
+
+    const { users } = res.data;
+
+    const applicableUsers = users.map((user) => {
+      return user.username;
+    });
+
+    return applicableUsers;
+  }
+
+
   /**
    * update user group
    *

+ 7 - 2
src/server/models/user-group-relation.js

@@ -194,7 +194,7 @@ class UserGroupRelation {
    * @returns {Promise<User>}
    * @memberof UserGroupRelation
    */
-  static findUserByNotRelatedGroup(userGroup) {
+  static findUserByNotRelatedGroup(userGroup, queryOptions) {
     const User = UserGroupRelation.crowi.model('User');
 
     return this.findAllRelationForUserGroup(userGroup)
@@ -202,7 +202,12 @@ class UserGroupRelation {
         const relatedUserIds = relations.map((relation) => {
           return relation.relatedUser.id;
         });
-        const query = { _id: { $nin: relatedUserIds }, status: User.STATUS_ACTIVE };
+        // TODO GW-717 set query option
+        const query = {
+          _id: { $nin: relatedUserIds },
+          status: User.STATUS_ACTIVE,
+          username: new RegExp(`${queryOptions.searchWord}`),
+        };
 
         debug('findUserByNotRelatedGroup ', query);
         return User.find(query).exec();

+ 9 - 1
src/server/routes/apiv3/user-group.js

@@ -318,12 +318,20 @@ module.exports = (crowi) => {
    *                        type: object
    *                      description: user objects
    */
+  // TODO rewrite swagger
   router.get('/:id/unrelated-users', loginRequiredStrictly, adminRequired, async(req, res) => {
     const { id } = req.params;
+    const {
+      searchWord, isForwardMatch, isAlsoNameSearched, isAlsoMailSearched,
+    } = req.query;
+
+    const queryOptions = {
+      searchWord, isForwardMatch, isAlsoNameSearched, isAlsoMailSearched,
+    };
 
     try {
       const userGroup = await UserGroup.findById(id);
-      const users = await UserGroupRelation.findUserByNotRelatedGroup(userGroup);
+      const users = await UserGroupRelation.findUserByNotRelatedGroup(userGroup, queryOptions);
 
       return res.apiv3({ users });
     }