diff --git a/src/features/chatbot/actionCreators.js b/src/features/chatbot/actionCreators.js index 0c263ef..82b9eb6 100644 --- a/src/features/chatbot/actionCreators.js +++ b/src/features/chatbot/actionCreators.js @@ -30,6 +30,7 @@ export function loadBotSession(botName, userKey, botType) { payload: data, botType }); + return data; } catch (error) { throw error; } @@ -53,14 +54,27 @@ export function initializeChat(sessionId) { }; } -export function saveMessage( - chatId, - messageSourceId, - messageDate, - messageContent -) { +export function closeChat() { return async function(dispatch, getState) { try { + const { chatId } = getState().bot.chat; + if (!chatId) return; + + const data = await dispatch(sendHttpRequest(api.closeChat(chatId))); + dispatch({ + type: types.CLOSE_BOT_CHAT_SUCCESS, + payload: data + }); + } catch (error) { + throw error; + } + }; +} + +export function saveMessage(messageSourceId, messageDate, messageContent) { + return async function(dispatch, getState) { + try { + const { chatId } = getState().bot.chat; const event = await dispatch( sendHttpRequest( api.saveMessage(chatId, messageSourceId, messageDate, messageContent) diff --git a/src/features/chatbot/actionTypes.js b/src/features/chatbot/actionTypes.js index b81c657..c2b8b1b 100644 --- a/src/features/chatbot/actionTypes.js +++ b/src/features/chatbot/actionTypes.js @@ -6,3 +6,4 @@ export const INITIALIZE_BOT_SESSION_SUCCESS = "INITIALIZE_BOT_SESSION_SUCCESS"; export const INITIALIZE_BOT_CHAT_STARTED = "INITIALIZE_BOT_CHAT_STARTED"; export const INITIALIZE_BOT_CHAT_SUCCESS = "INITIALIZE_BOT_CHAT_SUCCESS"; export const SAVE_BOT_MESSAGE_SUCCESS = "SAVE_BOT_MESSAGE_SUCCESS"; +export const CLOSE_BOT_CHAT_SUCCESS = "CLOSE_BOT_CHAT_SUCCESS"; diff --git a/src/features/chatbot/api.js b/src/features/chatbot/api.js index c15fa27..482a32f 100644 --- a/src/features/chatbot/api.js +++ b/src/features/chatbot/api.js @@ -5,7 +5,12 @@ const getBotSession = (botName, externalId, clientApplication, userKey) => get( `${baseUrl}/system/initialize-session/${botName}/${externalId}/${clientApplication}/${userKey}` ); -const initializeChat = () => get(`${baseUrl}/chat/initialize`); +const initializeChat = sessionId => + get(`${baseUrl}/chat/initialize/${sessionId}`); +const closeChat = chatId => + post(`${baseUrl}/chat/close`, { + chatId + }); const saveMessage = (chatId, messageSourceId, messageDate, messageContent) => post(`${baseUrl}/chat/message`, { chatId, @@ -17,5 +22,6 @@ const saveMessage = (chatId, messageSourceId, messageDate, messageContent) => export default { getBotSession, initializeChat, + closeChat, saveMessage }; diff --git a/src/features/chatbot/components/BotsManager.js b/src/features/chatbot/components/BotsManager.js index f73edf3..4eb1160 100644 --- a/src/features/chatbot/components/BotsManager.js +++ b/src/features/chatbot/components/BotsManager.js @@ -5,7 +5,13 @@ import { bindActionCreators } from "redux"; import { botType, bots, userKey } from "../constants"; import Wizard from "./Wizard"; import { makeStyles } from "@material-ui/core/styles"; -import { dismissBot, loadBotSession } from "../actionCreators"; +import { + dismissBot, + loadBotSession, + initializeChat, + closeChat, + saveMessage +} from "../actionCreators"; const useStyles = makeStyles(theme => ({ bot: { @@ -28,13 +34,24 @@ const BotsManager = ({ bot, actions }) => { setType(bot.type); if (bot.type == botType.none) return; - actions.loadBotSession(bots.Zirhan, userKey.unknown, bot.type); + actions + .loadBotSession(bots.Zirhan, userKey.unknown, bot.type) + .then(session => { + actions.initializeChat(session.sessionId); + }); }, [bot.type]); + const dismissBot = () => { + actions.closeChat(); + actions.dismissBot(); + }; + return (