Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Remove last flux store usage from actions (#1951)
Browse files Browse the repository at this point in the history
* Remove flus usages from diagnostics

* Remove flux usages from status actions

* Convert status actions to redux actions and add tests

* Updates per feedback

* Update per feedback
  • Loading branch information
jwilander committed Oct 29, 2018
1 parent c5d13af commit a714e5d
Show file tree
Hide file tree
Showing 21 changed files with 257 additions and 105 deletions.
4 changes: 2 additions & 2 deletions actions/diagnostics_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// See LICENSE.txt for license information.

import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';

import store from 'stores/redux_store.jsx';
import UserStore from 'stores/user_store.jsx';

const SUPPORTS_CLEAR_MARKS = isSupported([performance.clearMarks]);
const SUPPORTS_MARK = isSupported([performance.mark]);
Expand All @@ -17,7 +17,7 @@ const SUPPORTS_MEASURE_METHODS = isSupported([

export function trackEvent(category, event, props) {
if (global.window && global.window.analytics) {
const properties = Object.assign({category, type: event, user_actual_id: UserStore.getCurrentId()}, props);
const properties = Object.assign({category, type: event, user_actual_id: getCurrentUserId(store.getState())}, props);
const options = {
context: {
ip: '0.0.0.0',
Expand Down
136 changes: 55 additions & 81 deletions actions/status_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,83 @@
// See LICENSE.txt for license information.

import {getStatusesByIds} from 'mattermost-redux/actions/users';
import {getCurrentChannelId} from 'mattermost-redux/selectors/entities/channels';
import {getPostsInCurrentChannel} from 'mattermost-redux/selectors/entities/posts';
import {getDirectShowPreferences} from 'mattermost-redux/selectors/entities/preferences';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';

import ChannelStore from 'stores/channel_store.jsx';
import PostStore from 'stores/post_store.jsx';
import PreferenceStore from 'stores/preference_store.jsx';
import store from 'stores/redux_store.jsx';
import UserStore from 'stores/user_store.jsx';
import {Constants, Preferences} from 'utils/constants.jsx';

const dispatch = store.dispatch;
const getState = store.getState;

export function loadStatusesForChannel(channelId = ChannelStore.getCurrentId()) {
const postList = PostStore.getVisiblePosts(channelId);
if (!postList || !postList.posts) {
return;
}

const statuses = UserStore.getStatuses();
const statusesToLoad = {};
for (const pid in postList.posts) {
if (!postList.posts.hasOwnProperty(pid)) {
continue;
}

const post = postList.posts[pid];
if (statuses[post.user_id] == null) {
statusesToLoad[post.user_id] = true;
}
}

loadStatusesByIds(Object.keys(statusesToLoad));
}

export function loadStatusesForDMSidebar() {
const dmPrefs = PreferenceStore.getCategory(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW);
const statusesToLoad = [];

for (const [key, value] of dmPrefs) {
if (value === 'true') {
statusesToLoad.push(key);
}
}

loadStatusesByIds(statusesToLoad);
}
import {Constants} from 'utils/constants.jsx';

export function loadStatusesForChannelAndSidebar() {
const statusesToLoad = {};

const channelId = ChannelStore.getCurrentId();
const posts = PostStore.getVisiblePosts(channelId) || [];
for (const post of posts) {
statusesToLoad[post.user_id] = true;
}
return (dispatch, getState) => {
const state = getState();
const statusesToLoad = {};

const channelId = getCurrentChannelId(state);
const postsInChannel = getPostsInCurrentChannel(state);
const posts = postsInChannel.slice(0, state.views.channel.postVisibility[channelId] || 0);
for (const post of posts) {
if (post.user_id) {
statusesToLoad[post.user_id] = true;
}
}

const dmPrefs = PreferenceStore.getCategory(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW);
const dmPrefs = getDirectShowPreferences(state);

for (const [key, value] of dmPrefs) {
if (value === 'true') {
statusesToLoad[key] = true;
for (const pref of dmPrefs) {
if (pref.value === 'true') {
statusesToLoad[pref.name] = true;
}
}
}

const {currentUserId} = getState().entities.users;
statusesToLoad[currentUserId] = true;
const currentUserId = getCurrentUserId(state);
statusesToLoad[currentUserId] = true;

loadStatusesByIds(Object.keys(statusesToLoad));
dispatch(loadStatusesByIds(Object.keys(statusesToLoad)));
};
}

export function loadStatusesForProfilesList(users) {
if (users == null) {
return;
}
return (dispatch) => {
if (users == null) {
return;
}

const statusesToLoad = [];
for (let i = 0; i < users.length; i++) {
statusesToLoad.push(users[i].id);
}
const statusesToLoad = [];
for (let i = 0; i < users.length; i++) {
statusesToLoad.push(users[i].id);
}

loadStatusesByIds(statusesToLoad);
dispatch(loadStatusesByIds(statusesToLoad));
};
}

export function loadStatusesForProfilesMap(users) {
if (users == null) {
return;
}

const statusesToLoad = [];
for (const userId in users) {
if ({}.hasOwnProperty.call(users, userId)) {
statusesToLoad.push(userId);
return (dispatch) => {
if (users == null) {
return;
}

const statusesToLoad = [];
for (const userId in users) {
if ({}.hasOwnProperty.call(users, userId)) {
statusesToLoad.push(userId);
}
}
}

loadStatusesByIds(statusesToLoad);
dispatch(loadStatusesByIds(statusesToLoad));
};
}

export function loadStatusesByIds(userIds) {
if (userIds.length === 0) {
return;
}
return (dispatch) => {
if (userIds.length === 0) {
return;
}

getStatusesByIds(userIds)(dispatch, getState);
dispatch(getStatusesByIds(userIds));
};
}

let intervalId = '';
Expand All @@ -114,7 +88,7 @@ export function startPeriodicStatusUpdates() {

intervalId = setInterval(
() => {
loadStatusesForChannelAndSidebar();
store.dispatch(loadStatusesForChannelAndSidebar());
},
Constants.STATUS_INTERVAL
);
Expand Down
8 changes: 4 additions & 4 deletions actions/user_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function switchFromLdapToEmail(email, password, token, ldapPassword
export async function loadProfilesAndTeamMembers(page, perPage, teamId = getCurrentTeamId(getState()), success) {
const {data} = await UserActions.getProfilesInTeam(teamId, page, perPage)(dispatch, getState);
loadTeamMembersForProfilesList(data, teamId, success);
loadStatusesForProfilesList(data);
dispatch(loadStatusesForProfilesList(data));
}

export async function loadProfilesAndTeamMembersAndChannelMembers(page, perPage, teamId = getCurrentTeamId(getState()), channelId = getCurrentChannelId(getState()), success, error) {
Expand All @@ -55,7 +55,7 @@ export async function loadProfilesAndTeamMembersAndChannelMembers(page, perPage,
teamId,
() => {
loadChannelMembersForProfilesList(data, channelId, success, error);
loadStatusesForProfilesList(data);
dispatch(loadStatusesForProfilesList(data));
}
);
}
Expand Down Expand Up @@ -84,7 +84,7 @@ export function loadTeamMembersForProfilesList(profiles, teamId = getCurrentTeam

export async function loadProfilesWithoutTeam(page, perPage, success) {
const {data} = await UserActions.getProfilesWithoutTeam(page, perPage)(dispatch, getState);
loadStatusesForProfilesMap(data);
dispatch(loadStatusesForProfilesMap(data));

if (success) {
success(data);
Expand Down Expand Up @@ -342,7 +342,7 @@ function onThemeSaved(teamId, onSuccess) {

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

if (success) {
success(data);
Expand Down
2 changes: 1 addition & 1 deletion actions/websocket_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function reconnect(includeWebSocket = true) {
if (currentTeamId) {
loadChannelsForCurrentUser();
getPosts(ChannelStore.getCurrentId())(dispatch, getState);
StatusActions.loadStatusesForChannelAndSidebar();
dispatch(StatusActions.loadStatusesForChannelAndSidebar());
TeamActions.getMyTeamUnreads()(dispatch, getState);
}

Expand Down
4 changes: 2 additions & 2 deletions components/add_users_to_team/add_users_to_team.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {Modal} from 'react-bootstrap';
import {FormattedMessage} from 'react-intl';
import {Client4} from 'mattermost-redux/client';

import {loadStatusesForProfilesList} from 'actions/status_actions.jsx';
import Constants from 'utils/constants.jsx';
import {displayEntireNameForUser, localizeMessage} from 'utils/utils.jsx';

Expand All @@ -29,6 +28,7 @@ export default class AddUsersToTeam extends React.Component {
setModalSearchTerm: PropTypes.func.isRequired,
searchProfiles: PropTypes.func.isRequired,
addUsersToTeam: PropTypes.func.isRequired,
loadStatusesForProfilesList: PropTypes.func.isRequired,
}).isRequired,
}

Expand Down Expand Up @@ -67,7 +67,7 @@ export default class AddUsersToTeam extends React.Component {
this.setUsersLoadingState(true);
const {data} = await this.props.actions.searchProfiles(searchTerm, {not_in_team_id: this.props.currentTeamId});
if (data) {
loadStatusesForProfilesList(data);
this.props.actions.loadStatusesForProfilesList(data);
}
this.setUsersLoadingState(false);
},
Expand Down
2 changes: 2 additions & 0 deletions components/add_users_to_team/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {getProfilesNotInTeam, searchProfiles} from 'mattermost-redux/actions/use
import {getCurrentTeam} from 'mattermost-redux/selectors/entities/teams';
import {searchProfilesNotInCurrentTeam, getProfilesNotInCurrentTeam} from 'mattermost-redux/selectors/entities/users';

import {loadStatusesForProfilesList} from 'actions/status_actions.jsx';
import {addUsersToTeam} from 'actions/team_actions.jsx';
import {setModalSearchTerm} from 'actions/views/search';

Expand Down Expand Up @@ -39,6 +40,7 @@ function mapDispatchToProps(dispatch) {
setModalSearchTerm,
searchProfiles,
addUsersToTeam,
loadStatusesForProfilesList,
}, dispatch),
};
}
Expand Down
2 changes: 2 additions & 0 deletions components/member_list_channel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {getChannelStats} from 'mattermost-redux/actions/channels';
import {searchProfiles} from 'mattermost-redux/actions/users';
import {sortByUsername} from 'mattermost-redux/utils/user_utils';

import {loadStatusesForProfilesList} from 'actions/status_actions.jsx';
import {setModalSearchTerm} from 'actions/views/search';

import MemberListChannel from './member_list_channel.jsx';
Expand Down Expand Up @@ -72,6 +73,7 @@ function mapDispatchToProps(dispatch) {
searchProfiles,
getChannelStats,
setModalSearchTerm,
loadStatusesForProfilesList,
}, dispatch),
};
}
Expand Down
4 changes: 2 additions & 2 deletions components/member_list_channel/member_list_channel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import PropTypes from 'prop-types';
import React from 'react';

import {loadProfilesAndTeamMembersAndChannelMembers, loadTeamMembersAndChannelMembersForProfilesList} from 'actions/user_actions.jsx';
import {loadStatusesForProfilesList} from 'actions/status_actions.jsx';

import Constants from 'utils/constants.jsx';
import * as UserAgent from 'utils/user_agent.jsx';
Expand All @@ -28,6 +27,7 @@ export default class MemberListChannel extends React.PureComponent {
searchProfiles: PropTypes.func.isRequired,
getChannelStats: PropTypes.func.isRequired,
setModalSearchTerm: PropTypes.func.isRequired,
loadStatusesForProfilesList: PropTypes.func.isRequired,
}).isRequired,
}

Expand Down Expand Up @@ -71,7 +71,7 @@ export default class MemberListChannel extends React.PureComponent {

this.setState({loading: true});

loadStatusesForProfilesList(data);
this.props.actions.loadStatusesForProfilesList(data);
loadTeamMembersAndChannelMembersForProfilesList(data, nextProps.currentTeamId, nextProps.currentChannelId, this.loadComplete);
},
Constants.SEARCH_TIMEOUT_MILLISECONDS
Expand Down
2 changes: 2 additions & 0 deletions components/member_list_team/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {getProfilesInCurrentTeam, searchProfilesInCurrentTeam} from 'mattermost-
import {Permissions} from 'mattermost-redux/constants';
import {searchProfiles} from 'mattermost-redux/actions/users';

import {loadStatusesForProfilesList} from 'actions/status_actions.jsx';
import {setModalSearchTerm} from 'actions/views/search';

import MemberListTeam from './member_list_team.jsx';
Expand All @@ -35,6 +36,7 @@ function mapStateToProps(state, ownProps) {
currentTeamId: state.entities.teams.currentTeamId,
totalTeamMembers: stats.active_member_count,
canManageTeamMembers,
loadStatusesForProfilesList,
};
}

Expand Down
4 changes: 2 additions & 2 deletions components/member_list_team/member_list_team.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import PropTypes from 'prop-types';
import React from 'react';

import {loadProfilesAndTeamMembers, loadTeamMembersForProfilesList} from 'actions/user_actions.jsx';
import {loadStatusesForProfilesList} from 'actions/status_actions.jsx';
import Constants from 'utils/constants.jsx';
import * as UserAgent from 'utils/user_agent.jsx';

Expand All @@ -26,6 +25,7 @@ export default class MemberListTeam extends React.Component {
searchProfiles: PropTypes.func.isRequired,
getTeamStats: PropTypes.func.isRequired,
setModalSearchTerm: PropTypes.func.isRequired,
loadStatusesForProfilesList: PropTypes.func.isRequired,
}).isRequired,
}

Expand Down Expand Up @@ -69,7 +69,7 @@ export default class MemberListTeam extends React.Component {

this.setState({loading: true});

loadStatusesForProfilesList(data);
this.props.actions.loadStatusesForProfilesList(data);
loadTeamMembersForProfilesList(data, nextProps.currentTeamId, this.loadComplete);
},
Constants.SEARCH_TIMEOUT_MILLISECONDS
Expand Down
2 changes: 2 additions & 0 deletions components/more_direct_channels/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {getCurrentTeam} from 'mattermost-redux/selectors/entities/teams';

import {loadStatusesForProfilesList} from 'actions/status_actions.jsx';
import {setModalSearchTerm} from 'actions/views/search';

import MoreDirectChannels from './more_direct_channels.jsx';
Expand Down Expand Up @@ -75,6 +76,7 @@ function mapDispatchToProps(dispatch) {
searchProfiles,
setModalSearchTerm,
getTotalUsersStats,
loadStatusesForProfilesList,
}, dispatch),
};
}
Expand Down
Loading

0 comments on commit a714e5d

Please sign in to comment.