[Enhancement] Group widget messages by date (#363)

* [Enhancement] Group widget messages by date

* Update DateSeparator snapshot
This commit is contained in:
Pranav Raj S
2019-12-15 00:06:01 +05:30
committed by Sojan Jose
parent 7b63cbe1f7
commit cfc56705fd
11 changed files with 193 additions and 13 deletions

View File

@@ -3,6 +3,9 @@ import Vue from 'vue';
import { sendMessageAPI, getConversationAPI } from 'widget/api/conversation';
import { MESSAGE_TYPE } from 'widget/helpers/constants';
import getUuid from '../../helpers/uuid';
import DateHelper from '../../../shared/helpers/DateHelper';
const groupBy = require('lodash.groupby');
export const createTemporaryMessage = content => {
const timestamp = new Date().getTime();
@@ -31,10 +34,9 @@ const state = {
};
export const getters = {
getAllMessagesLoaded: _state => _state.uiFlags.allMessagesLoaded,
getConversation: _state => _state.conversations,
getConversationSize: _state => Object.keys(_state.conversations).length,
getAllMessagesLoaded: _state => _state.uiFlags.allMessagesLoaded,
getIsFetchingList: _state => _state.uiFlags.isFetchingList,
getEarliestMessage: _state => {
const conversation = Object.values(_state.conversations);
if (conversation.length) {
@@ -42,6 +44,12 @@ export const getters = {
}
return {};
},
getGroupedConversation: _state => {
return groupBy(Object.values(_state.conversations), message =>
new DateHelper(message.created_at).format()
);
},
getIsFetchingList: _state => _state.uiFlags.isFetchingList,
};
export const actions = {

View File

@@ -53,4 +53,57 @@ describe('#getters', () => {
expect(getters.getAllMessagesLoaded(state)).toEqual(false);
expect(getters.getIsFetchingList(state)).toEqual(false);
});
it('uiFlags', () => {
const state = {
conversations: {
1: {
id: 1,
content: 'Thanks for the help',
created_at: 1574075964,
},
2: {
id: 2,
content: 'Yes, It makes sense',
created_at: 1574092218,
},
3: {
id: 3,
content: 'Hey',
created_at: 1576340623,
},
4: {
id: 4,
content: 'How may I help you',
created_at: 1576340626,
},
},
};
expect(getters.getGroupedConversation(state)).toEqual({
'Nov 18, 2019': [
{
id: 1,
content: 'Thanks for the help',
created_at: 1574075964,
},
{
id: 2,
content: 'Yes, It makes sense',
created_at: 1574092218,
},
],
'Dec 14, 2019': [
{
id: 3,
content: 'Hey',
created_at: 1576340623,
},
{
id: 4,
content: 'How may I help you',
created_at: 1576340626,
},
],
});
});
});