bigbuffet-rw/app/soapbox/reducers/chat_messages.js

37 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-08-25 19:31:34 -07:00
import {
2020-08-26 15:29:22 -07:00
CHATS_FETCH_SUCCESS,
2020-08-25 19:31:34 -07:00
CHAT_MESSAGES_FETCH_SUCCESS,
CHAT_MESSAGE_SEND_SUCCESS,
} from 'soapbox/actions/chats';
2020-08-26 15:29:22 -07:00
import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
import { Map as ImmutableMap, fromJS } from 'immutable';
2020-08-25 18:33:49 -07:00
const initialState = ImmutableMap();
2020-08-26 11:39:38 -07:00
const importMessage = (state, message) => {
return state.set(message.get('id'), message);
2020-08-26 11:39:38 -07:00
};
const importMessages = (state, messages) =>
state.withMutations(mutable =>
messages.forEach(message => importMessage(mutable, message)));
2020-08-26 11:39:38 -07:00
const importLastMessages = (state, chats) =>
state.withMutations(mutable =>
chats.forEach(chat => importMessage(mutable, chat.get('last_message'))));
2020-08-25 18:33:49 -07:00
export default function chatMessages(state = initialState, action) {
switch(action.type) {
2020-08-26 15:29:22 -07:00
case CHATS_FETCH_SUCCESS:
return importLastMessages(state, fromJS(action.chats));
2020-08-25 18:33:49 -07:00
case CHAT_MESSAGES_FETCH_SUCCESS:
return importMessages(state, fromJS(action.chatMessages));
case CHAT_MESSAGE_SEND_SUCCESS:
return importMessage(state, fromJS(action.chatMessage));
2020-08-26 15:29:22 -07:00
case STREAMING_CHAT_UPDATE:
return importLastMessages(state, fromJS([action.chat]));
2020-08-25 18:33:49 -07:00
default:
return state;
}
};