Chats: sort chats in reducer instead of component
This commit is contained in:
parent
d9df091f75
commit
44f7ad5e1c
|
@ -10,18 +10,7 @@ import { makeGetChat } from 'soapbox/selectors';
|
||||||
const mapStateToProps = state => {
|
const mapStateToProps = state => {
|
||||||
const getChat = makeGetChat();
|
const getChat = makeGetChat();
|
||||||
return {
|
return {
|
||||||
chats: state.get('chats').map(chat =>
|
chats: state.get('chats').map(chat => getChat(state, chat.toJS())),
|
||||||
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;
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -8,16 +8,27 @@ const importChat = (state, chat) => state.set(chat.id, fromJS(normalizeChat(chat
|
||||||
const importChats = (state, chats) =>
|
const importChats = (state, chats) =>
|
||||||
state.withMutations(mutable => chats.forEach(chat => importChat(mutable, chat)));
|
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();
|
const initialState = ImmutableMap();
|
||||||
|
|
||||||
export default function chats(state = initialState, action) {
|
export default function chats(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case CHATS_FETCH_SUCCESS:
|
case CHATS_FETCH_SUCCESS:
|
||||||
return importChats(state, action.chats);
|
return importChats(state, action.chats).sort(chatDateComparator);
|
||||||
case STREAMING_CHAT_UPDATE:
|
case STREAMING_CHAT_UPDATE:
|
||||||
return importChats(state, [action.chat]);
|
return importChats(state, [action.chat]).sort(chatDateComparator);
|
||||||
case CHAT_FETCH_SUCCESS:
|
case CHAT_FETCH_SUCCESS:
|
||||||
return importChats(state, [action.chat]);
|
return importChats(state, [action.chat]).sort(chatDateComparator);
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue