From a2e45e89dd9ac22112c488a5ac9fed53f6504177 Mon Sep 17 00:00:00 2001
From: Avanish Singh <79073722+aaviix@users.noreply.github.com>
Date: Thu, 16 Nov 2023 16:24:16 +0100
Subject: [PATCH] Create Chatbotcomponent.jsx

---
 frontend-react/src/Chatbotcomponent.jsx | 77 +++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 frontend-react/src/Chatbotcomponent.jsx

diff --git a/frontend-react/src/Chatbotcomponent.jsx b/frontend-react/src/Chatbotcomponent.jsx
new file mode 100644
index 0000000..6a5e858
--- /dev/null
+++ b/frontend-react/src/Chatbotcomponent.jsx
@@ -0,0 +1,77 @@
+import React from "react";
+import { Launcher } from "react-chat-window"
+import io from "socket.io-client";
+
+class ChatBotRobot extends React.Component {
+    constructor(props) {
+        super(props);
+
+        this.state = {
+            messageList: [],
+            socket: io("http://localhost:3000"),
+            room: "user1",
+        }
+
+    }
+
+    UNSAFE_componentWillMount() {
+        this._sendMessage("Hello! Welcome to 'RESWHIZ'. I am your RECIPE recommendation assistance. Would you like to see what can I do?Type 'start' to get started. !");
+    }
+
+    componentDidMount() {
+        this.state.socket.connect(true);
+        this.state.socket.emit('join', this.state.room);
+
+        this.state.socket.on("send-msg-response", async (msg) => {
+            this.state.messageList.pop();
+            await this.setState({
+                messageList: [...this.state.messageList]
+            })
+
+            this._sendMessage(msg);
+        })
+
+    }
+
+    async _onMessageWasSent(message) {
+        await this.setState({
+            messageList: [...this.state.messageList, message]
+        })
+
+        this._sendMessage("••••");
+        console.log(message.data.text);
+        await this.state.socket.emit('new-msg', { msg: message.data.text, room: this.state.room })
+
+    }
+
+    _sendMessage(text) {
+        if (text.length > 0) {
+            this.setState({
+                messageList: [...this.state.messageList, {
+                    author: 'them',
+                    type: 'text',
+                    data: { text }
+                },]
+            })
+        }
+    }
+
+    render() {
+
+        return (
+            <div id="chatbox" className="chatbox">
+                <Launcher
+                    agentProfile={{
+                        teamName: 'RESWHIZ',
+                        image: 'Chatbot.png'
+                    }}
+                    onMessageWasSent={this._onMessageWasSent.bind(this)}
+                    messageList={this.state.messageList}
+                    showEmoji
+                />
+            </div>
+        );
+    }
+}
+
+export default ChatBotRobot;
-- 
GitLab