From ba4f0cb72c509a77d4d75ba5066374adc3a51566 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 7 Jun 2020 01:49:15 +0300 Subject: [PATCH] bot session initialization update --- src/features/chatbot/actionCreators.js | 16 ++++++++++------ src/features/chatbot/components/BotsManager.js | 2 +- src/features/chatbot/constants.js | 4 ++-- src/features/chatbot/reducer.js | 17 +++++++++++++++-- src/redux/reducers/initialState.js | 2 +- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/features/chatbot/actionCreators.js b/src/features/chatbot/actionCreators.js index 6d20314..0c263ef 100644 --- a/src/features/chatbot/actionCreators.js +++ b/src/features/chatbot/actionCreators.js @@ -10,13 +10,16 @@ export function dismissBot() { return { type: types.DISMISS_BOT }; } -export function loadBotSession(botName, userKey) { +export function loadBotSession(botName, userKey, botType) { return async function(dispatch, getState) { try { - dispatch({ type: types.INITIALIZE_BOT_SESSION_STARTED }); - const { frontendSession } = getState(); - const externalId = frontendSession.sessionId; - const clientApplication = frontendSession.applicationCode; + const state = getState(); + const session = state.bot.session[botType]; + if (session && (session.loading || session.loaded)) return; + + dispatch({ type: types.INITIALIZE_BOT_SESSION_STARTED, botType }); + const externalId = state.frontendSession.sessionId; + const clientApplication = state.frontendSession.applicationCode; const data = await dispatch( sendHttpRequest( api.getBotSession(botName, externalId, clientApplication, userKey) @@ -24,7 +27,8 @@ export function loadBotSession(botName, userKey) { ); dispatch({ type: types.INITIALIZE_BOT_SESSION_SUCCESS, - payload: data + payload: data, + botType }); } catch (error) { throw error; diff --git a/src/features/chatbot/components/BotsManager.js b/src/features/chatbot/components/BotsManager.js index 2e284c9..f73edf3 100644 --- a/src/features/chatbot/components/BotsManager.js +++ b/src/features/chatbot/components/BotsManager.js @@ -28,7 +28,7 @@ const BotsManager = ({ bot, actions }) => { setType(bot.type); if (bot.type == botType.none) return; - actions.loadBotSession(bots.Zirhan, userKey.unknown); + actions.loadBotSession(bots.Zirhan, userKey.unknown, bot.type); }, [bot.type]); return ( diff --git a/src/features/chatbot/constants.js b/src/features/chatbot/constants.js index ceb8cce..5b9a0b9 100644 --- a/src/features/chatbot/constants.js +++ b/src/features/chatbot/constants.js @@ -1,6 +1,6 @@ export const botType = { - none: Symbol("none"), - wizard: Symbol("wizard") + none: "none", + wizard: "wizard" }; export const bots = { diff --git a/src/features/chatbot/reducer.js b/src/features/chatbot/reducer.js index a697426..01e0690 100644 --- a/src/features/chatbot/reducer.js +++ b/src/features/chatbot/reducer.js @@ -11,12 +11,25 @@ export default function chatbotReducer(state = initialState.bot, action) { return { ...state, type: botType.none }; case types.INITIALIZE_BOT_SESSION_STARTED: - return { ...state, session: { loading: true, loaded: false } }; + return { + ...state, + session: { + ...state.session, + [action.botType]: { loading: true, loaded: false } + } + }; case types.INITIALIZE_BOT_SESSION_SUCCESS: return { ...state, - session: { loading: false, loaded: true, ...action.payload } + session: { + ...state.session, + [action.botType]: { + loading: false, + loaded: true, + ...action.payload + } + } }; case types.INITIALIZE_BOT_CHAT_STARTED: diff --git a/src/redux/reducers/initialState.js b/src/redux/reducers/initialState.js index 90d82a9..4733314 100644 --- a/src/redux/reducers/initialState.js +++ b/src/redux/reducers/initialState.js @@ -20,7 +20,7 @@ export default { }, bot: { type: null, - session: { loading: false, loaded: false }, + session: {}, chat: { loading: false, loaded: false } }, ajaxCallsInProgress: 0