2021-01-17 17:14:03 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
import { MESSAGE_TYPE } from 'widget/helpers/constants';
|
|
|
|
import { findUndeliveredMessage } from './helpers';
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
clearConversations($state) {
|
|
|
|
Vue.set($state, 'conversations', {});
|
|
|
|
},
|
|
|
|
pushMessageToConversation($state, message) {
|
|
|
|
const { id, status, message_type: type } = message;
|
2021-07-15 08:57:37 +00:00
|
|
|
|
2021-01-17 17:14:03 +00:00
|
|
|
const messagesInbox = $state.conversations;
|
|
|
|
const isMessageIncoming = type === MESSAGE_TYPE.INCOMING;
|
|
|
|
const isTemporaryMessage = status === 'in_progress';
|
|
|
|
|
|
|
|
if (!isMessageIncoming || isTemporaryMessage) {
|
|
|
|
Vue.set(messagesInbox, id, message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [messageInConversation] = findUndeliveredMessage(
|
|
|
|
messagesInbox,
|
|
|
|
message
|
|
|
|
);
|
|
|
|
if (!messageInConversation) {
|
|
|
|
Vue.set(messagesInbox, id, message);
|
|
|
|
} else {
|
|
|
|
Vue.delete(messagesInbox, messageInConversation.id);
|
|
|
|
Vue.set(messagesInbox, id, message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateAttachmentMessageStatus($state, { message, tempId }) {
|
|
|
|
const { id } = message;
|
|
|
|
const messagesInbox = $state.conversations;
|
|
|
|
|
|
|
|
const messageInConversation = messagesInbox[tempId];
|
|
|
|
|
|
|
|
if (messageInConversation) {
|
|
|
|
Vue.delete(messagesInbox, tempId);
|
|
|
|
Vue.set(messagesInbox, id, { ...message });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-02-15 18:44:13 +00:00
|
|
|
setConversationUIFlag($state, uiFlags) {
|
|
|
|
$state.uiFlags = {
|
|
|
|
...$state.uiFlags,
|
|
|
|
...uiFlags,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2021-01-17 17:14:03 +00:00
|
|
|
setConversationListLoading($state, status) {
|
|
|
|
$state.uiFlags.isFetchingList = status;
|
|
|
|
},
|
|
|
|
|
|
|
|
setMessagesInConversation($state, payload) {
|
|
|
|
if (!payload.length) {
|
|
|
|
$state.uiFlags.allMessagesLoaded = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.map(message => Vue.set($state.conversations, message.id, message));
|
|
|
|
},
|
|
|
|
|
|
|
|
updateMessage($state, { id, content_attributes }) {
|
|
|
|
$state.conversations[id] = {
|
|
|
|
...$state.conversations[id],
|
|
|
|
content_attributes: {
|
|
|
|
...($state.conversations[id].content_attributes || {}),
|
|
|
|
...content_attributes,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2021-12-21 06:32:43 +00:00
|
|
|
updateMessageMeta($state, { id, meta }) {
|
|
|
|
const message = $state.conversations[id];
|
|
|
|
if (!message) return;
|
|
|
|
|
|
|
|
const newMeta = message.meta ? { ...message.meta, ...meta } : { ...meta };
|
|
|
|
Vue.set(message, 'meta', {
|
|
|
|
...newMeta,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-07-15 08:57:37 +00:00
|
|
|
deleteMessage($state, id) {
|
|
|
|
const messagesInbox = $state.conversations;
|
|
|
|
Vue.delete(messagesInbox, id);
|
|
|
|
},
|
|
|
|
|
2021-01-17 17:14:03 +00:00
|
|
|
toggleAgentTypingStatus($state, { status }) {
|
|
|
|
const isTyping = status === 'on';
|
|
|
|
$state.uiFlags.isAgentTyping = isTyping;
|
|
|
|
},
|
|
|
|
|
|
|
|
setMetaUserLastSeenAt($state, lastSeen) {
|
|
|
|
$state.meta.userLastSeenAt = lastSeen;
|
|
|
|
},
|
|
|
|
};
|