2020-01-13 05:47:03 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
import * as types from '../mutation-types';
|
|
|
|
import ContactAPI from '../../api/contacts';
|
2021-04-16 15:01:07 +00:00
|
|
|
import ConversationApi from '../../api/conversations';
|
2020-01-13 05:47:03 +00:00
|
|
|
|
|
|
|
const state = {
|
|
|
|
records: {},
|
|
|
|
uiFlags: {
|
|
|
|
isFetching: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
getUIFlags($state) {
|
|
|
|
return $state.uiFlags;
|
|
|
|
},
|
|
|
|
getContactConversation: $state => id => {
|
|
|
|
return $state.records[Number(id)] || [];
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
2021-04-16 15:01:07 +00:00
|
|
|
create: async ({ commit }, params) => {
|
|
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
|
|
|
isCreating: true,
|
|
|
|
});
|
|
|
|
const { inboxId, message, contactId, sourceId } = params;
|
|
|
|
try {
|
|
|
|
const { data } = await ConversationApi.create({
|
|
|
|
inbox_id: inboxId,
|
|
|
|
contact_id: contactId,
|
|
|
|
source_id: sourceId,
|
|
|
|
message,
|
|
|
|
});
|
|
|
|
commit(types.default.ADD_CONTACT_CONVERSATION, {
|
|
|
|
id: contactId,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
} finally {
|
|
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
|
|
|
isCreating: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2020-01-13 05:47:03 +00:00
|
|
|
get: async ({ commit }, contactId) => {
|
|
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
|
|
|
isFetching: true,
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
const response = await ContactAPI.getConversations(contactId);
|
|
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS, {
|
|
|
|
id: contactId,
|
|
|
|
data: response.data.payload,
|
|
|
|
});
|
|
|
|
commit(types.default.SET_ALL_CONVERSATION, response.data.payload, {
|
|
|
|
root: true,
|
|
|
|
});
|
|
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
|
|
|
isFetching: false,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, {
|
|
|
|
isFetching: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
[types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG]($state, data) {
|
|
|
|
$state.uiFlags = {
|
|
|
|
...$state.uiFlags,
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
[types.default.SET_CONTACT_CONVERSATIONS]: ($state, { id, data }) => {
|
|
|
|
Vue.set($state.records, id, data);
|
|
|
|
},
|
2021-04-16 15:01:07 +00:00
|
|
|
[types.default.ADD_CONTACT_CONVERSATION]: ($state, { id, data }) => {
|
|
|
|
const conversations = $state.records[id] || [];
|
|
|
|
Vue.set($state.records, id, [...conversations, data]);
|
|
|
|
},
|
2021-09-23 07:22:49 +00:00
|
|
|
[types.default.DELETE_CONTACT_CONVERSATION]: ($state, id) => {
|
|
|
|
Vue.delete($state.records, id);
|
|
|
|
},
|
2020-01-13 05:47:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|