feat: set count from store on action cable event

This commit is contained in:
Shivam Mishra
2023-06-29 17:57:19 +05:30
parent 191b8a64fe
commit 7066f66fa7
4 changed files with 28 additions and 6 deletions
@@ -111,6 +111,7 @@ class ActionCableConnector extends BaseActionCableConnector {
} = data;
DashboardAudioNotificationHelper.onNewMessage(data);
this.app.$store.dispatch('addMessage', data);
this.app.$store.dispatch('updateUnreadCount');
this.app.$store.dispatch('updateConversationLastActivity', {
lastActivityAt,
conversationId,
@@ -289,6 +289,17 @@ const actions = {
}
},
updateUnreadCount({ getters }) {
const { installationName } = window.chatwootConfig;
const unreadCount = getters.getUnreadConversationsCount;
if (unreadCount === 0) {
document.title = installationName;
} else {
document.title = `${installationName} (${getters.getUnreadConversationsCount})`;
}
},
updateMessage({ commit }, message) {
commit(types.ADD_MESSAGE, message);
},
@@ -10,6 +10,15 @@ export const getSelectedChatConversation = ({
}) =>
allConversations.filter(conversation => conversation.id === selectedChatId);
export const getUnreadCountForChat = chat => {
return chat.messages.filter(
chatMessage =>
chatMessage.created_at * 1000 > chat.agent_last_seen_at * 1000 &&
chatMessage.message_type === 0 &&
chatMessage.private !== true
).length;
};
const sortComparator = {
latest: (a, b) => b.last_activity_at - a.last_activity_at,
sort_on_created_at: (a, b) => a.created_at - b.created_at,
@@ -107,15 +116,15 @@ const getters = {
? false
: chat.allMessagesLoaded;
},
getUnreadConversationsCount({ allConversations }) {
return allConversations.filter(chat => {
return getUnreadCountForChat(chat) > 0;
}).length;
},
getUnreadCount(_state) {
const [chat] = getSelectedChatConversation(_state);
if (!chat) return [];
return chat.messages.filter(
chatMessage =>
chatMessage.created_at * 1000 > chat.agent_last_seen_at * 1000 &&
chatMessage.message_type === 0 &&
chatMessage.private !== true
).length;
return getUnreadCountForChat(chat);
},
getChatStatusFilter: ({ chatStatusFilter }) => chatStatusFilter,
getChatSortFilter: ({ chatSortFilter }) => chatSortFilter,