Add conversation group by helpers

This commit is contained in:
Muhsin 2020-12-27 10:11:00 +05:30
parent 56280eaf35
commit 30920ae2e8

View file

@ -1,5 +1,7 @@
/* eslint no-param-reassign: 0 */ /* eslint no-param-reassign: 0 */
const groupBy = require('lodash.groupby');
import { formatUnixDate } from 'shared/helpers/DateHelper';
import { isASubmittedFormMessage } from 'shared/helpers/MessageTypeHelper';
import getUuid from 'widget/helpers/uuid'; import getUuid from 'widget/helpers/uuid';
import { MESSAGE_STATUS, MESSAGE_TYPE } from 'shared/constants/messages'; import { MESSAGE_STATUS, MESSAGE_TYPE } from 'shared/constants/messages';
@ -67,3 +69,42 @@ export const createPendingAttachment = data => {
}; };
return pendingMessage; return pendingMessage;
}; };
const getSenderName = message => (message.sender ? message.sender.name : '');
const shouldShowAvatar = (message, nextMessage) => {
const currentSender = getSenderName(message);
const nextSender = getSenderName(nextMessage);
return (
currentSender !== nextSender ||
message.message_type !== nextMessage.message_type ||
isASubmittedFormMessage(nextMessage)
);
};
const groupConversationBySender = conversationsForADate =>
conversationsForADate.map((message, index) => {
let showAvatar = false;
const isLastMessage = index === conversationsForADate.length - 1;
if (isASubmittedFormMessage(message)) {
showAvatar = false;
} else if (isLastMessage) {
showAvatar = true;
} else {
const nextMessage = conversationsForADate[index + 1];
showAvatar = shouldShowAvatar(message, nextMessage);
}
return { showAvatar, ...message };
});
export const getGroupedConversation = ({ conversations }) => {
const conversationGroupedByDate = groupBy(
Object.values(conversations),
message => formatUnixDate(message.created_at)
);
return Object.keys(conversationGroupedByDate).map(date => ({
date,
messages: groupConversationBySender(conversationGroupedByDate[date]),
}));
};