e4b7b9a63d
* State structure * How to refactr action modules * feat: Add state and mutations for multiple conversation on widget * Adds actions to support multiple conversation * Fix linter errors
71 lines
2 KiB
JavaScript
71 lines
2 KiB
JavaScript
import { MESSAGE_TYPE } from 'widget/helpers/constants';
|
|
import { isASubmittedFormMessage } from 'shared/helpers/MessageTypeHelper';
|
|
|
|
import getUuid from '../../../helpers/uuid';
|
|
export const createTemporaryMessage = ({ attachments, content }) => {
|
|
const timestamp = new Date().getTime() / 1000;
|
|
return {
|
|
id: getUuid(),
|
|
content,
|
|
attachments,
|
|
status: 'in_progress',
|
|
created_at: timestamp,
|
|
message_type: MESSAGE_TYPE.INCOMING,
|
|
};
|
|
};
|
|
export const createTemporaryAttachmentMessage = ({
|
|
thumbUrl,
|
|
fileType,
|
|
content,
|
|
}) => {
|
|
const attachment = {
|
|
thumb_url: thumbUrl,
|
|
data_url: thumbUrl,
|
|
file_type: fileType,
|
|
status: 'in_progress',
|
|
};
|
|
const message = createTemporaryMessage({
|
|
attachments: [attachment],
|
|
content,
|
|
});
|
|
return message;
|
|
};
|
|
|
|
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)
|
|
);
|
|
};
|
|
|
|
export 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 findUndeliveredMessage = (messageInbox, { content }) =>
|
|
Object.values(messageInbox).filter(
|
|
message => message.content === content && message.status === 'in_progress'
|
|
);
|
|
|
|
export const getNonDeletedMessages = ({ messages }) => {
|
|
return messages.filter(
|
|
item => !(item.content_attributes && item.content_attributes.deleted)
|
|
);
|
|
};
|