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';
|
|
|
|
import FBChannel from '../../../api/channel/fbChannel';
|
2019-09-05 07:31:36 +00:00
|
|
|
|
|
|
|
// actions
|
|
|
|
const actions = {
|
2019-10-27 13:31:59 +00:00
|
|
|
fetchAllConversations: async ({ commit }, 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);
|
|
|
|
commit(types.default.SET_CONV_TAB_META, metaData);
|
2019-09-05 07:31:36 +00:00
|
|
|
commit(types.default.CLEAR_LIST_LOADING_STATUS);
|
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
|
|
|
},
|
|
|
|
|
|
|
|
setActiveChat(store, data) {
|
|
|
|
const { commit } = store;
|
|
|
|
const localDispatch = store.dispatch;
|
|
|
|
let donePromise = null;
|
|
|
|
|
|
|
|
commit(types.default.CURRENT_CHAT_WINDOW, data);
|
|
|
|
commit(types.default.CLEAR_ALL_MESSAGES_LOADED);
|
|
|
|
|
|
|
|
if (data.dataFetched === undefined) {
|
|
|
|
donePromise = new Promise(resolve => {
|
|
|
|
localDispatch('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,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
Vue.set(data, 'dataFetched', true);
|
|
|
|
resolve();
|
|
|
|
})
|
2019-10-27 13:31:59 +00:00
|
|
|
.catch(() => {
|
|
|
|
// Handle error
|
2019-09-05 07:31:36 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
donePromise = new Promise(resolve => {
|
|
|
|
commit(types.default.SET_CHAT_META, { id: data.id });
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return donePromise;
|
|
|
|
},
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
toggleStatus: async ({ commit }, data) => {
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
addConversation({ commit }, conversation) {
|
|
|
|
commit(types.default.ADD_CONVERSATION, conversation);
|
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
toggleTyping: async ({ commit }, { status, inboxId, contactId }) => {
|
|
|
|
try {
|
|
|
|
await FBChannel.toggleTyping({ status, inboxId, contactId });
|
|
|
|
commit(types.default.FB_TYPING, { status });
|
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
markSeen: async ({ commit }, data) => {
|
|
|
|
try {
|
|
|
|
await FBChannel.markSeen(data);
|
|
|
|
commit(types.default.MARK_SEEN);
|
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
markMessagesRead: async ({ commit }, data) => {
|
2019-09-05 07:31:36 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
commit(types.default.MARK_MESSAGE_READ, data);
|
|
|
|
}, 4000);
|
2019-10-27 13:31:59 +00:00
|
|
|
try {
|
|
|
|
await ConversationApi.markMessageRead(data);
|
|
|
|
} 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);
|
|
|
|
},
|
|
|
|
|
|
|
|
setActiveInbox({ commit }, inboxId) {
|
|
|
|
commit(types.default.SET_ACTIVE_INBOX, inboxId);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default actions;
|