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-12-25 07:45:01 +00:00
|
|
|
import { findPendingMessageIndex } from './helpers';
|
2020-02-26 15:45:01 +00:00
|
|
|
import wootConstants from '../../../constants';
|
2019-09-05 07:31:36 +00:00
|
|
|
|
|
|
|
const state = {
|
|
|
|
allConversations: [],
|
|
|
|
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,
|
2020-07-23 18:03:17 +00:00
|
|
|
selectedChatId: null,
|
2019-09-05 07:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// mutations
|
2020-07-23 18:03:17 +00:00
|
|
|
export 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-07-23 18:03:17 +00:00
|
|
|
_state.selectedChatId = null;
|
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) {
|
2020-07-23 18:03:17 +00:00
|
|
|
_state.selectedChatId = null;
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.SET_PREVIOUS_CONVERSATIONS](_state, { id, data }) {
|
|
|
|
if (data.length) {
|
|
|
|
const [chat] = _state.allConversations.filter(c => c.id === id);
|
|
|
|
chat.messages.unshift(...data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-07-23 18:03:17 +00:00
|
|
|
[types.default.SET_CURRENT_CHAT_WINDOW](_state, activeChat) {
|
2019-09-05 07:31:36 +00:00
|
|
|
if (activeChat) {
|
2020-07-23 18:03:17 +00:00
|
|
|
_state.selectedChatId = activeChat.id;
|
2019-09-05 07:31:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.ASSIGN_AGENT](_state, assignee) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
2021-03-15 13:07:03 +00:00
|
|
|
Vue.set(chat.meta, 'assignee', assignee);
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.ASSIGN_TEAM](_state, team) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
|
|
|
Vue.set(chat.meta, 'team', team);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2021-09-29 14:03:51 +00:00
|
|
|
[types.default.CHANGE_CONVERSATION_STATUS](
|
|
|
|
_state,
|
|
|
|
{ conversationId, status, snoozedUntil }
|
|
|
|
) {
|
2021-08-02 07:41:07 +00:00
|
|
|
const conversation =
|
|
|
|
getters.getConversationById(_state)(conversationId) || {};
|
2021-09-29 14:03:51 +00:00
|
|
|
Vue.set(conversation, 'snoozed_until', snoozedUntil);
|
2021-08-02 07:41:07 +00:00
|
|
|
Vue.set(conversation, 'status', status);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
[types.default.MUTE_CONVERSATION](_state) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
|
|
|
chat.muted = true;
|
|
|
|
},
|
|
|
|
|
2020-10-08 06:32:08 +00:00
|
|
|
[types.default.UNMUTE_CONVERSATION](_state) {
|
|
|
|
const [chat] = getSelectedChatConversation(_state);
|
|
|
|
chat.muted = false;
|
|
|
|
},
|
|
|
|
|
2020-10-11 14:54:26 +00:00
|
|
|
[types.default.ADD_MESSAGE]({ allConversations, selectedChatId }, message) {
|
|
|
|
const { conversation_id: conversationId } = message;
|
|
|
|
const [chat] = getSelectedChatConversation({
|
|
|
|
allConversations,
|
|
|
|
selectedChatId: conversationId,
|
|
|
|
});
|
2019-09-05 07:31:36 +00:00
|
|
|
if (!chat) return;
|
2020-12-25 07:45:01 +00:00
|
|
|
|
|
|
|
const pendingMessageIndex = findPendingMessageIndex(chat, message);
|
|
|
|
if (pendingMessageIndex !== -1) {
|
|
|
|
Vue.set(chat.messages, pendingMessageIndex, message);
|
|
|
|
} else {
|
2019-09-05 07:31:36 +00:00
|
|
|
chat.messages.push(message);
|
2020-10-05 17:22:43 +00:00
|
|
|
chat.timestamp = message.created_at;
|
2020-10-11 14:54:26 +00:00
|
|
|
if (selectedChatId === conversationId) {
|
2019-09-05 07:31:36 +00:00
|
|
|
window.bus.$emit('scrollToMessage');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[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) {
|
2020-07-04 14:16:17 +00:00
|
|
|
const { messages, ...conversationAttributes } = conversation;
|
2020-04-18 14:55:58 +00:00
|
|
|
const currentConversation = {
|
|
|
|
...allConversations[currentConversationIndex],
|
2020-07-04 14:16:17 +00:00
|
|
|
...conversationAttributes,
|
2020-04-18 14:55:58 +00:00
|
|
|
};
|
|
|
|
Vue.set(allConversations, currentConversationIndex, currentConversation);
|
2020-07-23 18:03:17 +00:00
|
|
|
if (_state.selectedChatId === conversation.id) {
|
2020-04-18 14:55:58 +00:00
|
|
|
window.bus.$emit('scrollToMessage');
|
|
|
|
}
|
2020-06-25 15:34:03 +00:00
|
|
|
} else {
|
|
|
|
_state.allConversations.push(conversation);
|
2020-04-18 14:55:58 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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);
|
2020-08-03 08:10:20 +00:00
|
|
|
if (chat) {
|
|
|
|
chat.agent_last_seen_at = lastSeen;
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[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);
|
2021-03-15 13:07:03 +00:00
|
|
|
Vue.set(chat.meta, 'assignee', payload.assignee);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2020-06-02 17:29:02 +00:00
|
|
|
[types.default.UPDATE_CONVERSATION_CONTACT](
|
|
|
|
_state,
|
|
|
|
{ conversationId, ...payload }
|
|
|
|
) {
|
|
|
|
const [chat] = _state.allConversations.filter(c => c.id === conversationId);
|
|
|
|
if (chat) {
|
|
|
|
Vue.set(chat.meta, 'sender', payload);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-09-05 07:31:36 +00:00
|
|
|
[types.default.SET_ACTIVE_INBOX](_state, inboxId) {
|
2020-06-25 15:34:03 +00:00
|
|
|
_state.currentInbox = inboxId ? parseInt(inboxId, 10) : null;
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
2020-07-25 17:24:45 +00:00
|
|
|
|
|
|
|
[types.default.SET_CONVERSATION_CAN_REPLY](
|
|
|
|
_state,
|
|
|
|
{ conversationId, canReply }
|
|
|
|
) {
|
|
|
|
const [chat] = _state.allConversations.filter(c => c.id === conversationId);
|
|
|
|
if (chat) {
|
|
|
|
Vue.set(chat, 'can_reply', canReply);
|
|
|
|
}
|
|
|
|
},
|
2021-09-23 07:22:49 +00:00
|
|
|
|
|
|
|
[types.default.CLEAR_CONTACT_CONVERSATIONS](_state, contactId) {
|
|
|
|
const chats = _state.allConversations.filter(
|
|
|
|
c => c.meta.sender.id !== contactId
|
|
|
|
);
|
|
|
|
Vue.set(_state, 'allConversations', chats);
|
|
|
|
},
|
2019-09-05 07:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|