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 (
- {type === botType.wizard && } + {type === botType.wizard && ( + + )}
); @@ -53,7 +70,10 @@ function mapStateToProps(state) { function mapDispatchToProps(dispatch) { return { - actions: bindActionCreators({ dismissBot, loadBotSession }, dispatch) + actions: bindActionCreators( + { dismissBot, loadBotSession, initializeChat, closeChat, saveMessage }, + dispatch + ) }; } diff --git a/src/features/chatbot/components/Wizard.js b/src/features/chatbot/components/Wizard.js index d2bd761..f4956f6 100644 --- a/src/features/chatbot/components/Wizard.js +++ b/src/features/chatbot/components/Wizard.js @@ -6,7 +6,7 @@ import { useTheme } from "@material-ui/core/styles"; import { useTranslation } from "react-i18next"; import { bots } from "../constants"; -const Wizard = ({ dismissBot }) => { +const Wizard = ({ dismissBot, saveMessage }) => { const theme = useTheme(); const { t } = useTranslation(); @@ -96,7 +96,8 @@ const Wizard = ({ dismissBot }) => { }; Wizard.propTypes = { - dismissBot: PropTypes.func.isRequired + dismissBot: PropTypes.func.isRequired, + saveMessage: PropTypes.func.isRequired }; export default Wizard; diff --git a/src/features/chatbot/constants.js b/src/features/chatbot/constants.js index 5b9a0b9..81703eb 100644 --- a/src/features/chatbot/constants.js +++ b/src/features/chatbot/constants.js @@ -10,3 +10,8 @@ export const bots = { export const userKey = { unknown: "Unknown" }; + +export const messageSource = { + bot: 1, + user: 2 +}; diff --git a/src/features/chatbot/reducer.js b/src/features/chatbot/reducer.js index 01e0690..82e9fdb 100644 --- a/src/features/chatbot/reducer.js +++ b/src/features/chatbot/reducer.js @@ -41,6 +41,9 @@ export default function chatbotReducer(state = initialState.bot, action) { chat: { loading: false, loaded: true, ...action.payload } }; + case types.CLOSE_BOT_CHAT_SUCCESS: + return { ...state, chat: initialState.bot.chat }; + default: return state; }