Feature: View a contact's previous conversation (#422)

* Add API to fetch conversations of a contact

* Add conversation list in sidebar
This commit is contained in:
Pranav Raj S
2020-01-13 11:17:03 +05:30
committed by Sojan Jose
parent fc6a8c2601
commit 655c585358
19 changed files with 491 additions and 5 deletions

View File

@@ -0,0 +1,64 @@
import Vue from 'vue';
import * as types from '../mutation-types';
import ContactAPI from '../../api/contacts';
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 = {
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);
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};

View File

@@ -0,0 +1,41 @@
import axios from 'axios';
import { actions } from '../../contactConversations';
import * as types from '../../../mutation-types';
import conversationList from './fixtures';
const commit = jest.fn();
global.axios = axios;
jest.mock('axios');
describe('#actions', () => {
describe('#get', () => {
it('sends correct actions if API is success', async () => {
axios.get.mockResolvedValue({ data: { payload: conversationList } });
await actions.get({ commit }, 1);
expect(commit.mock.calls).toEqual([
[types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, { isFetching: true }],
[
types.default.SET_CONTACT_CONVERSATIONS,
{ id: 1, data: conversationList },
],
[types.default.SET_ALL_CONVERSATION, conversationList, { root: true }],
[
types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG,
{ isFetching: false },
],
]);
});
it('sends correct actions if API is error', async () => {
axios.get.mockRejectedValue({ message: 'Incorrect header' });
await actions.get({ commit });
expect(commit.mock.calls).toEqual([
[types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG, { isFetching: true }],
[
types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG,
{ isFetching: false },
],
]);
});
});
});

View File

@@ -0,0 +1,82 @@
export default [
{
meta: {
sender: {
id: 1,
name: 'sender1',
thumbnail: '',
channel: 'Channel::WebWidget',
},
assignee: null,
},
id: 1,
messages: [
{
id: 1,
content: 'Hello',
account_id: 1,
inbox_id: 1,
conversation_id: 1,
message_type: 1,
created_at: 1578555084,
updated_at: '2020-01-09T07:31:24.419Z',
private: false,
user_id: 1,
status: 'sent',
fb_id: null,
content_type: 'text',
content_attributes: {},
sender: {
name: 'Sender 1',
avatar_url: 'random_url',
},
},
],
inbox_id: 1,
status: 0,
timestamp: 1578555084,
user_last_seen_at: 0,
agent_last_seen_at: 1578555084,
unread_count: 0,
},
{
meta: {
sender: {
id: 2,
name: 'sender1',
thumbnail: '',
channel: 'Channel::WebWidget',
},
assignee: null,
},
id: 2,
messages: [
{
id: 2,
content: 'Hello',
account_id: 1,
inbox_id: 2,
conversation_id: 2,
message_type: 1,
created_at: 1578555084,
updated_at: '2020-01-09T07:31:24.419Z',
private: false,
user_id: 2,
status: 'sent',
fb_id: null,
content_type: 'text',
content_attributes: {},
sender: {
name: 'Sender 1',
avatar_url: 'random_url',
},
},
],
inbox_id: 2,
status: 0,
timestamp: 1578555084,
user_last_seen_at: 0,
agent_last_seen_at: 1578555084,
unread_count: 0,
},
];

View File

@@ -0,0 +1,23 @@
import { getters } from '../../contactConversations';
describe('#getters', () => {
it('getContactConversation', () => {
const state = {
records: { 1: [{ id: 1, contact_id: 1, message: 'Hello' }] },
};
expect(getters.getContactConversation(state)(1)).toEqual([
{ id: 1, contact_id: 1, message: 'Hello' },
]);
});
it('getUIFlags', () => {
const state = {
uiFlags: {
isFetching: true,
},
};
expect(getters.getUIFlags(state)).toEqual({
isFetching: true,
});
});
});

View File

@@ -0,0 +1,29 @@
import * as types from '../../../mutation-types';
import { mutations } from '../../contactConversations';
describe('#mutations', () => {
describe('#SET_CONTACT_CONVERSATIONS_UI_FLAG', () => {
it('set ui flags', () => {
const state = { uiFlags: { isFetching: true } };
mutations[types.default.SET_CONTACT_CONVERSATIONS_UI_FLAG](state, {
isFetching: false,
});
expect(state.uiFlags).toEqual({
isFetching: false,
});
});
});
describe('#SET_CONTACT_CONVERSATIONS', () => {
it('set contact conversation records', () => {
const state = { records: {} };
mutations[types.default.SET_CONTACT_CONVERSATIONS](state, {
id: 1,
data: [{ id: 1, contact_id: 1, message: 'hello' }],
});
expect(state.records).toEqual({
1: [{ id: 1, contact_id: 1, message: 'hello' }],
});
});
});
});