Skip to content

Commit

Permalink
Fix pinned post list from refreshing (mattermost#2042)
Browse files Browse the repository at this point in the history
* Fix pinned post list from refreshing

* Fix show post for the current channel when the channel is switched
  • Loading branch information
enahum committed Nov 20, 2018
1 parent 59ed7ce commit a3b98ac
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 74 deletions.
46 changes: 5 additions & 41 deletions components/search_results/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@

import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';

import {getMorePostsForSearch} from 'mattermost-redux/actions/search';
import {getChannel} from 'mattermost-redux/selectors/entities/channels';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {getSearchMatches, getSearchResults} from 'mattermost-redux/selectors/entities/posts';
import * as PreferenceSelectors from 'mattermost-redux/selectors/entities/preferences';
import {getConfig} from 'mattermost-redux/selectors/entities/general';

import {getMorePostsForSearch} from 'mattermost-redux/actions/search';

import {
getCurrentUser,
getUsers,
getUserStatuses,
} from 'mattermost-redux/selectors/entities/users';
import {getCurrentUser} from 'mattermost-redux/selectors/entities/users';

import {
getSearchResultsTerms,
Expand All @@ -27,14 +22,9 @@ import {Preferences} from 'utils/constants.jsx';

import SearchResults from './search_results.jsx';

const getCategory = PreferenceSelectors.makeGetCategory();

function makeMapStateToProps() {
let results;
let posts;
let channels;
let flaggedPosts;
let isFlaggedByPostId;

return function mapStateToProps(state) {
const config = getConfig(state);
Expand All @@ -49,22 +39,13 @@ function makeMapStateToProps() {
if (newResults && newResults !== results) {
results = newResults;

channels = new Map();

posts = [];
results.forEach((post) => {
if (!post) {
return;
}

let channel;
if (channels.has(post.channel_id)) {
channel = channels.get(post.channel_id);
} else {
channel = getChannel(state, post.channel_id);
channels.set(post.channel_id, channel);
}

const channel = getChannel(state, post.channel_id);
if (channel && channel.delete_at !== 0 && !viewArchivedChannels) {
return;
}
Expand All @@ -73,28 +54,11 @@ function makeMapStateToProps() {
});
}

const newFlaggedPosts = getCategory(state, Preferences.CATEGORY_FLAGGED_POST);

// Cache flagged posts map
if (newFlaggedPosts !== flaggedPosts) {
flaggedPosts = newFlaggedPosts;

isFlaggedByPostId = new Map();

for (const pref of flaggedPosts) {
isFlaggedByPostId.set(pref.name, true);
}
}

return {
results: posts,
matches: getSearchMatches(state),
profiles: getUsers(state),
statuses: getUserStatuses(state),
currentUser: getCurrentUser(state),
channels,
searchTerms: getSearchResultsTerms(state),
isFlaggedByPostId,
isSearchingTerm: getIsSearchingTerm(state),
isSearchingFlaggedPost: getIsSearchingFlaggedPost(state),
isSearchingPinnedPost: getIsSearchingPinnedPost(state),
Expand Down
27 changes: 0 additions & 27 deletions components/search_results/search_results.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ export default class SearchResults extends React.PureComponent {
static propTypes = {
results: PropTypes.array,
matches: PropTypes.object,
profiles: PropTypes.object,
statuses: PropTypes.object,
currentUser: PropTypes.object,
channels: PropTypes.object,
searchTerms: PropTypes.string,
isFlaggedByPostId: PropTypes.object,
isSearchingTerm: PropTypes.bool,
isSearchingFlaggedPost: PropTypes.bool,
isSearchingPinnedPost: PropTypes.bool,
Expand Down Expand Up @@ -129,8 +125,6 @@ export default class SearchResults extends React.PureComponent {
const results = this.props.results;
const noResults = (!results || results.length === 0);
const searchTerms = this.props.searchTerms;
const profiles = this.props.profiles || {};
const statuses = this.props.statuses || {};

let ctls = null;

Expand Down Expand Up @@ -196,38 +190,17 @@ export default class SearchResults extends React.PureComponent {
}

ctls = sortedResults.map((post, idx, arr) => {
let profile;
if (this.props.currentUser.id === post.user_id) {
profile = this.props.currentUser;
} else {
profile = profiles[post.user_id];
}

let status = 'offline';
if (statuses) {
status = statuses[post.user_id] || 'offline';
}

let isFlagged = false;
if (this.props.isFlaggedByPostId) {
isFlagged = this.props.isFlaggedByPostId.get(post.id) || false;
}

const reverseCount = arr.length - idx - 1;

return (
<SearchResultsItem
key={post.id}
channel={this.props.channels.get(post.channel_id)}
compactDisplay={this.props.compactDisplay}
post={post}
matches={this.props.matches[post.id]}
lastPostCount={(reverseCount >= 0 && reverseCount < Constants.TEST_ID_COUNT) ? reverseCount : -1}
user={profile}
term={(!this.props.isFlaggedPosts && !this.props.isPinnedPosts && !this.props.isMentionSearch) ? searchTerms : ''}
isMentionSearch={this.props.isMentionSearch}
isFlagged={isFlagged}
status={status}
/>
);
}, this);
Expand Down
16 changes: 13 additions & 3 deletions components/search_results_item/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {getCurrentTeam} from 'mattermost-redux/selectors/entities/teams';
import {makeGetCommentCountForPost} from 'mattermost-redux/selectors/entities/posts';
import {getChannel} from 'mattermost-redux/selectors/entities/channels';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {makeGetCommentCountForPost} from 'mattermost-redux/selectors/entities/posts';
import {getMyPreferences} from 'mattermost-redux/selectors/entities/preferences';
import {getCurrentTeam} from 'mattermost-redux/selectors/entities/teams';
import {getUser, getStatusForUserId} from 'mattermost-redux/selectors/entities/users';
import {isPostFlagged} from 'mattermost-redux/utils/post_utils';

import {
closeRightHandSide,
Expand All @@ -20,12 +24,18 @@ function mapStateToProps() {

return (state, ownProps) => {
const config = getConfig(state);
const preferences = getMyPreferences(state);
const enablePostUsernameOverride = config.EnablePostUsernameOverride === 'true';
const {post} = ownProps;

return {
channel: getChannel(state, post.channel_id),
currentTeamName: getCurrentTeam(state).name,
commentCountForPost: getCommentCountForPost(state, {post: ownProps.post}),
commentCountForPost: getCommentCountForPost(state, {post}),
enablePostUsernameOverride,
isFlagged: isPostFlagged(post.id, preferences),
user: getUser(state, post.user_id),
status: getStatusForUserId(state, post.user_id) || 'offline',
};
};
}
Expand Down
3 changes: 1 addition & 2 deletions components/sidebar_right/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {getCurrentUser} from 'mattermost-redux/selectors/entities/users';
import {getChannel} from 'mattermost-redux/selectors/entities/channels';
import {getPost} from 'mattermost-redux/selectors/entities/posts';

import {getPinnedPosts, setRhsExpanded, showPinnedPosts} from 'actions/views/rhs';
import {setRhsExpanded, showPinnedPosts} from 'actions/views/rhs';
import {
getIsRhsExpanded,
getIsRhsOpen,
Expand Down Expand Up @@ -57,7 +57,6 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators({
getPinnedPosts,
setRhsExpanded,
showPinnedPosts,
}, dispatch),
Expand Down
6 changes: 5 additions & 1 deletion components/sidebar_right/sidebar_right.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default class SidebarRight extends React.PureComponent {
isPinnedPosts: PropTypes.bool,
previousRhsState: PropTypes.string,
actions: PropTypes.shape({
getPinnedPosts: PropTypes.func.isRequired,
setRhsExpanded: PropTypes.func.isRequired,
showPinnedPosts: PropTypes.func.isRequired,
}),
Expand All @@ -41,6 +40,11 @@ export default class SidebarRight extends React.PureComponent {
trackEvent('ui', 'ui_rhs_opened');
setTimeout(postListScrollChange, 0);
}

const {actions, isPinnedPosts, channel} = this.props;
if (isPinnedPosts && prevProps.isPinnedPosts === isPinnedPosts && channel.id !== prevProps.channel.id) {
actions.showPinnedPosts(channel.id);
}
}

onShrink = () => {
Expand Down

0 comments on commit a3b98ac

Please sign in to comment.