Skip to content

Commit

Permalink
fix: reply to messages does not make the message disappear from list
Browse files Browse the repository at this point in the history
  • Loading branch information
nubsthead committed Feb 25, 2022
1 parent f72ab4c commit efe4cb5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/store/conversations-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
searchConv
} from './actions';
import {
handleAddMessagesInConversationReducer,
handleCreatedConversationsReducer,
handleCreatedMessagesInConversationsReducer,
handleDeletedConversationsReducer,
Expand Down Expand Up @@ -183,6 +184,7 @@ export const conversationsSlice = createSlice({
handleCreatedMessagesInConversation: produce(handleCreatedMessagesInConversationsReducer),
handleModifiedMessagesInConversation: produce(handleModifiedMessagesInConversationReducer),
handleDeletedMessagesInConversation: produce(handleDeletedMessagesInConversationReducer),
handleAddMessagesInConversation: produce(handleAddMessagesInConversationReducer),
setCurrentFolder: produce(setCurrentFolderReducer),
setSearchedInFolder: produce(setSearchedInFolderReducer)
},
Expand All @@ -208,6 +210,7 @@ export const {
handleNotifyDeletedConversations,
handleCreatedMessagesInConversation,
handleModifiedMessagesInConversation,
handleAddMessagesInConversation,
handleDeletedMessagesInConversation,
setSearchedInFolder
} = conversationsSlice.actions;
Expand Down
17 changes: 16 additions & 1 deletion src/store/sync/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const handleModifiedMessagesInConversationReducer = (
return messageToUpdate
? {
...msg,
parent: messageToUpdate.parent
...messageToUpdate
}
: msg;
})
Expand All @@ -131,6 +131,21 @@ export const handleModifiedMessagesInConversationReducer = (
);
};

export const handleAddMessagesInConversationReducer = (
state: ConversationsStateType,
{ payload }: Payload
): void => {
forEach(payload, (msg) => {
const addMsg = omit(msg, ['conversation']) as ConvMessage;
if (msg?.conversation && state?.conversations?.[msg?.conversation]) {
state.conversations[msg.conversation] = {
...state.conversations[msg.conversation],
messages: [...state.conversations[msg.conversation].messages, addMsg]
};
}
});
};

export const handleDeletedConversationsReducer = (
state: ConversationsStateType,
{ payload }: Payload
Expand Down
39 changes: 34 additions & 5 deletions src/views/sidebar/sync-data-handler.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { combineReducers } from '@reduxjs/toolkit';
import { isEmpty, map, keyBy, find, filter, forEach, sortBy } from 'lodash';
import { isEmpty, map, keyBy, find, filter, forEach, sortBy, reduce } from 'lodash';
import { useTranslation } from 'react-i18next';
import {
handleCreatedFolders,
Expand All @@ -33,13 +33,16 @@ import {
handleDeletedMessagesInConversation,
setSearchedInFolder,
selectCurrentFolder,
handleCreatedMessagesInConversation
handleCreatedMessagesInConversation,
selectConversations,
handleAddMessagesInConversation
} from '../../store/conversations-slice';
import {
messageSliceReducer,
handleCreatedMessages,
handleModifiedMessages,
handleDeletedMessages
handleDeletedMessages,
selectMessages
} from '../../store/messages-slice';
import { normalizeConversation } from '../../normalizations/normalize-conversation';
import { normalizeMailMessageFromSoap } from '../../normalizations/normalize-message';
Expand Down Expand Up @@ -69,6 +72,8 @@ export const SyncDataHandler = () => {
const dispatch = useDispatch();
const [initialized, setInitialized] = useState(false);
const currentFolder = useSelector(selectCurrentFolder);
const conversationState = useSelector(selectConversations);
const messagesState = useSelector(selectMessages);

useEffect(() => {
if (!isEmpty(refresh) && !initialized) {
Expand Down Expand Up @@ -143,12 +148,36 @@ export const SyncDataHandler = () => {
);
dispatch(handleModifiedMessages(messages));

// the condition filters message with parent property (the only ones we need to update)
// the condition filters messages with parent property (the only ones we need to update)
const toUpdate = filter(messages, 'parent');
if (toUpdate?.length > 0) {
// this function updates messages' parent in conversations. If parent never changes it does not need to be called
dispatch(handleModifiedMessagesInConversation(toUpdate));
}
// the condition filters messages with conversation property (the only ones we need to add to conversation)
const conversationToUpdate = filter(messages, 'conversation');
if (conversationToUpdate?.length > 0) {
const msgsReference = reduce(
conversationToUpdate,
(acc, msg) => {
if (messagesState?.[msg?.id]) {
return [
...acc,
{
id: messagesState?.[msg?.id].id,
parent: messagesState?.[msg?.id].parent,
date: messagesState?.[msg?.id].date,
conversation: msg.conversation
}
];
}
return acc;
},
[]
);
// this function add messages' in conversations. If conversation never changes it does not need to be called
dispatch(handleAddMessagesInConversation(msgsReference));
}
}
}
if (notify.deleted) {
Expand All @@ -162,6 +191,6 @@ export const SyncDataHandler = () => {
});
}
}
}, [dispatch, initialized, notifyList, seq, t]);
}, [dispatch, initialized, messagesState, notifyList, seq, t]);
return <InboxBadgeUpdater />;
};

0 comments on commit efe4cb5

Please sign in to comment.