-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.jsx
More file actions
63 lines (57 loc) · 1.88 KB
/
page.jsx
File metadata and controls
63 lines (57 loc) · 1.88 KB
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
59
60
61
62
63
'use client'
import MainLayout from '@/app/components/MainLayout';
import { useState } from 'react';
export default function Chatbot() {
const [input, setInput] = useState('');
const [messages, setMessages] = useState([]);
const handleSend = async () => {
if (!input.trim()) return;
// Add user message to the chat
setMessages((prev) => [...prev, { role: 'user', content: input }]);
setInput('');
// Fetch chatbot response from Node.js backend
const response = await fetch('http://localhost:8080/api/v1/user/chatbot', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ prompt: input }),
});
if (response.ok) {
const data = await response.json();
setMessages((prev) => [...prev, { role: 'assistant', content: data.response }]);
} else {
setMessages((prev) => [...prev, { role: 'assistant', content: 'Failed to fetch response.' }]);
}
};
return (
<MainLayout >
<div className="h-[80vh] overflow-y-auto mb-4">
{messages.map((msg, index) => (
<div
key={index}
className={`p-2 my-2 rounded-lg ${msg.role === 'user' ? 'bg-blue-100 ml-auto' : 'bg-gray-100 mr-auto'}`}
>
{msg.content}
</div>
))}
</div>
<div className="flex">
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && handleSend()}
className="flex-1 p-2 border rounded-l-lg"
placeholder="Type a message..."
/>
<button
onClick={handleSend}
className="bg-blue-600 text-white p-2 rounded-r-lg hover:bg-blue-700"
>
Send
</button>
</div>
</MainLayout>
);
}