Chats: sort chats in reducer instead of component

merge-requests/214/head
Alex Gleason 4 years ago
parent d9df091f75
commit 44f7ad5e1c
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

@ -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())),
};
};

@ -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;
}

Loading…
Cancel
Save