2019-09-05 07:31:36 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
import * as types from '../../mutation-types';
|
|
|
|
import ConversationApi from '../../../api/inbox/conversation';
|
2019-10-27 13:31:59 +00:00
|
|
|
import MessageApi from '../../../api/inbox/message';
|
2020-07-25 17:24:45 +00:00
|
|
|
import { MESSAGE_TYPE } from 'widget/helpers/constants';
|
2019-09-05 07:31:36 +00:00
|
|
|
|
|
|
|
// actions
|
|
|
|
const actions = {
|
2020-03-08 16:38:25 +00:00
|
|
|
getConversation: async ({ commit }, conversationId) => {
|
|
|
|
try {
|
|
|
|
const response = await ConversationApi.show(conversationId);
|
2020-06-25 15:34:03 +00:00
|
|
|
commit(types.default.UPDATE_CONVERSATION, response.data);
|
2020-06-02 17:29:02 +00:00
|
|
|
commit(
|
|
|
|
`contacts/${types.default.SET_CONTACT_ITEM}`,
|
|
|
|
response.data.meta.sender
|
|
|
|
);
|
2020-03-08 16:38:25 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-26 15:45:01 +00:00
|
|
|
fetchAllConversations: async ({ commit, dispatch }, params) => {
|
2019-09-05 07:31:36 +00:00
|
|
|
commit(types.default.SET_LIST_LOADING_STATUS);
|
2019-10-27 13:31:59 +00:00
|
|
|
try {
|
|
|
|
const response = await ConversationApi.get(params);
|
|
|
|
const { data } = response.data;
|
|
|
|
const { payload: chatList, meta: metaData } = data;
|
|
|
|
commit(types.default.SET_ALL_CONVERSATION, chatList);
|
2020-06-14 08:37:52 +00:00
|
|
|
dispatch('conversationStats/set', metaData);
|
2020-06-25 15:34:03 +00:00
|
|
|
dispatch('conversationLabels/setBulkConversationLabels', chatList);
|
2019-09-05 07:31:36 +00:00
|
|
|
commit(types.default.CLEAR_LIST_LOADING_STATUS);
|
2020-06-02 17:29:02 +00:00
|
|
|
commit(
|
|
|
|
`contacts/${types.default.SET_CONTACTS}`,
|
|
|
|
chatList.map(chat => chat.meta.sender)
|
|
|
|
);
|
2020-02-26 15:45:01 +00:00
|
|
|
dispatch(
|
|
|
|
'conversationPage/setCurrentPage',
|
|
|
|
{
|
|
|
|
filter: params.assigneeType,
|
|
|
|
page: params.page,
|
|
|
|
},
|
|
|
|
{ root: true }
|
|
|
|
);
|
|
|
|
if (!chatList.length) {
|
|
|
|
dispatch(
|
|
|
|
'conversationPage/setEndReached',
|
|
|
|
{ filter: params.assigneeType },
|
|
|
|
{ root: true }
|
|
|
|
);
|
|
|
|
}
|
2019-10-27 13:31:59 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
emptyAllConversations({ commit }) {
|
|
|
|
commit(types.default.EMPTY_ALL_CONVERSATION);
|
|
|
|
},
|
|
|
|
|
|
|
|
clearSelectedState({ commit }) {
|
|
|
|
commit(types.default.CLEAR_CURRENT_CHAT_WINDOW);
|
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
fetchPreviousMessages: async ({ commit }, data) => {
|
|
|
|
try {
|
2020-01-01 17:00:43 +00:00
|
|
|
const {
|
|
|
|
data: { meta, payload },
|
|
|
|
} = await MessageApi.getPreviousMessages(data);
|
|
|
|
commit(
|
|
|
|
`conversationMetadata/${types.default.SET_CONVERSATION_METADATA}`,
|
|
|
|
{
|
|
|
|
id: data.conversationId,
|
|
|
|
data: meta,
|
|
|
|
}
|
|
|
|
);
|
2019-10-27 13:31:59 +00:00
|
|
|
commit(types.default.SET_PREVIOUS_CONVERSATIONS, {
|
|
|
|
id: data.conversationId,
|
2020-01-01 17:00:43 +00:00
|
|
|
data: payload,
|
2019-10-27 13:31:59 +00:00
|
|
|
});
|
2020-01-01 17:00:43 +00:00
|
|
|
if (payload.length < 20) {
|
2019-10-27 13:31:59 +00:00
|
|
|
commit(types.default.SET_ALL_MESSAGES_LOADED);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2020-07-23 18:03:17 +00:00
|
|
|
async setActiveChat({ commit, dispatch }, data) {
|
|
|
|
commit(types.default.SET_CURRENT_CHAT_WINDOW, data);
|
2019-09-05 07:31:36 +00:00
|
|
|
commit(types.default.CLEAR_ALL_MESSAGES_LOADED);
|
|
|
|
|
|
|
|
if (data.dataFetched === undefined) {
|
2020-07-23 18:03:17 +00:00
|
|
|
try {
|
|
|
|
await dispatch('fetchPreviousMessages', {
|
2019-10-27 13:31:59 +00:00
|
|
|
conversationId: data.id,
|
2019-09-05 07:31:36 +00:00
|
|
|
before: data.messages[0].id,
|
2020-07-23 18:03:17 +00:00
|
|
|
});
|
|
|
|
Vue.set(data, 'dataFetched', true);
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
assignAgent: async ({ commit }, { conversationId, agentId }) => {
|
|
|
|
try {
|
|
|
|
const response = await ConversationApi.assignAgent({
|
|
|
|
conversationId,
|
|
|
|
agentId,
|
2019-09-05 07:31:36 +00:00
|
|
|
});
|
2019-10-27 13:31:59 +00:00
|
|
|
commit(types.default.ASSIGN_AGENT, response.data);
|
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2020-06-02 17:29:02 +00:00
|
|
|
toggleStatus: async ({ commit }, data) => {
|
2019-10-27 13:31:59 +00:00
|
|
|
try {
|
|
|
|
const response = await ConversationApi.toggleStatus(data);
|
|
|
|
commit(
|
|
|
|
types.default.RESOLVE_CONVERSATION,
|
|
|
|
response.data.payload.current_status
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
sendMessage: async ({ commit }, data) => {
|
|
|
|
try {
|
|
|
|
const response = await MessageApi.create(data);
|
|
|
|
commit(types.default.SEND_MESSAGE, response.data);
|
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
addMessage({ commit }, message) {
|
|
|
|
commit(types.default.ADD_MESSAGE, message);
|
2020-07-25 17:24:45 +00:00
|
|
|
if (message.message_type === MESSAGE_TYPE.INCOMING) {
|
|
|
|
commit(types.default.SET_CONVERSATION_CAN_REPLY, {
|
|
|
|
conversationId: message.conversation_id,
|
|
|
|
canReply: true,
|
|
|
|
});
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2020-04-17 15:45:20 +00:00
|
|
|
updateMessage({ commit }, message) {
|
|
|
|
commit(types.default.ADD_MESSAGE, message);
|
|
|
|
},
|
|
|
|
|
2020-07-04 14:16:17 +00:00
|
|
|
addConversation({ commit, state, dispatch }, conversation) {
|
2020-04-18 14:55:58 +00:00
|
|
|
const { currentInbox } = state;
|
2020-07-04 14:16:17 +00:00
|
|
|
const {
|
|
|
|
inbox_id: inboxId,
|
|
|
|
meta: { sender },
|
|
|
|
} = conversation;
|
|
|
|
if (!currentInbox || Number(currentInbox) === inboxId) {
|
2020-04-18 14:55:58 +00:00
|
|
|
commit(types.default.ADD_CONVERSATION, conversation);
|
2020-07-04 14:16:17 +00:00
|
|
|
dispatch('contacts/setContact', sender);
|
2020-04-18 14:55:58 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-07-04 14:16:17 +00:00
|
|
|
updateConversation({ commit, dispatch }, conversation) {
|
|
|
|
const {
|
|
|
|
meta: { sender },
|
|
|
|
} = conversation;
|
2020-04-18 14:55:58 +00:00
|
|
|
commit(types.default.UPDATE_CONVERSATION, conversation);
|
2020-07-04 14:16:17 +00:00
|
|
|
dispatch('contacts/setContact', sender);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
markMessagesRead: async ({ commit }, data) => {
|
|
|
|
try {
|
2020-08-03 08:10:20 +00:00
|
|
|
const {
|
|
|
|
data: { id, agent_last_seen_at: lastSeen },
|
|
|
|
} = await ConversationApi.markMessageRead(data);
|
|
|
|
setTimeout(
|
|
|
|
() =>
|
|
|
|
commit(types.default.MARK_MESSAGE_READ, {
|
|
|
|
id,
|
|
|
|
lastSeen,
|
|
|
|
}),
|
|
|
|
4000
|
|
|
|
);
|
2019-10-27 13:31:59 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setChatFilter({ commit }, data) {
|
|
|
|
commit(types.default.CHANGE_CHAT_STATUS_FILTER, data);
|
|
|
|
},
|
|
|
|
|
|
|
|
updateAssignee({ commit }, data) {
|
|
|
|
commit(types.default.UPDATE_ASSIGNEE, data);
|
|
|
|
},
|
|
|
|
|
2020-06-02 17:29:02 +00:00
|
|
|
updateConversationContact({ commit }, data) {
|
|
|
|
if (data.id) {
|
|
|
|
commit(`contacts/${types.default.SET_CONTACT_ITEM}`, data);
|
|
|
|
}
|
|
|
|
commit(types.default.UPDATE_CONVERSATION_CONTACT, data);
|
|
|
|
},
|
|
|
|
|
2019-09-05 07:31:36 +00:00
|
|
|
setActiveInbox({ commit }, inboxId) {
|
|
|
|
commit(types.default.SET_ACTIVE_INBOX, inboxId);
|
|
|
|
},
|
2020-03-22 10:24:36 +00:00
|
|
|
|
|
|
|
sendAttachment: async ({ commit }, data) => {
|
|
|
|
try {
|
2020-04-17 15:45:20 +00:00
|
|
|
const response = await MessageApi.sendAttachment(data);
|
2020-03-22 10:24:36 +00:00
|
|
|
commit(types.default.SEND_MESSAGE, response.data);
|
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
|
|
|
},
|
2020-05-26 12:13:59 +00:00
|
|
|
|
|
|
|
muteConversation: async ({ commit }, conversationId) => {
|
|
|
|
try {
|
|
|
|
await ConversationApi.mute(conversationId);
|
|
|
|
commit(types.default.MUTE_CONVERSATION);
|
|
|
|
} catch (error) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
},
|
2020-08-17 05:55:13 +00:00
|
|
|
|
|
|
|
sendEmailTranscript: async (_, { conversationId, email }) => {
|
|
|
|
try {
|
|
|
|
await ConversationApi.sendEmailTranscript({ conversationId, email });
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2019-09-05 07:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default actions;
|