Skip to content

Commit

Permalink
Merge pull request #254 from thiagocavalcanti/support/delete-messages
Browse files Browse the repository at this point in the history
fix behavior when deleting messages passing count and id params
  • Loading branch information
Martín Callegari authored May 10, 2021
2 parents 9139321 + 37bc2f6 commit b504594
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/store/reducers/messagesReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) =>
Expand All @@ -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);

0 comments on commit b504594

Please sign in to comment.