2019-09-05 07:31:36 +00:00
|
|
|
import authAPI from '../../../api/auth';
|
2021-03-20 12:12:29 +00:00
|
|
|
import { applyPageFilters } from './helpers';
|
2019-09-05 07:31:36 +00:00
|
|
|
|
|
|
|
export const getSelectedChatConversation = ({
|
|
|
|
allConversations,
|
2020-07-23 18:03:17 +00:00
|
|
|
selectedChatId,
|
2019-09-05 07:31:36 +00:00
|
|
|
}) =>
|
2020-07-23 18:03:17 +00:00
|
|
|
allConversations.filter(conversation => conversation.id === selectedChatId);
|
2019-09-05 07:31:36 +00:00
|
|
|
|
|
|
|
// getters
|
|
|
|
const getters = {
|
2020-06-02 17:29:02 +00:00
|
|
|
getAllConversations: ({ allConversations }) =>
|
|
|
|
allConversations.sort(
|
|
|
|
(a, b) => b.messages.last()?.created_at - a.messages.last()?.created_at
|
|
|
|
),
|
2020-07-23 18:03:17 +00:00
|
|
|
getSelectedChat: ({ selectedChatId, allConversations }) => {
|
|
|
|
const selectedChat = allConversations.find(
|
|
|
|
conversation => conversation.id === selectedChatId
|
|
|
|
);
|
|
|
|
return selectedChat || {};
|
|
|
|
},
|
2021-03-20 12:12:29 +00:00
|
|
|
getMineChats: _state => activeFilters => {
|
2019-09-05 07:31:36 +00:00
|
|
|
const currentUserID = authAPI.getCurrentUser().id;
|
2021-03-20 12:12:29 +00:00
|
|
|
|
|
|
|
return _state.allConversations.filter(conversation => {
|
|
|
|
const { assignee } = conversation.meta;
|
|
|
|
const isAssignedToMe = assignee && assignee.id === currentUserID;
|
|
|
|
const shouldFilter = applyPageFilters(conversation, activeFilters);
|
|
|
|
const isChatMine = isAssignedToMe && shouldFilter;
|
|
|
|
|
|
|
|
return isChatMine;
|
|
|
|
});
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
2021-03-20 12:12:29 +00:00
|
|
|
getUnAssignedChats: _state => activeFilters => {
|
|
|
|
return _state.allConversations.filter(conversation => {
|
|
|
|
const isUnAssigned = !conversation.meta.assignee;
|
|
|
|
const shouldFilter = applyPageFilters(conversation, activeFilters);
|
|
|
|
return isUnAssigned && shouldFilter;
|
|
|
|
});
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
2021-03-20 12:12:29 +00:00
|
|
|
getAllStatusChats: _state => activeFilters => {
|
|
|
|
return _state.allConversations.filter(conversation => {
|
|
|
|
const shouldFilter = applyPageFilters(conversation, activeFilters);
|
|
|
|
return shouldFilter;
|
|
|
|
});
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
getChatListLoadingStatus: ({ listLoadingStatus }) => listLoadingStatus,
|
|
|
|
getAllMessagesLoaded(_state) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
2020-03-08 16:38:25 +00:00
|
|
|
return !chat || chat.allMessagesLoaded === undefined
|
2019-09-05 07:31:36 +00:00
|
|
|
? false
|
|
|
|
: chat.allMessagesLoaded;
|
|
|
|
},
|
|
|
|
getUnreadCount(_state) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
2020-03-08 16:38:25 +00:00
|
|
|
if (!chat) return [];
|
2019-09-05 07:31:36 +00:00
|
|
|
return chat.messages.filter(
|
|
|
|
chatMessage =>
|
|
|
|
chatMessage.created_at * 1000 > chat.agent_last_seen_at * 1000 &&
|
2020-02-22 14:46:56 +00:00
|
|
|
chatMessage.message_type === 0 &&
|
|
|
|
chatMessage.private !== true
|
2019-09-05 07:31:36 +00:00
|
|
|
).length;
|
|
|
|
},
|
|
|
|
getChatStatusFilter: ({ chatStatusFilter }) => chatStatusFilter,
|
|
|
|
getSelectedInbox: ({ currentInbox }) => currentInbox,
|
2021-07-05 06:31:54 +00:00
|
|
|
getConversationById: _state => conversationId => {
|
2021-08-02 07:41:07 +00:00
|
|
|
return _state.allConversations.find(
|
|
|
|
value => value.id === Number(conversationId)
|
|
|
|
);
|
2021-07-05 06:31:54 +00:00
|
|
|
},
|
2019-09-05 07:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default getters;
|