From 44f7ad5e1cfc8c9677352265ac651fb89f05b471 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 27 Aug 2020 13:45:37 -0500 Subject: [PATCH] Chats: sort chats in reducer instead of component --- .../features/chats/components/chat_list.js | 13 +------------ app/soapbox/reducers/chats.js | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/soapbox/features/chats/components/chat_list.js b/app/soapbox/features/chats/components/chat_list.js index 0928c3040..ca5349b20 100644 --- a/app/soapbox/features/chats/components/chat_list.js +++ b/app/soapbox/features/chats/components/chat_list.js @@ -10,18 +10,7 @@ import { makeGetChat } from 'soapbox/selectors'; const mapStateToProps = state => { const getChat = makeGetChat(); return { - chats: state.get('chats').map(chat => - getChat(state, chat.toJS()) - ).sort((valueA, valueB) => { - // Sort most recently updated chats at the top - const a = new Date(valueA.get('updated_at')); - const b = new Date(valueB.get('updated_at')); - - if (a === b) return 0; - if (a > b) return -1; - if (a < b) return 1; - return 0; - }), + chats: state.get('chats').map(chat => getChat(state, chat.toJS())), }; }; diff --git a/app/soapbox/reducers/chats.js b/app/soapbox/reducers/chats.js index 7659e38e7..6c85bfe7b 100644 --- a/app/soapbox/reducers/chats.js +++ b/app/soapbox/reducers/chats.js @@ -8,16 +8,27 @@ const importChat = (state, chat) => state.set(chat.id, fromJS(normalizeChat(chat const importChats = (state, chats) => state.withMutations(mutable => chats.forEach(chat => importChat(mutable, chat))); +const chatDateComparator = (chatA, chatB) => { + // Sort most recently updated chats at the top + const a = new Date(chatA.get('updated_at')); + const b = new Date(chatB.get('updated_at')); + + if (a === b) return 0; + if (a > b) return -1; + if (a < b) return 1; + return 0; +}; + const initialState = ImmutableMap(); export default function chats(state = initialState, action) { switch(action.type) { case CHATS_FETCH_SUCCESS: - return importChats(state, action.chats); + return importChats(state, action.chats).sort(chatDateComparator); case STREAMING_CHAT_UPDATE: - return importChats(state, [action.chat]); + return importChats(state, [action.chat]).sort(chatDateComparator); case CHAT_FETCH_SUCCESS: - return importChats(state, [action.chat]); + return importChats(state, [action.chat]).sort(chatDateComparator); default: return state; }