2019-10-29 07:20:54 +00:00
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
import Vue from 'vue';
|
|
|
|
import { sendMessageAPI, getConversationAPI } from 'widget/api/conversation';
|
2019-11-29 12:12:35 +00:00
|
|
|
import { MESSAGE_TYPE } from 'widget/helpers/constants';
|
|
|
|
import getUuid from '../../helpers/uuid';
|
2019-12-14 18:36:01 +00:00
|
|
|
import DateHelper from '../../../shared/helpers/DateHelper';
|
|
|
|
|
|
|
|
const groupBy = require('lodash.groupby');
|
2019-11-29 12:12:35 +00:00
|
|
|
|
|
|
|
export const createTemporaryMessage = content => {
|
|
|
|
const timestamp = new Date().getTime();
|
|
|
|
return {
|
|
|
|
id: getUuid(),
|
|
|
|
content,
|
|
|
|
status: 'in_progress',
|
|
|
|
created_at: timestamp,
|
|
|
|
message_type: MESSAGE_TYPE.INCOMING,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const findUndeliveredMessage = (messageInbox, { content }) =>
|
|
|
|
Object.values(messageInbox).filter(
|
|
|
|
message => message.content === content && message.status === 'in_progress'
|
|
|
|
);
|
2019-10-29 07:20:54 +00:00
|
|
|
|
|
|
|
export const DEFAULT_CONVERSATION = 'default';
|
2019-12-11 15:27:06 +00:00
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
const state = {
|
|
|
|
conversations: {},
|
2019-12-11 15:27:06 +00:00
|
|
|
uiFlags: {
|
|
|
|
allMessagesLoaded: false,
|
|
|
|
isFetchingList: false,
|
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
export const getters = {
|
2019-12-14 18:36:01 +00:00
|
|
|
getAllMessagesLoaded: _state => _state.uiFlags.allMessagesLoaded,
|
2019-10-29 07:20:54 +00:00
|
|
|
getConversation: _state => _state.conversations,
|
2019-11-26 17:05:26 +00:00
|
|
|
getConversationSize: _state => Object.keys(_state.conversations).length,
|
2019-12-11 15:27:06 +00:00
|
|
|
getEarliestMessage: _state => {
|
|
|
|
const conversation = Object.values(_state.conversations);
|
|
|
|
if (conversation.length) {
|
|
|
|
return conversation[0];
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
},
|
2019-12-14 18:36:01 +00:00
|
|
|
getGroupedConversation: _state => {
|
|
|
|
return groupBy(Object.values(_state.conversations), message =>
|
|
|
|
new DateHelper(message.created_at).format()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
getIsFetchingList: _state => _state.uiFlags.isFetchingList,
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
export const actions = {
|
2019-11-29 12:12:35 +00:00
|
|
|
sendMessage: async ({ commit }, params) => {
|
2019-10-29 07:20:54 +00:00
|
|
|
const { content } = params;
|
2019-12-11 15:27:06 +00:00
|
|
|
commit('pushMessageToConversation', createTemporaryMessage(content));
|
2019-10-29 07:20:54 +00:00
|
|
|
await sendMessageAPI(content);
|
|
|
|
},
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
fetchOldConversations: async ({ commit }, { before } = {}) => {
|
2019-10-29 07:20:54 +00:00
|
|
|
try {
|
2019-12-11 15:27:06 +00:00
|
|
|
commit('setConversationListLoading', true);
|
|
|
|
const { data } = await getConversationAPI({ before });
|
|
|
|
commit('setMessagesInConversation', data);
|
|
|
|
commit('setConversationListLoading', false);
|
2019-10-29 07:20:54 +00:00
|
|
|
} catch (error) {
|
2019-12-11 15:27:06 +00:00
|
|
|
commit('setConversationListLoading', false);
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addMessage({ commit }, data) {
|
2019-12-11 15:27:06 +00:00
|
|
|
commit('pushMessageToConversation', data);
|
2019-10-29 07:20:54 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
export const mutations = {
|
|
|
|
pushMessageToConversation($state, message) {
|
2019-11-29 12:12:35 +00:00
|
|
|
const { id, status, message_type: type } = message;
|
2019-10-29 07:20:54 +00:00
|
|
|
const messagesInbox = $state.conversations;
|
2019-11-29 12:12:35 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-10-29 07:20:54 +00:00
|
|
|
},
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
setConversationListLoading($state, status) {
|
|
|
|
$state.uiFlags.isFetchingList = status;
|
|
|
|
},
|
|
|
|
|
|
|
|
setMessagesInConversation($state, payload) {
|
2019-10-29 07:20:54 +00:00
|
|
|
if (!payload.length) {
|
2019-12-11 15:27:06 +00:00
|
|
|
$state.uiFlags.allMessagesLoaded = true;
|
2019-10-29 07:20:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
payload.map(message => Vue.set($state.conversations, message.id, message));
|
2019-10-29 07:20:54 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|