UNPKG

@botonic/react

Version:

Build Chatbots using React

175 lines 6.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.messagesReducer = void 0; const index_types_1 = require("../../index-types"); const actions_1 = require("./actions"); const messagesReducer = (state, action) => { switch (action.type) { case actions_1.WebchatAction.ADD_MESSAGE: return addMessageReducer(state, action); case actions_1.WebchatAction.ADD_MESSAGE_COMPONENT: return addMessageComponent(state, action); case actions_1.WebchatAction.UPDATE_MESSAGE: return updateMessageReducer(state, action); case actions_1.WebchatAction.UPDATE_CUSTOM_MESSAGE_PROPS: return updateCustomMessagePropsReducer(state, action); case actions_1.WebchatAction.UPDATE_REPLIES: return { ...state, replies: action.payload }; case actions_1.WebchatAction.REMOVE_REPLIES: return { ...state, replies: undefined }; case actions_1.WebchatAction.CLEAR_MESSAGES: return { ...state, messagesJSON: [], messagesComponents: [], numUnreadMessages: 0, }; case actions_1.WebchatAction.UPDATE_LAST_MESSAGE_DATE: return { ...state, lastMessageUpdate: action.payload, }; case actions_1.WebchatAction.RESET_UNREAD_MESSAGES: return resetUnreadMessages(state); case actions_1.WebchatAction.SET_LAST_MESSAGE_VISIBLE: return { ...state, isLastMessageVisible: action.payload, }; default: throw new Error(); } }; exports.messagesReducer = messagesReducer; function addMessageComponent(state, action) { const messageComponent = action.payload; const isUnreadMessage = messageComponent.props?.isUnread && messageComponent.props?.sentBy !== index_types_1.SENDERS.user && messageComponent.props?.sentBy !== index_types_1.SENDERS.system; const numUnreadMessages = isUnreadMessage ? state.numUnreadMessages + 1 : state.numUnreadMessages; return { ...state, messagesComponents: [...(state.messagesComponents || []), messageComponent], numUnreadMessages, }; } function resetUnreadMessages(state) { const messagesComponents = state.messagesComponents.map(messageComponent => { if (messageComponent.props.isUnread) { messageComponent = { ...messageComponent, props: { ...messageComponent.props, isUnread: false }, }; } return messageComponent; }); const messagesJSON = state.messagesJSON.map(messageJSON => { if (messageJSON.isUnread) { messageJSON.isUnread = false; } return messageJSON; }); return { ...state, messagesComponents, messagesJSON, numUnreadMessages: 0, }; } function updateMessageReducer(state, action) { const messageId = action.payload.id; const msgIndex = state.messagesJSON.map(m => m.id).indexOf(messageId); if (msgIndex > -1) { const msgComponent = state.messagesComponents[msgIndex]; let updatedMsgComponent = {}; if (msgComponent) { updatedMsgComponent = { ...msgComponent, ...{ props: { ...msgComponent.props, ack: action.payload.ack }, }, }; } const updatedMessagesComponents = msgComponent ? getUpdatedMessagesComponents(state, msgIndex, updatedMsgComponent) : state.messagesComponents; const messageJSON = state.messagesJSON.find(m => m.id === messageId); const updatedMessagesJSON = messageJSON ? getUpdatedMessagesJSON(state, msgIndex, action.payload) : state.messagesJSON; const numUnreadMessages = updatedMessagesComponents.filter(messageComponent => messageComponent.props?.isUnread && messageComponent.props?.sentBy !== index_types_1.SENDERS.user && messageComponent.props?.sentBy !== index_types_1.SENDERS.system).length; return { ...state, messagesJSON: updatedMessagesJSON, messagesComponents: updatedMessagesComponents, numUnreadMessages, }; } return state; } function addMessageReducer(state, action) { if (state.messagesJSON?.find(m => m.id === action.payload.id)) { return state; } return { ...state, messagesJSON: [...(state.messagesJSON || []), action.payload], }; } function updateCustomMessagePropsReducer(state, action) { const { messageId, props } = action.payload; if (!messageId) { return state; } // Similar to updateMessageReducer but only for custom messages when update props const msgIndex = state.messagesJSON.map(m => m.id).indexOf(messageId); if (msgIndex > -1) { const msgComponent = state.messagesComponents[msgIndex]; if (msgComponent) { msgComponent.props = { ...msgComponent.props, ack: action.payload.ack, ...props, }; } const updatedMessagesComponents = msgComponent ? getUpdatedMessagesComponents(state, msgIndex, msgComponent) : state.messagesComponents; const messageJSON = state.messagesJSON.find(m => m.id === messageId); if (messageJSON) { messageJSON.data = { ...messageJSON.data, ...props, }; } const updatedMessagesJSON = messageJSON ? getUpdatedMessagesJSON(state, msgIndex, messageJSON) : state.messagesJSON; return { ...state, messagesJSON: updatedMessagesJSON, messagesComponents: updatedMessagesComponents, }; } return state; } // Helper functions to update messagesComponents and messagesJSON function getUpdatedMessagesComponents(state, msgIndex, updatedMessageComponent) { return [ ...state.messagesComponents.slice(0, msgIndex), { ...updatedMessageComponent }, ...state.messagesComponents.slice(msgIndex + 1), ]; } function getUpdatedMessagesJSON(state, msgIndex, messageJSON) { return [ ...state.messagesJSON.slice(0, msgIndex), { ...messageJSON }, ...state.messagesJSON.slice(msgIndex + 1), ]; } //# sourceMappingURL=messages-reducer.js.map