-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
58 lines (51 loc) · 1.95 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React, { useState } from 'react';
import axios from 'axios';
import './App.css';
function App() {
const [message, setMessage] = useState('');
const [chatHistory, setChatHistory] = useState([]);
const sendMessage = async () => {
if (message.trim() === '') return;
const userMessage = { sender: 'user', text: message };
setChatHistory(prev => [...prev, userMessage]);
try {
const response = await axios.post('http://127.0.0.1:5000/chat', { message });
const botMessage = { sender: 'bot', text: response.data.message };
setChatHistory(prev => [...prev, botMessage]);
} catch (error) {
console.error('Error sending message:', error);
}
setMessage('');
};
return (
<div className="flex flex-col items-center justify-center min-h-screen bg-gray-100">
<div className="w-full max-w-md p-4 bg-white rounded-lg shadow-md">
<div className="chat-window h-64 overflow-y-auto mb-4">
{chatHistory.map((chat, index) => (
<div key={index} className={`message ${chat.sender === 'user' ? 'text-right' : 'text-left'}`}>
<div className={`inline-block p-2 rounded-lg ${chat.sender === 'user' ? 'bg-blue-500 text-white' : 'bg-gray-200'}`}>
{chat.text}
</div>
</div>
))}
</div>
<div className="input-area flex">
<input
type="text"
value={message}
onChange={e => setMessage(e.target.value)}
placeholder="Type a message..."
className="flex-1 p-2 border border-gray-300 rounded-l-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
<button
onClick={sendMessage}
className="px-4 py-2 bg-blue-500 text-white rounded-r-lg hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500"
>
Send
</button>
</div>
</div>
</div>
);
}
export default App;