2019-09-05 07:31:36 +00:00
|
|
|
/* eslint no-console: 0 */
|
|
|
|
/* eslint no-param-reassign: 0 */
|
|
|
|
import Vue from 'vue';
|
|
|
|
import * as types from '../../mutation-types';
|
|
|
|
import getters, { getSelectedChatConversation } from './getters';
|
|
|
|
import actions from './actions';
|
2020-02-26 15:45:01 +00:00
|
|
|
import wootConstants from '../../../constants';
|
2019-09-05 07:31:36 +00:00
|
|
|
|
2020-05-04 17:37:56 +00:00
|
|
|
const initialSelectedChat = {
|
|
|
|
id: null,
|
|
|
|
meta: {},
|
|
|
|
status: null,
|
2020-05-26 12:13:59 +00:00
|
|
|
muted: false,
|
2020-05-04 17:37:56 +00:00
|
|
|
seen: false,
|
|
|
|
agentTyping: 'off',
|
|
|
|
dataFetched: false,
|
|
|
|
};
|
2019-09-05 07:31:36 +00:00
|
|
|
const state = {
|
|
|
|
allConversations: [],
|
|
|
|
convTabStats: {
|
|
|
|
mineCount: 0,
|
|
|
|
unAssignedCount: 0,
|
|
|
|
allCount: 0,
|
|
|
|
},
|
2020-05-04 17:37:56 +00:00
|
|
|
selectedChat: { ...initialSelectedChat },
|
2019-09-05 07:31:36 +00:00
|
|
|
listLoadingStatus: true,
|
2020-02-26 15:45:01 +00:00
|
|
|
chatStatusFilter: wootConstants.STATUS_TYPE.OPEN,
|
2019-09-05 07:31:36 +00:00
|
|
|
currentInbox: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
// mutations
|
|
|
|
const mutations = {
|
2019-12-01 04:46:51 +00:00
|
|
|
[types.default.SET_ALL_CONVERSATION](_state, conversationList) {
|
|
|
|
const newAllConversations = [..._state.allConversations];
|
|
|
|
conversationList.forEach(conversation => {
|
|
|
|
const indexInCurrentList = newAllConversations.findIndex(
|
|
|
|
c => c.id === conversation.id
|
|
|
|
);
|
|
|
|
if (indexInCurrentList < 0) {
|
|
|
|
newAllConversations.push(conversation);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_state.allConversations = newAllConversations;
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
[types.default.EMPTY_ALL_CONVERSATION](_state) {
|
|
|
|
_state.allConversations = [];
|
2020-05-04 17:37:56 +00:00
|
|
|
_state.selectedChat = { ...initialSelectedChat };
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
[types.default.SET_ALL_MESSAGES_LOADED](_state) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
|
|
|
Vue.set(chat, 'allMessagesLoaded', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.CLEAR_ALL_MESSAGES_LOADED](_state) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
|
|
|
Vue.set(chat, 'allMessagesLoaded', false);
|
|
|
|
},
|
|
|
|
[types.default.CLEAR_CURRENT_CHAT_WINDOW](_state) {
|
|
|
|
_state.selectedChat.id = null;
|
|
|
|
_state.selectedChat.agentTyping = 'off';
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.SET_PREVIOUS_CONVERSATIONS](_state, { id, data }) {
|
|
|
|
if (data.length) {
|
|
|
|
const [chat] = _state.allConversations.filter(c => c.id === id);
|
|
|
|
chat.messages.unshift(...data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
[types.default.SET_CONV_TAB_META](
|
|
|
|
_state,
|
|
|
|
{
|
2019-12-01 04:46:51 +00:00
|
|
|
mine_count: mineCount,
|
|
|
|
unassigned_count: unAssignedCount,
|
2019-10-27 13:31:59 +00:00
|
|
|
all_count: allCount,
|
|
|
|
} = {}
|
|
|
|
) {
|
2019-12-01 04:46:51 +00:00
|
|
|
Vue.set(_state.convTabStats, 'mineCount', mineCount);
|
|
|
|
Vue.set(_state.convTabStats, 'allCount', allCount);
|
|
|
|
Vue.set(_state.convTabStats, 'unAssignedCount', unAssignedCount);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.CURRENT_CHAT_WINDOW](_state, activeChat) {
|
|
|
|
if (activeChat) {
|
|
|
|
Object.assign(_state.selectedChat, activeChat);
|
|
|
|
Vue.set(_state.selectedChat.meta, 'assignee', activeChat.meta.assignee);
|
|
|
|
Vue.set(_state.selectedChat.meta, 'status', activeChat.meta.status);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.APPEND_MESSAGES](_state, { id, data }) {
|
|
|
|
if (data.length) {
|
|
|
|
const [chat] = _state.allConversations.filter(c => c.id === id);
|
|
|
|
chat.messages = data;
|
|
|
|
Vue.set(chat, 'dataFetched', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.SET_CHAT_META](_state, { id, data }) {
|
|
|
|
const [chat] = _state.allConversations.filter(c => c.id === id);
|
|
|
|
if (data !== undefined) {
|
|
|
|
Vue.set(chat, 'labels', data.labels);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.ASSIGN_AGENT](_state, assignee) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
|
|
|
chat.meta.assignee = assignee;
|
|
|
|
if (assignee === null) {
|
|
|
|
Object.assign(_state.selectedChat.meta.assignee, assignee);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.RESOLVE_CONVERSATION](_state, status) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
|
|
|
chat.status = status;
|
|
|
|
_state.selectedChat.status = status;
|
|
|
|
},
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
[types.default.MUTE_CONVERSATION](_state) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
|
|
|
chat.muted = true;
|
|
|
|
_state.selectedChat.muted = true;
|
|
|
|
},
|
|
|
|
|
2020-04-17 15:45:20 +00:00
|
|
|
[types.default.SEND_MESSAGE](_state, currentMessage) {
|
2019-09-05 07:31:36 +00:00
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
2020-04-17 15:45:20 +00:00
|
|
|
const allMessagesExceptCurrent = (chat.messages || []).filter(
|
|
|
|
message => message.id !== currentMessage.id
|
|
|
|
);
|
|
|
|
allMessagesExceptCurrent.push(currentMessage);
|
|
|
|
chat.messages = allMessagesExceptCurrent;
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.ADD_MESSAGE](_state, message) {
|
|
|
|
const [chat] = _state.allConversations.filter(
|
|
|
|
c => c.id === message.conversation_id
|
|
|
|
);
|
|
|
|
if (!chat) return;
|
2020-04-17 15:45:20 +00:00
|
|
|
const previousMessageIndex = chat.messages.findIndex(
|
|
|
|
m => m.id === message.id
|
|
|
|
);
|
|
|
|
if (previousMessageIndex === -1) {
|
2019-09-05 07:31:36 +00:00
|
|
|
chat.messages.push(message);
|
|
|
|
if (_state.selectedChat.id === message.conversation_id) {
|
|
|
|
window.bus.$emit('scrollToMessage');
|
|
|
|
}
|
2020-04-17 15:45:20 +00:00
|
|
|
} else {
|
|
|
|
chat.messages[previousMessageIndex] = message;
|
2019-09-05 07:31:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.ADD_CONVERSATION](_state, conversation) {
|
|
|
|
_state.allConversations.push(conversation);
|
|
|
|
},
|
|
|
|
|
2020-04-18 14:55:58 +00:00
|
|
|
[types.default.UPDATE_CONVERSATION](_state, conversation) {
|
|
|
|
const { allConversations } = _state;
|
|
|
|
const currentConversationIndex = allConversations.findIndex(
|
|
|
|
c => c.id === conversation.id
|
|
|
|
);
|
|
|
|
if (currentConversationIndex > -1) {
|
|
|
|
const currentConversation = {
|
|
|
|
...allConversations[currentConversationIndex],
|
|
|
|
status: conversation.status,
|
|
|
|
};
|
|
|
|
Vue.set(allConversations, currentConversationIndex, currentConversation);
|
|
|
|
if (_state.selectedChat.id === conversation.id) {
|
|
|
|
_state.selectedChat.status = conversation.status;
|
|
|
|
window.bus.$emit('scrollToMessage');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
[types.default.MARK_SEEN](_state) {
|
|
|
|
_state.selectedChat.seen = true;
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2020-05-04 17:37:56 +00:00
|
|
|
[types.default.SET_AGENT_TYPING](_state, { status }) {
|
2019-10-27 13:31:59 +00:00
|
|
|
_state.selectedChat.agentTyping = status;
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.SET_LIST_LOADING_STATUS](_state) {
|
|
|
|
_state.listLoadingStatus = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.CLEAR_LIST_LOADING_STATUS](_state) {
|
|
|
|
_state.listLoadingStatus = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.MARK_MESSAGE_READ](_state, { id, lastSeen }) {
|
|
|
|
const [chat] = _state.allConversations.filter(c => c.id === id);
|
|
|
|
chat.agent_last_seen_at = lastSeen;
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.CHANGE_CHAT_STATUS_FILTER](_state, data) {
|
|
|
|
_state.chatStatusFilter = data;
|
|
|
|
},
|
|
|
|
|
2019-10-24 20:07:01 +00:00
|
|
|
// Update assignee on action cable message
|
2019-09-05 07:31:36 +00:00
|
|
|
[types.default.UPDATE_ASSIGNEE](_state, payload) {
|
|
|
|
const [chat] = _state.allConversations.filter(c => c.id === payload.id);
|
|
|
|
chat.meta.assignee = payload.assignee;
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.SET_ACTIVE_INBOX](_state, inboxId) {
|
|
|
|
_state.currentInbox = inboxId;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|