gab-social/app/javascript/gabsocial/reducers/chat_messages.js

33 lines
1000 B
JavaScript
Raw Normal View History

import { Map as ImmutableMap, fromJS } from 'immutable'
2020-11-25 21:22:37 +00:00
import {
CHAT_MESSAGES_SEND_SUCCESS,
CHAT_MESSAGES_DELETE_REQUEST,
} from '../actions/chat_messages'
2020-11-25 21:22:37 +00:00
import {
CHAT_MESSAGES_IMPORT,
} from '../actions/importer'
2020-11-25 21:22:37 +00:00
const importChatMessage = (state, chatMessage) => state.set(chatMessage.id, fromJS(chatMessage))
2020-11-25 21:22:37 +00:00
const importChatMessages = (state, chatMessages) =>
state.withMutations((mutable) => chatMessages.forEach((chatMessage) => importChatMessage(mutable, chatMessage)))
2020-11-25 21:22:37 +00:00
const deleteChatMessage = (state, id) => {
return state.delete(id)
}
2020-11-25 21:22:37 +00:00
const initialState = ImmutableMap()
2020-11-25 21:22:37 +00:00
export default function chat_messages(state = initialState, action) {
2020-11-25 21:22:37 +00:00
switch(action.type) {
case CHAT_MESSAGES_IMPORT:
return importChatMessages(state, action.chatMessages)
case CHAT_MESSAGES_SEND_SUCCESS:
return importChatMessage(state, action.chatMessage)
case CHAT_MESSAGES_DELETE_REQUEST:
return deleteChatMessage(state, action.chatMessageId)
2020-11-25 21:22:37 +00:00
default:
return state
2020-11-25 21:22:37 +00:00
}
}