From 6c4c706313eb765eb00c639f381646be74f27b69 Mon Sep 17 00:00:00 2001
From: Joram Wilander <jwawilander@gmail.com>
Date: Tue, 25 Apr 2017 11:46:02 -0400
Subject: Start moving webapp to Redux (#6140)

* Start moving webapp to Redux

* Fix localforage import

* Updates per feedback

* Feedback udpates and a few fixes

* Minor updates

* Fix statuses, config not loading properly, getMe sanitizing too much

* Fix preferences

* Fix user autocomplete

* Fix sessions and audits

* Fix error handling for all redux actions

* Use new directory structure for components and containers

* Refresh immediately on logout instead of after timeout

* Add fetch polyfill
---
 .../admin_console/system_users/system_users.jsx    | 87 +++++++---------------
 1 file changed, 26 insertions(+), 61 deletions(-)

(limited to 'webapp/components/admin_console/system_users/system_users.jsx')

diff --git a/webapp/components/admin_console/system_users/system_users.jsx b/webapp/components/admin_console/system_users/system_users.jsx
index 0b967dead..7bc4b81ed 100644
--- a/webapp/components/admin_console/system_users/system_users.jsx
+++ b/webapp/components/admin_console/system_users/system_users.jsx
@@ -23,6 +23,9 @@ import * as Utils from 'utils/utils.jsx';
 
 import SystemUsersList from './system_users_list.jsx';
 
+import store from 'stores/redux_store.jsx';
+import {searchProfiles, searchProfilesInTeam} from 'mattermost-redux/selectors/entities/users';
+
 const ALL_USERS = '';
 const NO_TEAM = 'no_team';
 
@@ -120,25 +123,18 @@ export default class SystemUsers extends React.Component {
 
     updateUsersFromStore(teamId = this.state.teamId, term = this.state.term) {
         if (term) {
-            if (teamId === this.state.teamId) {
-                // Search results aren't in the store, so manually update the users in them
-                const users = [...this.state.users];
-
-                for (let i = 0; i < users.length; i++) {
-                    const user = users[i];
-
-                    if (UserStore.hasProfile(user.id)) {
-                        users[i] = UserStore.getProfile(user.id);
-                    }
-                }
-
-                this.setState({
-                    users
-                });
+            let users;
+            if (teamId) {
+                users = searchProfilesInTeam(store.getState(), teamId, term);
             } else {
-                this.doSearch(teamId, term, true);
+                users = searchProfiles(store.getState(), term);
             }
 
+            if (users.length === 0 && UserStore.hasProfile(term)) {
+                users = [UserStore.getProfile(term)];
+            }
+
+            this.setState({users});
             return;
         }
 
@@ -179,11 +175,11 @@ export default class SystemUsers extends React.Component {
         // Paging isn't supported while searching
 
         if (this.state.teamId === ALL_USERS) {
-            loadProfiles((page + 1) * USERS_PER_PAGE, USERS_PER_PAGE, this.loadComplete);
+            loadProfiles(page, USERS_PER_PAGE, this.loadComplete);
         } else if (this.state.teamId === NO_TEAM) {
             loadProfilesWithoutTeam(page + 1, USERS_PER_PAGE, this.loadComplete);
         } else {
-            loadProfilesAndTeamMembers((page + 1) * USERS_PER_PAGE, USERS_PER_PAGE, this.state.teamId, this.loadComplete);
+            loadProfilesAndTeamMembers(page + 1, USERS_PER_PAGE, this.state.teamId, this.loadComplete);
         }
     }
 
@@ -204,11 +200,9 @@ export default class SystemUsers extends React.Component {
 
     doSearch(teamId, term, now = false) {
         clearTimeout(this.searchTimeoutId);
+        this.term = term;
 
-        this.setState({
-            loading: true,
-            users: []
-        });
+        this.setState({loading: true});
 
         const options = {
             [UserSearchOptions.ALLOW_INACTIVE]: true
@@ -217,74 +211,45 @@ export default class SystemUsers extends React.Component {
             options[UserSearchOptions.WITHOUT_TEAM] = true;
         }
 
-        const searchTimeoutId = setTimeout(
+        this.searchTimeoutId = setTimeout(
             () => {
                 searchUsers(
                     term,
                     teamId,
                     options,
                     (users) => {
-                        if (searchTimeoutId !== this.searchTimeoutId) {
-                            return;
-                        }
-
-                        if (users.length > 0) {
-                            this.setState({
-                                loading: false,
-                                users
-                            });
-                        } else if (term.length === USER_ID_LENGTH) {
+                        if (users.length === 0 && term.length === USER_ID_LENGTH) {
                             // This term didn't match any users name, but it does look like it might be a user's ID
-                            this.getUserById(term, searchTimeoutId);
+                            this.getUserById(term);
                         } else {
-                            this.setState({
-                                loading: false
-                            });
+                            this.setState({loading: false});
                         }
                     },
                     () => {
-                        this.setState({
-                            loading: false
-                        });
+                        this.setState({loading: false});
                     }
                 );
             },
             now ? 0 : Constants.SEARCH_TIMEOUT_MILLISECONDS
         );
-
-        this.searchTimeoutId = searchTimeoutId;
     }
 
-    getUserById(id, searchTimeoutId) {
+    getUserById(id) {
         if (UserStore.hasProfile(id)) {
-            this.setState({
-                loading: false,
-                users: [UserStore.getProfile(id)]
-            });
-
+            this.setState({loading: false});
             return;
         }
 
         getUser(
             id,
-            (user) => {
-                if (searchTimeoutId !== this.searchTimeoutId) {
-                    return;
-                }
-
+            () => {
                 this.setState({
-                    loading: false,
-                    users: [user]
+                    loading: false
                 });
             },
             () => {
-                if (searchTimeoutId !== this.searchTimeoutId) {
-                    return;
-                }
-
                 this.setState({
-                    loading: false,
-                    users: []
+                    loading: false
                 });
             }
         );
-- 
cgit v1.2.3-1-g7c22

