Skip to content

Commit

Permalink
Remove user_actions usage from UI components (mattermost#2679)
Browse files Browse the repository at this point in the history
* Remove user_actions from NeedTeam component

* Migrate saveTheme usage into mattermost-redux

* Refactor SystemUsers component

- Migrate to Mattermost Redux
- Simplify debouncing using lodash/debounce
- Replace bound functions to arrow functions
- Replace promise to async/await

* Migrate system_users_dropdown component to use redux without user_actions

* Migrate user_settings_advanced component to use redux without user_actions

* Refactor the <PasswordResetForm/> component

* Refactor the <ShouldVerifyEmail/> component

* Refactor the <PasswordResetSendLink/> component

* Fix unresolved import statement

* Remove unused legacy functions from user_actions

* Fix test

* Refactor OAuth related action and component

* Remove unnecessary await

* Replace var to const

* Replace lodash/debounce to mm-redux's

* Move defaultProps into class property

* Safly access to current value of input ref

* Remove fixme comment from localized_input

* Mock expected values from related tests

* Extract async load-data function from lifecycle method

* Update Snapshot
  • Loading branch information
cometkim authored and saturninoabril committed Jun 2, 2019
1 parent 365d6ba commit a653a63
Show file tree
Hide file tree
Showing 32 changed files with 640 additions and 533 deletions.
134 changes: 2 additions & 132 deletions actions/user_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// See LICENSE.txt for license information.

import {getChannelAndMyMember, getChannelMembersByIds} from 'mattermost-redux/actions/channels';
import {deletePreferences as deletePreferencesRedux, savePreferences as savePreferencesRedux} from 'mattermost-redux/actions/preferences';
import {savePreferences as savePreferencesRedux} from 'mattermost-redux/actions/preferences';
import {getTeamMembersByIds} from 'mattermost-redux/actions/teams';
import * as UserActions from 'mattermost-redux/actions/users';
import {Client4} from 'mattermost-redux/client';
import {bindClientFunc} from 'mattermost-redux/actions/helpers';
import {Preferences as PreferencesRedux} from 'mattermost-redux/constants';
import {
getChannel,
Expand All @@ -15,11 +13,10 @@ import {
getMyChannelMember,
getChannelMembersInChannels,
} from 'mattermost-redux/selectors/entities/channels';
import {getBool, makeGetCategory} from 'mattermost-redux/selectors/entities/preferences';
import {getBool} from 'mattermost-redux/selectors/entities/preferences';
import {getCurrentTeamId, getTeamMember} from 'mattermost-redux/selectors/entities/teams';
import * as Selectors from 'mattermost-redux/selectors/entities/users';

import {browserHistory} from 'utils/browser_history';
import {loadStatusesForProfilesList, loadStatusesForProfilesMap} from 'actions/status_actions.jsx';
import store from 'stores/redux_store.jsx';
import * as Utils from 'utils/utils.jsx';
Expand Down Expand Up @@ -272,64 +269,6 @@ export async function loadProfilesForDM() {
}
}

export function saveTheme(teamId, theme) {
return async (doDispatch, doGetState) => {
const currentUserId = Selectors.getCurrentUserId(doGetState());
const preference = [{
user_id: currentUserId,
category: Preferences.CATEGORY_THEME,
name: teamId,
value: JSON.stringify(theme),
}];

await doDispatch(savePreferencesRedux(currentUserId, preference));
return doDispatch(onThemeSaved(teamId));
};
}

function onThemeSaved(teamId) {
return async (doDispatch, doGetState) => {
const getCategory = makeGetCategory();
const state = doGetState();
const themePreferences = getCategory(state, Preferences.CATEGORY_THEME);

if (teamId !== '' && themePreferences.length > 1) {
// no extra handling to be done to delete team-specific themes
return;
}

const currentUserId = Selectors.getCurrentUserId(state);
const toDelete = [];

for (const themePreference of themePreferences) {
const name = themePreference.name;
if (name === '' || name === teamId) {
continue;
}

toDelete.push({
user_id: currentUserId,
category: Preferences.CATEGORY_THEME,
name,
});
}

if (toDelete.length > 0) {
// we're saving a new global theme so delete any team-specific ones
doDispatch(deletePreferencesRedux(currentUserId, toDelete));
}
};
}

export async function searchUsers(term, teamId = getCurrentTeamId(getState()), options = {}, success) {
const {data} = await UserActions.searchProfiles(term, {team_id: teamId, ...options})(dispatch, getState);
dispatch(loadStatusesForProfilesList(data));

if (success) {
success(data);
}
}

export async function autocompleteUsersInTeam(username, success) {
const {data} = await UserActions.autocompleteUsers(username, getCurrentTeamId(getState()))(dispatch, getState);
if (success) {
Expand All @@ -344,66 +283,6 @@ export async function autocompleteUsers(username, success) {
}
}

export function revokeAllSessions(userId) {
return async (doDispatch, doGetState) => {
const {data, error} = await doDispatch(UserActions.revokeAllSessionsForUser(userId));
if (error) {
const serverError = doGetState().requests.users.updateUser.error;
return {error: {id: serverError.server_error_id, ...serverError}};
}
return {data};
};
}

export async function verifyEmail(token, success, error) {
const {data, error: err} = await UserActions.verifyUserEmail(token)(dispatch, getState);
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
}

export async function resetPassword(token, password, success, error) {
const {data, error: err} = await UserActions.resetUserPassword(token, password)(dispatch, getState);
if (data) {
browserHistory.push('/login?extra=' + Constants.PASSWORD_CHANGE);
if (success) {
success(data);
}
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
}

export async function resendVerification(email, success, error) {
const {data, error: err} = await UserActions.sendVerificationEmail(email)(dispatch, getState);
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
}

export function getAuthorizedApps() {
return (doDispatch, doGetState) => {
const currentUserId = Selectors.getCurrentUserId(doGetState());
const getAuthAppsAction = bindClientFunc({
clientFunc: Client4.getAuthorizedOAuthApps,
params: [currentUserId],
});

return doDispatch(getAuthAppsAction);
};
}

export function deauthorizeOAuthApp(appId) {
return bindClientFunc({
clientFunc: Client4.deauthorizeOAuthApp,
params: [appId],
});
}

export function autoResetStatus() {
return async (doDispatch, doGetState) => {
const {currentUserId} = getState().entities.users;
Expand All @@ -423,12 +302,3 @@ export function autoResetStatus() {
return userStatus;
};
}

export async function sendPasswordResetEmail(email, success, error) {
const {data, error: err} = await UserActions.sendPasswordResetEmail(email)(dispatch, getState);
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
}
52 changes: 0 additions & 52 deletions actions/user_actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,56 +207,4 @@ describe('Actions.User', () => {
await testStore.dispatch(UserActions.loadChannelMembersForProfilesList([], 'current_channel_id'));
expect(testStore.getActions()).toEqual([]);
});

test('saveTheme for a specific team', async () => {
const {currentUserId} = initialState.entities.users;
const {currentTeamId} = initialState.entities.teams;
const expectedActions = [{
args: [
currentUserId,
[{
category: 'theme',
name: currentTeamId,
user_id: currentUserId,
value: JSON.stringify(Preferences.THEMES.mattermostDark),
}],
],
type: 'MOCK_SAVE_PREFERENCES',
}];
const testStore = await mockStore(initialState);

await testStore.dispatch(UserActions.saveTheme(currentTeamId, Preferences.THEMES.mattermostDark));
expect(testStore.getActions()).toEqual(expectedActions);
});

test('saveTheme for a all teams', async () => {
const {currentUserId} = initialState.entities.users;
const {currentTeamId} = initialState.entities.teams;
const expectedActions = [{
args: [
currentUserId,
[{
category: 'theme',
name: '',
user_id: currentUserId,
value: JSON.stringify(Preferences.THEMES.mattermostDark),
}],
],
type: 'MOCK_SAVE_PREFERENCES',
}, {
args: [
currentUserId,
[{
category: 'theme',
name: currentTeamId,
user_id: currentUserId,
}],
],
type: 'MOCK_DELETE_PREFERENCES',
}];
const testStore = await mockStore(initialState);

await testStore.dispatch(UserActions.saveTheme('', Preferences.THEMES.mattermostDark));
expect(testStore.getActions()).toEqual(expectedActions);
});
});
8 changes: 7 additions & 1 deletion components/admin_console/system_users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';

import {getTeams, getTeamStats} from 'mattermost-redux/actions/teams';
import {getUser, getUserAccessToken, getProfiles} from 'mattermost-redux/actions/users';
import {
getUser,
getUserAccessToken,
getProfiles,
searchProfiles,
} from 'mattermost-redux/actions/users';
import {getTeamsList} from 'mattermost-redux/selectors/entities/teams';
import {getUsers} from 'mattermost-redux/selectors/entities/users';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
Expand Down Expand Up @@ -72,6 +77,7 @@ function mapDispatchToProps(dispatch) {
setSystemUsersSearch,
loadProfilesWithoutTeam,
getProfiles,
searchProfiles,
}, dispatch),
};
}
Expand Down
Loading

0 comments on commit a653a63

Please sign in to comment.