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,
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)

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_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";

View File

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

View File

@ -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 (
<div className={classes.botPosition}>
<div className={classes.bot}>
{type === botType.wizard && <Wizard dismissBot={actions.dismissBot} />}
{type === botType.wizard && (
<Wizard dismissBot={dismissBot} saveMessage={actions.saveMessage} />
)}
</div>
</div>
);
@ -53,7 +70,10 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) {
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 { 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;

View File

@ -10,3 +10,8 @@ export const bots = {
export const userKey = {
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 }
};
case types.CLOSE_BOT_CHAT_SUCCESS:
return { ...state, chat: initialState.bot.chat };
default:
return state;
}