feat: Allow users to mark a conversation as unread (#5924)
Allow users to mark conversations as unread. Loom video: https://www.loom.com/share/ab70552d3c9c48b685da7dfa64be8bb3 fixes: #5552 Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
@@ -8,7 +8,7 @@ import {
|
||||
buildConversationList,
|
||||
isOnMentionsView,
|
||||
} from './helpers/actionHelpers';
|
||||
|
||||
import messageReadActions from './actions/messageReadActions';
|
||||
// actions
|
||||
const actions = {
|
||||
getConversation: async ({ commit }, conversationId) => {
|
||||
@@ -257,17 +257,6 @@ const actions = {
|
||||
dispatch('contacts/setContact', sender);
|
||||
},
|
||||
|
||||
markMessagesRead: async ({ commit }, data) => {
|
||||
try {
|
||||
const {
|
||||
data: { id, agent_last_seen_at: lastSeen },
|
||||
} = await ConversationApi.markMessageRead(data);
|
||||
setTimeout(() => commit(types.MARK_MESSAGE_READ, { id, lastSeen }), 4000);
|
||||
} catch (error) {
|
||||
// Handle error
|
||||
}
|
||||
},
|
||||
|
||||
setChatFilter({ commit }, data) {
|
||||
commit(types.CHANGE_CHAT_STATUS_FILTER, data);
|
||||
},
|
||||
@@ -336,6 +325,7 @@ const actions = {
|
||||
clearConversationFilters({ commit }) {
|
||||
commit(types.CLEAR_CONVERSATION_FILTERS);
|
||||
},
|
||||
...messageReadActions,
|
||||
};
|
||||
|
||||
export default actions;
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { throwErrorMessage } from 'dashboard/store/utils/api';
|
||||
import ConversationApi from '../../../../api/inbox/conversation';
|
||||
import mutationTypes from '../../../mutation-types';
|
||||
|
||||
export default {
|
||||
markMessagesRead: async ({ commit }, data) => {
|
||||
try {
|
||||
const {
|
||||
data: { id, agent_last_seen_at: lastSeen },
|
||||
} = await ConversationApi.markMessageRead(data);
|
||||
setTimeout(
|
||||
() =>
|
||||
commit(mutationTypes.UPDATE_MESSAGE_UNREAD_COUNT, { id, lastSeen }),
|
||||
4000
|
||||
);
|
||||
} catch (error) {
|
||||
// Handle error
|
||||
}
|
||||
},
|
||||
|
||||
markMessagesUnread: async ({ commit }, { id }) => {
|
||||
try {
|
||||
const {
|
||||
data: { agent_last_seen_at: lastSeen, unread_count: unreadCount },
|
||||
} = await ConversationApi.markMessagesUnread({ id });
|
||||
commit(mutationTypes.UPDATE_MESSAGE_UNREAD_COUNT, {
|
||||
id,
|
||||
lastSeen,
|
||||
unreadCount,
|
||||
});
|
||||
} catch (error) {
|
||||
throwErrorMessage(error);
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -146,13 +146,16 @@ export const mutations = {
|
||||
_state.listLoadingStatus = false;
|
||||
},
|
||||
|
||||
[types.MARK_MESSAGE_READ](_state, { id, lastSeen }) {
|
||||
[types.UPDATE_MESSAGE_UNREAD_COUNT](
|
||||
_state,
|
||||
{ id, lastSeen, unreadCount = 0 }
|
||||
) {
|
||||
const [chat] = _state.allConversations.filter(c => c.id === id);
|
||||
if (chat) {
|
||||
chat.agent_last_seen_at = lastSeen;
|
||||
Vue.set(chat, 'agent_last_seen_at', lastSeen);
|
||||
Vue.set(chat, 'unread_count', unreadCount);
|
||||
}
|
||||
},
|
||||
|
||||
[types.CHANGE_CHAT_STATUS_FILTER](_state, data) {
|
||||
_state.chatStatusFilter = data;
|
||||
},
|
||||
|
||||
@@ -245,7 +245,7 @@ describe('#actions', () => {
|
||||
jest.runAllTimers();
|
||||
expect(commit).toHaveBeenCalledTimes(1);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.MARK_MESSAGE_READ, { id: 1, lastSeen }],
|
||||
[types.UPDATE_MESSAGE_UNREAD_COUNT, { id: 1, lastSeen }],
|
||||
]);
|
||||
});
|
||||
it('sends correct mutations if api is unsuccessful', async () => {
|
||||
@@ -255,6 +255,30 @@ describe('#actions', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#markMessagesUnread', () => {
|
||||
it('sends correct mutations if API is successful', async () => {
|
||||
const lastSeen = new Date().getTime() / 1000;
|
||||
axios.post.mockResolvedValue({
|
||||
data: { id: 1, agent_last_seen_at: lastSeen, unread_count: 1 },
|
||||
});
|
||||
await actions.markMessagesUnread({ commit }, { id: 1 });
|
||||
jest.runAllTimers();
|
||||
expect(commit).toHaveBeenCalledTimes(1);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[
|
||||
types.UPDATE_MESSAGE_UNREAD_COUNT,
|
||||
{ id: 1, lastSeen, unreadCount: 1 },
|
||||
],
|
||||
]);
|
||||
});
|
||||
it('sends correct mutations if API is unsuccessful', async () => {
|
||||
axios.post.mockRejectedValue({ message: 'Incorrect header' });
|
||||
await expect(
|
||||
actions.markMessagesUnread({ commit }, { id: 1 })
|
||||
).rejects.toThrow(Error);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#sendEmailTranscript', () => {
|
||||
it('sends correct mutations if api is successful', async () => {
|
||||
axios.post.mockResolvedValue({});
|
||||
|
||||
@@ -11,20 +11,20 @@ describe('#mutations', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#MARK_MESSAGE_READ', () => {
|
||||
describe('#UPDATE_MESSAGE_UNREAD_COUNT', () => {
|
||||
it('mark conversation as read', () => {
|
||||
const state = { allConversations: [{ id: 1 }] };
|
||||
const lastSeen = new Date().getTime() / 1000;
|
||||
mutations[types.MARK_MESSAGE_READ](state, { id: 1, lastSeen });
|
||||
mutations[types.UPDATE_MESSAGE_UNREAD_COUNT](state, { id: 1, lastSeen });
|
||||
expect(state.allConversations).toEqual([
|
||||
{ id: 1, agent_last_seen_at: lastSeen },
|
||||
{ id: 1, agent_last_seen_at: lastSeen, unread_count: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('doesnot send any mutation if chat doesnot exist', () => {
|
||||
const state = { allConversations: [] };
|
||||
const lastSeen = new Date().getTime() / 1000;
|
||||
mutations[types.MARK_MESSAGE_READ](state, { id: 1, lastSeen });
|
||||
mutations[types.UPDATE_MESSAGE_UNREAD_COUNT](state, { id: 1, lastSeen });
|
||||
expect(state.allConversations).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -36,7 +36,7 @@ export default {
|
||||
ADD_MESSAGE: 'ADD_MESSAGE',
|
||||
DELETE_MESSAGE: 'DELETE_MESSAGE',
|
||||
ADD_PENDING_MESSAGE: 'ADD_PENDING_MESSAGE',
|
||||
MARK_MESSAGE_READ: 'MARK_MESSAGE_READ',
|
||||
UPDATE_MESSAGE_UNREAD_COUNT: 'UPDATE_MESSAGE_UNREAD_COUNT',
|
||||
SET_PREVIOUS_CONVERSATIONS: 'SET_PREVIOUS_CONVERSATIONS',
|
||||
SET_ACTIVE_INBOX: 'SET_ACTIVE_INBOX',
|
||||
UPDATE_CONVERSATION_CUSTOM_ATTRIBUTES:
|
||||
|
||||
Reference in New Issue
Block a user