Chore: Remove selectedChat from store (#1087)
* Chore: Remove selected chat from store
This commit is contained in:
@@ -30,7 +30,7 @@
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<!-- No conversation selected -->
|
<!-- No conversation selected -->
|
||||||
<div v-else-if="allConversations.length && currentChat.id === null">
|
<div v-else-if="allConversations.length && !currentChat.id">
|
||||||
<img src="~dashboard/assets/images/chat.svg" alt="No Chat" />
|
<img src="~dashboard/assets/images/chat.svg" alt="No Chat" />
|
||||||
<span>{{ $t('CONVERSATION.404') }}</span>
|
<span>{{ $t('CONVERSATION.404') }}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -84,35 +84,21 @@ const actions = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
setActiveChat(store, data) {
|
async setActiveChat({ commit, dispatch }, data) {
|
||||||
const { commit } = store;
|
commit(types.default.SET_CURRENT_CHAT_WINDOW, data);
|
||||||
const localDispatch = store.dispatch;
|
|
||||||
let donePromise = null;
|
|
||||||
|
|
||||||
commit(types.default.CURRENT_CHAT_WINDOW, data);
|
|
||||||
commit(types.default.CLEAR_ALL_MESSAGES_LOADED);
|
commit(types.default.CLEAR_ALL_MESSAGES_LOADED);
|
||||||
|
|
||||||
if (data.dataFetched === undefined) {
|
if (data.dataFetched === undefined) {
|
||||||
donePromise = new Promise(resolve => {
|
try {
|
||||||
localDispatch('fetchPreviousMessages', {
|
await dispatch('fetchPreviousMessages', {
|
||||||
conversationId: data.id,
|
conversationId: data.id,
|
||||||
before: data.messages[0].id,
|
before: data.messages[0].id,
|
||||||
})
|
});
|
||||||
.then(() => {
|
|
||||||
Vue.set(data, 'dataFetched', true);
|
Vue.set(data, 'dataFetched', true);
|
||||||
resolve();
|
} catch (error) {
|
||||||
})
|
// Ignore error
|
||||||
.catch(() => {
|
}
|
||||||
// Handle error
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
donePromise = new Promise(resolve => {
|
|
||||||
commit(types.default.SET_CHAT_META, { id: data.id });
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return donePromise;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
assignAgent: async ({ commit }, { conversationId, agentId }) => {
|
assignAgent: async ({ commit }, { conversationId, agentId }) => {
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ import authAPI from '../../../api/auth';
|
|||||||
|
|
||||||
export const getSelectedChatConversation = ({
|
export const getSelectedChatConversation = ({
|
||||||
allConversations,
|
allConversations,
|
||||||
selectedChat,
|
selectedChatId,
|
||||||
}) =>
|
}) =>
|
||||||
allConversations.filter(conversation => conversation.id === selectedChat.id);
|
allConversations.filter(conversation => conversation.id === selectedChatId);
|
||||||
|
|
||||||
// getters
|
// getters
|
||||||
const getters = {
|
const getters = {
|
||||||
@@ -12,7 +12,12 @@ const getters = {
|
|||||||
allConversations.sort(
|
allConversations.sort(
|
||||||
(a, b) => b.messages.last()?.created_at - a.messages.last()?.created_at
|
(a, b) => b.messages.last()?.created_at - a.messages.last()?.created_at
|
||||||
),
|
),
|
||||||
getSelectedChat: ({ selectedChat }) => selectedChat,
|
getSelectedChat: ({ selectedChatId, allConversations }) => {
|
||||||
|
const selectedChat = allConversations.find(
|
||||||
|
conversation => conversation.id === selectedChatId
|
||||||
|
);
|
||||||
|
return selectedChat || {};
|
||||||
|
},
|
||||||
getMineChats(_state) {
|
getMineChats(_state) {
|
||||||
const currentUserID = authAPI.getCurrentUser().id;
|
const currentUserID = authAPI.getCurrentUser().id;
|
||||||
return _state.allConversations.filter(chat =>
|
return _state.allConversations.filter(chat =>
|
||||||
@@ -53,7 +58,7 @@ const getters = {
|
|||||||
getChatStatusFilter: ({ chatStatusFilter }) => chatStatusFilter,
|
getChatStatusFilter: ({ chatStatusFilter }) => chatStatusFilter,
|
||||||
getSelectedInbox: ({ currentInbox }) => currentInbox,
|
getSelectedInbox: ({ currentInbox }) => currentInbox,
|
||||||
getNextChatConversation: _state => {
|
getNextChatConversation: _state => {
|
||||||
const { selectedChat } = _state;
|
const [selectedChat] = getSelectedChatConversation(_state);
|
||||||
const conversations = getters.getAllStatusChats(_state);
|
const conversations = getters.getAllStatusChats(_state);
|
||||||
if (conversations.length <= 1) {
|
if (conversations.length <= 1) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -6,28 +6,16 @@ import getters, { getSelectedChatConversation } from './getters';
|
|||||||
import actions from './actions';
|
import actions from './actions';
|
||||||
import wootConstants from '../../../constants';
|
import wootConstants from '../../../constants';
|
||||||
|
|
||||||
const initialSelectedChat = {
|
|
||||||
id: null,
|
|
||||||
meta: {},
|
|
||||||
status: null,
|
|
||||||
muted: false,
|
|
||||||
seen: false,
|
|
||||||
inbox_id: null,
|
|
||||||
additional_attributes: {
|
|
||||||
type: '',
|
|
||||||
},
|
|
||||||
dataFetched: false,
|
|
||||||
};
|
|
||||||
const state = {
|
const state = {
|
||||||
allConversations: [],
|
allConversations: [],
|
||||||
selectedChat: { ...initialSelectedChat },
|
|
||||||
listLoadingStatus: true,
|
listLoadingStatus: true,
|
||||||
chatStatusFilter: wootConstants.STATUS_TYPE.OPEN,
|
chatStatusFilter: wootConstants.STATUS_TYPE.OPEN,
|
||||||
currentInbox: null,
|
currentInbox: null,
|
||||||
|
selectedChatId: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
// mutations
|
// mutations
|
||||||
const mutations = {
|
export const mutations = {
|
||||||
[types.default.SET_ALL_CONVERSATION](_state, conversationList) {
|
[types.default.SET_ALL_CONVERSATION](_state, conversationList) {
|
||||||
const newAllConversations = [..._state.allConversations];
|
const newAllConversations = [..._state.allConversations];
|
||||||
conversationList.forEach(conversation => {
|
conversationList.forEach(conversation => {
|
||||||
@@ -42,7 +30,7 @@ const mutations = {
|
|||||||
},
|
},
|
||||||
[types.default.EMPTY_ALL_CONVERSATION](_state) {
|
[types.default.EMPTY_ALL_CONVERSATION](_state) {
|
||||||
_state.allConversations = [];
|
_state.allConversations = [];
|
||||||
_state.selectedChat = { ...initialSelectedChat };
|
_state.selectedChatId = null;
|
||||||
},
|
},
|
||||||
[types.default.SET_ALL_MESSAGES_LOADED](_state) {
|
[types.default.SET_ALL_MESSAGES_LOADED](_state) {
|
||||||
const [chat] = getSelectedChatConversation(_state);
|
const [chat] = getSelectedChatConversation(_state);
|
||||||
@@ -54,7 +42,7 @@ const mutations = {
|
|||||||
Vue.set(chat, 'allMessagesLoaded', false);
|
Vue.set(chat, 'allMessagesLoaded', false);
|
||||||
},
|
},
|
||||||
[types.default.CLEAR_CURRENT_CHAT_WINDOW](_state) {
|
[types.default.CLEAR_CURRENT_CHAT_WINDOW](_state) {
|
||||||
_state.selectedChat.id = null;
|
_state.selectedChatId = null;
|
||||||
},
|
},
|
||||||
|
|
||||||
[types.default.SET_PREVIOUS_CONVERSATIONS](_state, { id, data }) {
|
[types.default.SET_PREVIOUS_CONVERSATIONS](_state, { id, data }) {
|
||||||
@@ -64,47 +52,25 @@ const mutations = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
[types.default.CURRENT_CHAT_WINDOW](_state, activeChat) {
|
[types.default.SET_CURRENT_CHAT_WINDOW](_state, activeChat) {
|
||||||
if (activeChat) {
|
if (activeChat) {
|
||||||
Object.assign(_state.selectedChat, activeChat);
|
_state.selectedChatId = activeChat.id;
|
||||||
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) {
|
[types.default.ASSIGN_AGENT](_state, assignee) {
|
||||||
const [chat] = getSelectedChatConversation(_state);
|
const [chat] = getSelectedChatConversation(_state);
|
||||||
chat.meta.assignee = assignee;
|
chat.meta.assignee = assignee;
|
||||||
if (assignee === null) {
|
|
||||||
Object.assign(_state.selectedChat.meta.assignee, assignee);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
[types.default.RESOLVE_CONVERSATION](_state, status) {
|
[types.default.RESOLVE_CONVERSATION](_state, status) {
|
||||||
const [chat] = getSelectedChatConversation(_state);
|
const [chat] = getSelectedChatConversation(_state);
|
||||||
chat.status = status;
|
chat.status = status;
|
||||||
_state.selectedChat.status = status;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
[types.default.MUTE_CONVERSATION](_state) {
|
[types.default.MUTE_CONVERSATION](_state) {
|
||||||
const [chat] = getSelectedChatConversation(_state);
|
const [chat] = getSelectedChatConversation(_state);
|
||||||
chat.muted = true;
|
chat.muted = true;
|
||||||
_state.selectedChat.muted = true;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
[types.default.SEND_MESSAGE](_state, currentMessage) {
|
[types.default.SEND_MESSAGE](_state, currentMessage) {
|
||||||
@@ -126,7 +92,7 @@ const mutations = {
|
|||||||
);
|
);
|
||||||
if (previousMessageIndex === -1) {
|
if (previousMessageIndex === -1) {
|
||||||
chat.messages.push(message);
|
chat.messages.push(message);
|
||||||
if (_state.selectedChat.id === message.conversation_id) {
|
if (_state.selectedChatId === message.conversation_id) {
|
||||||
window.bus.$emit('scrollToMessage');
|
window.bus.$emit('scrollToMessage');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -150,8 +116,7 @@ const mutations = {
|
|||||||
...conversationAttributes,
|
...conversationAttributes,
|
||||||
};
|
};
|
||||||
Vue.set(allConversations, currentConversationIndex, currentConversation);
|
Vue.set(allConversations, currentConversationIndex, currentConversation);
|
||||||
if (_state.selectedChat.id === conversation.id) {
|
if (_state.selectedChatId === conversation.id) {
|
||||||
_state.selectedChat.status = conversation.status;
|
|
||||||
window.bus.$emit('scrollToMessage');
|
window.bus.$emit('scrollToMessage');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -159,10 +124,6 @@ const mutations = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
[types.default.MARK_SEEN](_state) {
|
|
||||||
_state.selectedChat.seen = true;
|
|
||||||
},
|
|
||||||
|
|
||||||
[types.default.SET_LIST_LOADING_STATUS](_state) {
|
[types.default.SET_LIST_LOADING_STATUS](_state) {
|
||||||
_state.listLoadingStatus = true;
|
_state.listLoadingStatus = true;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -58,9 +58,7 @@ describe('#getters', () => {
|
|||||||
id: 2,
|
id: 2,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
selectedChat: {
|
selectedChatId: 1,
|
||||||
id: 1,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
expect(getters.getNextChatConversation(state)).toEqual({
|
expect(getters.getNextChatConversation(state)).toEqual({
|
||||||
id: 2,
|
id: 2,
|
||||||
@@ -73,9 +71,7 @@ describe('#getters', () => {
|
|||||||
id: 1,
|
id: 1,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
selectedChat: {
|
selectedChatId: 1,
|
||||||
id: 1,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
expect(getters.getNextChatConversation(state)).toBeNull();
|
expect(getters.getNextChatConversation(state)).toBeNull();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import types from '../../../mutation-types';
|
||||||
|
import { mutations } from '../../conversations';
|
||||||
|
|
||||||
|
describe('#mutations', () => {
|
||||||
|
describe('#EMPTY_ALL_CONVERSATION', () => {
|
||||||
|
it('empty conversations', () => {
|
||||||
|
const state = { allConversations: [{ id: 1 }], selectedChatId: 1 };
|
||||||
|
mutations[types.EMPTY_ALL_CONVERSATION](state);
|
||||||
|
expect(state.allConversations).toEqual([]);
|
||||||
|
expect(state.selectedChatId).toEqual(null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#MARK_MESSAGE_READ', () => {
|
||||||
|
it('mark conversation as read', () => {
|
||||||
|
const state = { allConversations: [{ id: 1 }] };
|
||||||
|
const lastSeen = new Date().getTime() / 1000;
|
||||||
|
mutations[types.MARK_MESSAGE_READ](state, { id: 1, lastSeen });
|
||||||
|
expect(state.allConversations).toEqual([
|
||||||
|
{ id: 1, agent_last_seen_at: lastSeen },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#CLEAR_CURRENT_CHAT_WINDOW', () => {
|
||||||
|
it('clears current chat window', () => {
|
||||||
|
const state = { selectedChatId: 1 };
|
||||||
|
mutations[types.CLEAR_CURRENT_CHAT_WINDOW](state);
|
||||||
|
expect(state.selectedChatId).toEqual(null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#SET_CURRENT_CHAT_WINDOW', () => {
|
||||||
|
it('set current chat window', () => {
|
||||||
|
const state = { selectedChatId: 1 };
|
||||||
|
mutations[types.SET_CURRENT_CHAT_WINDOW](state, { id: 2 });
|
||||||
|
expect(state.selectedChatId).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not set current chat window', () => {
|
||||||
|
const state = { selectedChatId: 1 };
|
||||||
|
mutations[types.SET_CURRENT_CHAT_WINDOW](state);
|
||||||
|
expect(state.selectedChatId).toEqual(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -18,9 +18,8 @@ export default {
|
|||||||
UPDATE_CONVERSATION_CONTACT: 'UPDATE_CONVERSATION_CONTACT',
|
UPDATE_CONVERSATION_CONTACT: 'UPDATE_CONVERSATION_CONTACT',
|
||||||
|
|
||||||
// Active chat
|
// Active chat
|
||||||
CURRENT_CHAT_WINDOW: 'CURRENT_CHAT_WINDOW',
|
SET_CURRENT_CHAT_WINDOW: 'SET_CURRENT_CHAT_WINDOW',
|
||||||
CLEAR_CURRENT_CHAT_WINDOW: 'CLEAR_CURRENT_CHAT_WINDOW',
|
CLEAR_CURRENT_CHAT_WINDOW: 'CLEAR_CURRENT_CHAT_WINDOW',
|
||||||
APPEND_MESSAGES: 'APPEND_MESSAGES',
|
|
||||||
CLEAR_ALL_MESSAGES: 'CLEAR_ALL_MESSAGES',
|
CLEAR_ALL_MESSAGES: 'CLEAR_ALL_MESSAGES',
|
||||||
RESOLVE_CONVERSATION: 'RESOLVE_CONVERSATION',
|
RESOLVE_CONVERSATION: 'RESOLVE_CONVERSATION',
|
||||||
ADD_CONVERSATION: 'ADD_CONVERSATION',
|
ADD_CONVERSATION: 'ADD_CONVERSATION',
|
||||||
@@ -30,7 +29,6 @@ export default {
|
|||||||
ASSIGN_AGENT: 'ASSIGN_AGENT',
|
ASSIGN_AGENT: 'ASSIGN_AGENT',
|
||||||
SET_CHAT_META: 'SET_CHAT_META',
|
SET_CHAT_META: 'SET_CHAT_META',
|
||||||
ADD_MESSAGE: 'ADD_MESSAGE',
|
ADD_MESSAGE: 'ADD_MESSAGE',
|
||||||
MARK_SEEN: 'MARK_SEEN',
|
|
||||||
MARK_MESSAGE_READ: 'MARK_MESSAGE_READ',
|
MARK_MESSAGE_READ: 'MARK_MESSAGE_READ',
|
||||||
SET_PREVIOUS_CONVERSATIONS: 'SET_PREVIOUS_CONVERSATIONS',
|
SET_PREVIOUS_CONVERSATIONS: 'SET_PREVIOUS_CONVERSATIONS',
|
||||||
SET_ACTIVE_INBOX: 'SET_ACTIVE_INBOX',
|
SET_ACTIVE_INBOX: 'SET_ACTIVE_INBOX',
|
||||||
|
|||||||
Reference in New Issue
Block a user