diff --git a/src/store/reducers/messagesReducer.ts b/src/store/reducers/messagesReducer.ts index c7deebb20..139d60d4f 100644 --- a/src/store/reducers/messagesReducer.ts +++ b/src/store/reducers/messagesReducer.ts @@ -25,7 +25,7 @@ const messagesReducer = { [ADD_NEW_USER_MESSAGE]: (state: MessagesState, { text, id }) => ({ ...state, messages: [...state.messages, createNewMessage(text, MESSAGE_SENDER.CLIENT, id)] }), - [ADD_NEW_RESPONSE_MESSAGE]: (state: MessagesState, { text, id }) => + [ADD_NEW_RESPONSE_MESSAGE]: (state: MessagesState, { text, id }) => ({ ...state, messages: [...state.messages, createNewMessage(text, MESSAGE_SENDER.RESPONSE, id)], badgeCount: state.badgeCount + 1 }), [ADD_NEW_LINK_SNIPPET]: (state: MessagesState, { link, id }) => @@ -39,17 +39,20 @@ const messagesReducer = { [HIDE_AVATAR]: (state: MessagesState, { index }) => state.messages[index].showAvatar = false, [DELETE_MESSAGES]: (state: MessagesState, { count, id }) => - ({ - ...state, - messages: id ? - state.messages.filter(message => message.customId !== id) : - state.messages.splice(state.messages.length - 1, count) - }), + ({ + ...state, + messages: id + ? state.messages.filter((_, index) => { + const targetMsg = state.messages.findIndex(tMsg => tMsg.customId === id) + return index < targetMsg - count + 1 || index > targetMsg + }) + : state.messages.slice(0, state.messages.length - count) + }), [SET_BADGE_COUNT]: (state: MessagesState, { count }) => ({ ...state, badgeCount: count }), [MARK_ALL_READ]: (state: MessagesState) => - ({ ...state, messages: state.messages.map(message => ({ ...message, unread: false})), badgeCount: 0}) + ({ ...state, messages: state.messages.map(message => ({ ...message, unread: false })), badgeCount: 0 }) } export default (state = initialState, action: MessagesActions) => createReducer(messagesReducer, state, action);