closeChat

master
Tudor Stanciu 2020-06-07 02:38:23 +03:00
parent ba4f0cb72c
commit 3160cf9bd0
7 changed files with 63 additions and 13 deletions

View File

@ -30,6 +30,7 @@ export function loadBotSession(botName, userKey, botType) {
payload: data, payload: data,
botType botType
}); });
return data;
} catch (error) { } catch (error) {
throw error; throw error;
} }
@ -53,14 +54,27 @@ export function initializeChat(sessionId) {
}; };
} }
export function saveMessage( export function closeChat() {
chatId,
messageSourceId,
messageDate,
messageContent
) {
return async function(dispatch, getState) { return async function(dispatch, getState) {
try { 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( const event = await dispatch(
sendHttpRequest( sendHttpRequest(
api.saveMessage(chatId, messageSourceId, messageDate, messageContent) api.saveMessage(chatId, messageSourceId, messageDate, messageContent)

View File

@ -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_STARTED = "INITIALIZE_BOT_CHAT_STARTED";
export const INITIALIZE_BOT_CHAT_SUCCESS = "INITIALIZE_BOT_CHAT_SUCCESS"; export const INITIALIZE_BOT_CHAT_SUCCESS = "INITIALIZE_BOT_CHAT_SUCCESS";
export const SAVE_BOT_MESSAGE_SUCCESS = "SAVE_BOT_MESSAGE_SUCCESS"; export const SAVE_BOT_MESSAGE_SUCCESS = "SAVE_BOT_MESSAGE_SUCCESS";
export const CLOSE_BOT_CHAT_SUCCESS = "CLOSE_BOT_CHAT_SUCCESS";

View File

@ -5,7 +5,12 @@ const getBotSession = (botName, externalId, clientApplication, userKey) =>
get( get(
`${baseUrl}/system/initialize-session/${botName}/${externalId}/${clientApplication}/${userKey}` `${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) => const saveMessage = (chatId, messageSourceId, messageDate, messageContent) =>
post(`${baseUrl}/chat/message`, { post(`${baseUrl}/chat/message`, {
chatId, chatId,
@ -17,5 +22,6 @@ const saveMessage = (chatId, messageSourceId, messageDate, messageContent) =>
export default { export default {
getBotSession, getBotSession,
initializeChat, initializeChat,
closeChat,
saveMessage saveMessage
}; };

View File

@ -5,7 +5,13 @@ import { bindActionCreators } from "redux";
import { botType, bots, userKey } from "../constants"; import { botType, bots, userKey } from "../constants";
import Wizard from "./Wizard"; import Wizard from "./Wizard";
import { makeStyles } from "@material-ui/core/styles"; import { makeStyles } from "@material-ui/core/styles";
import { dismissBot, loadBotSession } from "../actionCreators"; import {
dismissBot,
loadBotSession,
initializeChat,
closeChat,
saveMessage
} from "../actionCreators";
const useStyles = makeStyles(theme => ({ const useStyles = makeStyles(theme => ({
bot: { bot: {
@ -28,13 +34,24 @@ const BotsManager = ({ bot, actions }) => {
setType(bot.type); setType(bot.type);
if (bot.type == botType.none) return; 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]); }, [bot.type]);
const dismissBot = () => {
actions.closeChat();
actions.dismissBot();
};
return ( return (
<div className={classes.botPosition}> <div className={classes.botPosition}>
<div className={classes.bot}> <div className={classes.bot}>
{type === botType.wizard && <Wizard dismissBot={actions.dismissBot} />} {type === botType.wizard && (
<Wizard dismissBot={dismissBot} saveMessage={actions.saveMessage} />
)}
</div> </div>
</div> </div>
); );
@ -53,7 +70,10 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) { function mapDispatchToProps(dispatch) {
return { return {
actions: bindActionCreators({ dismissBot, loadBotSession }, dispatch) actions: bindActionCreators(
{ dismissBot, loadBotSession, initializeChat, closeChat, saveMessage },
dispatch
)
}; };
} }

View File

@ -6,7 +6,7 @@ import { useTheme } from "@material-ui/core/styles";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { bots } from "../constants"; import { bots } from "../constants";
const Wizard = ({ dismissBot }) => { const Wizard = ({ dismissBot, saveMessage }) => {
const theme = useTheme(); const theme = useTheme();
const { t } = useTranslation(); const { t } = useTranslation();
@ -96,7 +96,8 @@ const Wizard = ({ dismissBot }) => {
}; };
Wizard.propTypes = { Wizard.propTypes = {
dismissBot: PropTypes.func.isRequired dismissBot: PropTypes.func.isRequired,
saveMessage: PropTypes.func.isRequired
}; };
export default Wizard; export default Wizard;

View File

@ -10,3 +10,8 @@ export const bots = {
export const userKey = { export const userKey = {
unknown: "Unknown" unknown: "Unknown"
}; };
export const messageSource = {
bot: 1,
user: 2
};

View File

@ -41,6 +41,9 @@ export default function chatbotReducer(state = initialState.bot, action) {
chat: { loading: false, loaded: true, ...action.payload } chat: { loading: false, loaded: true, ...action.payload }
}; };
case types.CLOSE_BOT_CHAT_SUCCESS:
return { ...state, chat: initialState.bot.chat };
default: default:
return state; return state;
} }