feat: Refactor getters for multiple conversations

This commit is contained in:
Nithin David 2021-09-20 10:13:39 +05:30
parent bf98ccb6fb
commit dd7936921b
2 changed files with 27 additions and 15 deletions

View file

@ -4,28 +4,40 @@ import { groupConversationBySender } from './helpers';
import { formatUnixDate } from 'shared/helpers/DateHelper'; import { formatUnixDate } from 'shared/helpers/DateHelper';
export const getters = { export const getters = {
getAllMessagesLoaded: _state => _state.uiFlags.allMessagesLoaded, isAllMessagesFetched: _state => conversationId =>
getIsCreating: _state => _state.uiFlags.isCreating, _state.conversations[conversationId].uiFlags.allFetched,
getIsAgentTyping: _state => _state.uiFlags.isAgentTyping, isCreating: _state => _state.uiFlags.isCreating,
getConversation: _state => _state.conversations, isAgentTypingIn: _state => conversationId =>
getConversationSize: _state => Object.keys(_state.conversations).length, _state.conversations[conversationId].uiFlags.isAgentTyping,
getEarliestMessage: _state => { allConversations: _state =>
const conversation = Object.values(_state.conversations); _state.conversations.allIds.map(id => _state.conversations.byId[id]),
if (conversation.length) { totalConversationsLength: _state => _state.conversations.allIds.length,
return conversation[0]; firstMessageIn: (...getterArguments) => conversationId => {
const { _state, _rootGetters } = getterArguments;
const messagesInConversation =
_state.conversations.byId[conversationId].messages;
if (messagesInConversation.length) {
const messageId = messagesInConversation[0];
const lastMessage = _rootGetters.messageV2.getMessageById(messageId);
return lastMessage;
} }
return {}; return {};
}, },
getGroupedConversation: _state => { groupByMessagesIn: (...getterArguments) => conversationId => {
const conversationGroupedByDate = groupBy( const { _state, _rootGetters } = getterArguments;
Object.values(_state.conversations), const messageIds = _state.conversations.byId[conversationId].messages;
message => formatUnixDate(message.created_at) const messagesInConversation = messageIds.map(messageId =>
_rootGetters.messageV2.getMessageById(messageId)
);
const conversationGroupedByDate = groupBy(messagesInConversation, message =>
formatUnixDate(message.created_at)
); );
return Object.keys(conversationGroupedByDate).map(date => ({ return Object.keys(conversationGroupedByDate).map(date => ({
date, date,
messages: groupConversationBySender(conversationGroupedByDate[date]), messages: groupConversationBySender(conversationGroupedByDate[date]),
})); }));
}, },
getIsFetchingList: _state => _state.uiFlags.isFetchingList, getIsFetchingList: _state => _state.uiFlags.isFetchingList,
getMessageCount: _state => { getMessageCount: _state => {
return Object.values(_state.conversations).length; return Object.values(_state.conversations).length;

View file

@ -8,12 +8,12 @@ const state = {
allIds: [], allIds: [],
uiFlags: { uiFlags: {
byId: { byId: {
// 1: { allMessagesLoaded: false, isAgentTyping: false, isFetching: false }, // 1: { allFetched: false, isAgentTyping: false, isFetching: false },
}, },
}, },
}, },
uiFlags: { uiFlags: {
allConversationsLoaded: false, allFetched: false,
isFetching: false, isFetching: false,
isCreating: true, isCreating: true,
}, },