Create Index.js
Browse files
Index.js
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import React, { useState } from 'react';
|
2 |
+
import axios from 'axios';
|
3 |
+
|
4 |
+
const Chatbot = () => {
|
5 |
+
const [input, setInput] = useState('');
|
6 |
+
const [conversation, setConversation] = useState([]);
|
7 |
+
|
8 |
+
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
9 |
+
setInput(e.target.value);
|
10 |
+
};
|
11 |
+
|
12 |
+
const handleSendMessage = () => {
|
13 |
+
const newMessage = { text: input, sender: 'user' };
|
14 |
+
setConversation((prevConversation) => [...prevConversation, newMessage]);
|
15 |
+
axios.post('https://example.com/api/chatbot', { input })
|
16 |
+
.then((response) => {
|
17 |
+
const botResponse = { text: response.data, sender: 'bot' };
|
18 |
+
setConversation((prevConversation) => [...prevConversation, botResponse]);
|
19 |
+
})
|
20 |
+
.catch((error) => {
|
21 |
+
console.error(error);
|
22 |
+
});
|
23 |
+
setInput('');
|
24 |
+
};
|
25 |
+
|
26 |
+
return (
|
27 |
+
<div className="max-w-md mx-auto p-4 bg-gray-100 rounded-md shadow-md">
|
28 |
+
<h1 className="text-lg font-bold mb-4">Chatbot</h1>
|
29 |
+
<ul className="mb-4">
|
30 |
+
{conversation.map((message, index) => (
|
31 |
+
<li key={index} className={`text-lg ${message.sender === 'user' ? 'text-blue-600' : 'text-green-600'}`}>
|
32 |
+
{message.text}
|
33 |
+
</li>
|
34 |
+
))}
|
35 |
+
</ul>
|
36 |
+
<input
|
37 |
+
type="text"
|
38 |
+
value={input}
|
39 |
+
onChange={handleInputChange}
|
40 |
+
className="w-full p-2 mb-4 border border-gray-400 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-600"
|
41 |
+
/>
|
42 |
+
<button
|
43 |
+
onClick={handleSendMessage}
|
44 |
+
className="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-600"
|
45 |
+
>
|
46 |
+
Send
|
47 |
+
</button>
|
48 |
+
</div>
|
49 |
+
);
|
50 |
+
};
|
51 |
+
|
52 |
+
export default Chatbot;
|