bot session initialization update

master
Tudor Stanciu 2020-06-07 01:49:15 +03:00
parent 482ef013f5
commit ba4f0cb72c
5 changed files with 29 additions and 12 deletions

View File

@ -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;

View File

@ -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 (

View File

@ -1,6 +1,6 @@
export const botType = {
none: Symbol("none"),
wizard: Symbol("wizard")
none: "none",
wizard: "wizard"
};
export const bots = {

View File

@ -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:

View File

@ -20,7 +20,7 @@ export default {
},
bot: {
type: null,
session: { loading: false, loaded: false },
session: {},
chat: { loading: false, loaded: false }
},
ajaxCallsInProgress: 0