Commit d0bb5e84 authored by Joram Wilander's avatar Joram Wilander Committed by JoramWilander

Fix pin posts not updating in RHS (#2154)

parent 5a2b1a6f
......@@ -62,24 +62,7 @@ export function flagPost(postId) {
const rhsState = getRhsState(state);
if (rhsState === RHSStates.FLAG) {
const results = state.entities.search.results;
const index = results.indexOf(postId);
if (index === -1) {
const flaggedPost = PostSelectors.getPost(state, postId);
const posts = getPostsForIds(state, results).reduce((acc, post) => {
acc[post.id] = post;
return acc;
}, {});
posts[flaggedPost.id] = flaggedPost;
const newResults = [...results, postId];
newResults.sort((a, b) => comparePosts(posts[a], posts[b]));
dispatch({
type: SearchTypes.RECEIVED_SEARCH_POSTS,
data: {posts, order: newResults},
});
}
addPostToSearchResults(postId, state, dispatch);
}
return {data: true};
......@@ -93,19 +76,7 @@ export function unflagPost(postId) {
const rhsState = getRhsState(state);
if (rhsState === RHSStates.FLAG) {
let results = state.entities.search.results;
const index = results.indexOf(postId);
if (index > -1) {
results = [...results];
results.splice(index, 1);
const posts = getPostsForIds(state, results);
dispatch({
type: SearchTypes.RECEIVED_SEARCH_POSTS,
data: {posts, order: results},
});
}
removePostFromSearchResults(postId, state, dispatch);
}
return {data: true};
......@@ -215,25 +186,76 @@ export function searchForTerm(term) {
};
}
function addPostToSearchResults(postId, state, dispatch) {
const results = state.entities.search.results;
const index = results.indexOf(postId);
if (index === -1) {
const newPost = PostSelectors.getPost(state, postId);
const posts = getPostsForIds(state, results).reduce((acc, post) => {
acc[post.id] = post;
return acc;
}, {});
posts[newPost.id] = newPost;
const newResults = [...results, postId];
newResults.sort((a, b) => comparePosts(posts[a], posts[b]));
dispatch({
type: SearchTypes.RECEIVED_SEARCH_POSTS,
data: {posts, order: newResults},
});
}
}
function removePostFromSearchResults(postId, state, dispatch) {
let results = state.entities.search.results;
const index = results.indexOf(postId);
if (index > -1) {
results = [...results];
results.splice(index, 1);
const posts = getPostsForIds(state, results);
dispatch({
type: SearchTypes.RECEIVED_SEARCH_POSTS,
data: {posts, order: results},
});
}
}
export function pinPost(postId) {
return async (dispatch) => {
return async (dispatch, getState) => {
await dispatch(PostActions.pinPost(postId));
AppDispatcher.handleServerAction({
type: ActionTypes.RECEIVED_POST_PINNED,
postId,
});
const state = getState();
const rhsState = getRhsState(state);
if (rhsState === RHSStates.PIN) {
addPostToSearchResults(postId, state, dispatch);
}
};
}
export function unpinPost(postId) {
return async (dispatch) => {
return async (dispatch, getState) => {
await dispatch(PostActions.unpinPost(postId));
AppDispatcher.handleServerAction({
type: ActionTypes.RECEIVED_POST_UNPINNED,
postId,
});
const state = getState();
const rhsState = getRhsState(state);
if (rhsState === RHSStates.PIN) {
removePostFromSearchResults(postId, state, dispatch);
}
};
}
......
......@@ -5,9 +5,10 @@ import thunk from 'redux-thunk';
import configureStore from 'redux-mock-store';
import {Posts} from 'mattermost-redux/constants';
import {SearchTypes} from 'mattermost-redux/action_types';
import * as Actions from 'actions/post_actions';
import {Constants, ActionTypes} from 'utils/constants';
import {Constants, ActionTypes, RHSStates} from 'utils/constants';
const mockStore = configureStore([thunk]);
......@@ -17,6 +18,10 @@ jest.mock('mattermost-redux/actions/posts', () => ({
createPostImmediately: (...args) => ({type: 'MOCK_CREATE_POST_IMMEDIATELY', args}),
getPosts: (...args) => ({type: 'MOCK_GET_POSTS', args}),
getPostsBefore: (...args) => ({type: 'MOCK_GET_POSTS_BEFORE', args}),
flagPost: (...args) => ({type: 'MOCK_FLAG_POST', args}),
unflagPost: (...args) => ({type: 'MOCK_UNFLAG_POST', args}),
pinPost: (...args) => ({type: 'MOCK_PIN_POST', args}),
unpinPost: (...args) => ({type: 'MOCK_UNPIN_POST', args}),
}));
jest.mock('actions/emoji_actions', () => ({
......@@ -144,6 +149,7 @@ describe('Actions.Posts', () => {
},
},
emojis: {customEmoji: {}},
search: {results: []},
},
views: {
posts: {
......@@ -309,4 +315,52 @@ describe('Actions.Posts', () => {
{args: ['emoji_name_1'], type: 'MOCK_ADD_RECENT_EMOJI'},
]);
});
test('flagPost', async () => {
const testStore = await mockStore({...initialState, views: {rhs: {rhsState: RHSStates.FLAG}}});
const post = testStore.getState().entities.posts.posts[latestPost.id];
await testStore.dispatch(Actions.flagPost(post.id));
expect(testStore.getActions()).toEqual([
{args: [post.id], type: 'MOCK_FLAG_POST'},
{data: {posts: {[post.id]: post}, order: [post.id]}, type: SearchTypes.RECEIVED_SEARCH_POSTS},
]);
});
test('unflagPost', async () => {
const testStore = await mockStore({views: {rhs: {rhsState: RHSStates.FLAG}}, entities: {...initialState.entities, search: {results: [latestPost.id]}}});
const post = testStore.getState().entities.posts.posts[latestPost.id];
await testStore.dispatch(Actions.unflagPost(post.id));
expect(testStore.getActions()).toEqual([
{args: [post.id], type: 'MOCK_UNFLAG_POST'},
{data: {posts: [], order: []}, type: SearchTypes.RECEIVED_SEARCH_POSTS},
]);
});
test('pinPost', async () => {
const testStore = await mockStore({...initialState, views: {rhs: {rhsState: RHSStates.PIN}}});
const post = testStore.getState().entities.posts.posts[latestPost.id];
await testStore.dispatch(Actions.pinPost(post.id));
expect(testStore.getActions()).toEqual([
{args: [post.id], type: 'MOCK_PIN_POST'},
{data: {posts: {[post.id]: post}, order: [post.id]}, type: SearchTypes.RECEIVED_SEARCH_POSTS},
]);
});
test('unpinPost', async () => {
const testStore = await mockStore({views: {rhs: {rhsState: RHSStates.PIN}}, entities: {...initialState.entities, search: {results: [latestPost.id]}}});
const post = testStore.getState().entities.posts.posts[latestPost.id];
await testStore.dispatch(Actions.unpinPost(post.id));
expect(testStore.getActions()).toEqual([
{args: [post.id], type: 'MOCK_UNPIN_POST'},
{data: {posts: [], order: []}, type: SearchTypes.RECEIVED_SEARCH_POSTS},
]);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment