-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrontend_dummy.ts
86 lines (74 loc) · 2.3 KB
/
Frontend_dummy.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Assuming you're using a modern framework like React with hooks
import { useState, useEffect } from 'react';
interface Message {
role: 'user' | 'assistant';
content: string;
}
const ChatComponent = () => {
const [messages, setMessages] = useState<Message[]>([]);
const [input, setInput] = useState('');
const [userId, setUserId] = useState('');
useEffect(() => {
// Generate a random userId for this session
setUserId(Math.random().toString(36).substring(7));
}, []);
const sendMessage = async () => {
if (!input.trim()) return;
const userMessage: Message = { role: 'user', content: input };
setMessages(prev => [...prev, userMessage]);
setInput('');
try {
const response = await fetch('https://chatgpt-ciplak.web.app/api/chatbot', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: input, userId }),
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
const aiMessage: Message = { role: 'assistant', content: data.reply };
setMessages(prev => [...prev, aiMessage]);
} catch (error) {
console.error('Error:', error);
const errorMessage: Message = {
role: 'assistant',
content: 'Sorry, there was an error processing your request.'
};
setMessages(prev => [...prev, errorMessage]);
}
};
const clearHistory = async () => {
try {
await fetch('https://chatgpt-ciplak.web.app/api/clearChatHistory', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ userId }),
});
setMessages([]);
} catch (error) {
console.error('Error clearing history:', error);
}
};
return (
<div>
{messages.map((msg, index) => (
<div key={index} className={msg.role}>
{msg.content}
</div>
))}
<input
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && sendMessage()}
/>
<button onClick={sendMessage}>Send</button>
<button onClick={clearHistory}>Clear History</button>
</div>
);
};
export default ChatComponent;