2019-09-05 07:31:36 +00:00
|
|
|
import authAPI from '../../../api/auth';
|
|
|
|
|
|
|
|
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 || {};
|
|
|
|
},
|
2019-09-05 07:31:36 +00:00
|
|
|
getMineChats(_state) {
|
|
|
|
const currentUserID = authAPI.getCurrentUser().id;
|
|
|
|
return _state.allConversations.filter(chat =>
|
2020-07-27 16:49:26 +00:00
|
|
|
!chat.meta.assignee
|
2019-09-05 07:31:36 +00:00
|
|
|
? false
|
|
|
|
: chat.status === _state.chatStatusFilter &&
|
|
|
|
chat.meta.assignee.id === currentUserID
|
|
|
|
);
|
|
|
|
},
|
|
|
|
getUnAssignedChats(_state) {
|
|
|
|
return _state.allConversations.filter(
|
2020-07-27 16:49:26 +00:00
|
|
|
chat => !chat.meta.assignee && chat.status === _state.chatStatusFilter
|
2019-09-05 07:31:36 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
getAllStatusChats(_state) {
|
|
|
|
return _state.allConversations.filter(
|
|
|
|
chat => chat.status === _state.chatStatusFilter
|
|
|
|
);
|
|
|
|
},
|
|
|
|
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,
|
2020-06-02 17:29:02 +00:00
|
|
|
getNextChatConversation: _state => {
|
2020-07-23 18:03:17 +00:00
|
|
|
const [selectedChat] = getSelectedChatConversation(_state);
|
2020-05-14 08:13:02 +00:00
|
|
|
const conversations = getters.getAllStatusChats(_state);
|
|
|
|
if (conversations.length <= 1) {
|
|
|
|
return null;
|
2020-06-02 17:29:02 +00:00
|
|
|
}
|
2020-05-14 08:13:02 +00:00
|
|
|
const currentIndex = conversations.findIndex(
|
|
|
|
conversation => conversation.id === selectedChat.id
|
|
|
|
);
|
|
|
|
const nextIndex = (currentIndex + 1) % conversations.length;
|
|
|
|
return conversations[nextIndex];
|
|
|
|
},
|
2019-09-05 07:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default getters;
|