2020-06-06 01:42:11 +03:00
|
|
|
import React from "react";
|
2020-06-06 03:16:14 +03:00
|
|
|
import PropTypes from "prop-types";
|
2020-06-06 01:42:11 +03:00
|
|
|
import ChatBot from "react-simple-chatbot";
|
2020-06-06 02:47:03 +03:00
|
|
|
import { ThemeProvider } from "styled-components";
|
|
|
|
import { useTheme } from "@material-ui/core/styles";
|
2020-06-06 03:35:39 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2020-06-07 04:12:20 +03:00
|
|
|
import { bots, messageSource } from "../constants";
|
2020-06-06 01:42:11 +03:00
|
|
|
|
2020-06-07 02:38:23 +03:00
|
|
|
const Wizard = ({ dismissBot, saveMessage }) => {
|
2020-06-06 02:47:03 +03:00
|
|
|
const theme = useTheme();
|
2020-06-06 03:35:39 +03:00
|
|
|
const { t } = useTranslation();
|
2020-06-06 02:47:03 +03:00
|
|
|
|
|
|
|
const botTheme = {
|
|
|
|
background: "#f5f8fb",
|
|
|
|
fontFamily: "monospace",
|
|
|
|
headerBgColor: theme.palette.primary.main,
|
|
|
|
headerFontColor: "#fff",
|
|
|
|
headerFontSize: "16px",
|
|
|
|
botBubbleColor: theme.palette.primary.main,
|
|
|
|
botFontColor: "#fff",
|
|
|
|
userBubbleColor: "#fff",
|
|
|
|
userFontColor: "#4a4a4a"
|
|
|
|
};
|
|
|
|
|
2020-06-07 04:12:20 +03:00
|
|
|
const getMessage = message => input => {
|
|
|
|
const currentDate = new Date();
|
|
|
|
let messageToSave = message;
|
|
|
|
if (message.includes("previousValue") && input.previousValue) {
|
|
|
|
messageToSave = message.replace("{previousValue}", input.previousValue);
|
|
|
|
}
|
|
|
|
saveMessage(messageSource.bot, currentDate, messageToSave);
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
const validate = text => {
|
|
|
|
const currentDate = new Date();
|
|
|
|
saveMessage(messageSource.user, currentDate, text);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-06-06 01:42:11 +03:00
|
|
|
const steps = [
|
|
|
|
{
|
2020-06-06 02:47:03 +03:00
|
|
|
id: "1",
|
2020-06-07 04:12:20 +03:00
|
|
|
message: getMessage(t("Chatbot.Wizard.Message1")),
|
2020-06-06 02:47:03 +03:00
|
|
|
trigger: "2"
|
2020-06-06 01:42:11 +03:00
|
|
|
},
|
|
|
|
{
|
2020-06-06 02:47:03 +03:00
|
|
|
id: "2",
|
2020-06-07 04:12:20 +03:00
|
|
|
message: getMessage(t("Chatbot.Wizard.Message2")),
|
2020-06-06 02:47:03 +03:00
|
|
|
trigger: "3"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "3",
|
2020-06-07 04:12:20 +03:00
|
|
|
message: getMessage(t("Chatbot.Wizard.Message3")),
|
2020-06-06 02:47:03 +03:00
|
|
|
trigger: "4"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "4",
|
|
|
|
user: true,
|
2020-06-07 04:12:20 +03:00
|
|
|
validator: validate,
|
2020-06-06 02:47:03 +03:00
|
|
|
trigger: "5"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "5",
|
2020-06-07 04:12:20 +03:00
|
|
|
message: getMessage(t("Chatbot.Wizard.Message5")),
|
2020-06-06 03:16:14 +03:00
|
|
|
trigger: "6"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "6",
|
|
|
|
user: true,
|
2020-06-07 04:12:20 +03:00
|
|
|
validator: validate,
|
2020-06-06 03:16:14 +03:00
|
|
|
trigger: "7"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "7",
|
2020-06-07 04:12:20 +03:00
|
|
|
message: getMessage(t("Chatbot.Wizard.Message7")),
|
2020-06-06 03:16:14 +03:00
|
|
|
trigger: "8"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "8",
|
|
|
|
user: true,
|
2020-06-07 04:12:20 +03:00
|
|
|
validator: validate,
|
2020-06-06 03:16:14 +03:00
|
|
|
trigger: "9"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "9",
|
2020-06-07 04:12:20 +03:00
|
|
|
message: getMessage(t("Chatbot.Wizard.Message9")),
|
2020-06-06 01:42:11 +03:00
|
|
|
end: true
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2020-06-06 03:16:14 +03:00
|
|
|
const handleEnd = () => {
|
|
|
|
setTimeout(dismissBot, 3000);
|
|
|
|
};
|
|
|
|
|
2020-06-06 14:21:54 +03:00
|
|
|
const getAvatar = () => {
|
|
|
|
const basePath = "public/icons/wizard.png";
|
|
|
|
if (process.env.PUBLIC_URL) {
|
|
|
|
return `${process.env.PUBLIC_URL}/${basePath}`;
|
|
|
|
} else {
|
|
|
|
return basePath;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-06 02:47:03 +03:00
|
|
|
return (
|
|
|
|
<ThemeProvider theme={botTheme}>
|
|
|
|
<ChatBot
|
2020-06-06 03:16:14 +03:00
|
|
|
handleEnd={handleEnd}
|
2020-06-06 02:47:03 +03:00
|
|
|
steps={steps}
|
2020-06-06 14:21:54 +03:00
|
|
|
botAvatar={getAvatar()}
|
2020-06-07 01:34:52 +03:00
|
|
|
headerTitle={bots.Zirhan}
|
2020-06-06 02:47:03 +03:00
|
|
|
/>
|
|
|
|
</ThemeProvider>
|
|
|
|
);
|
2020-06-06 01:42:11 +03:00
|
|
|
};
|
|
|
|
|
2020-06-06 03:16:14 +03:00
|
|
|
Wizard.propTypes = {
|
2020-06-07 02:38:23 +03:00
|
|
|
dismissBot: PropTypes.func.isRequired,
|
|
|
|
saveMessage: PropTypes.func.isRequired
|
2020-06-06 03:16:14 +03:00
|
|
|
};
|
|
|
|
|
2020-06-06 01:42:11 +03:00
|
|
|
export default Wizard;
|