Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #523

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/pages/ChatSupport.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import axios from 'axios';
import React, { useEffect, useRef, useState } from 'react';
import './ChatSupport.css';
const ChatSupport = () => {
const [messages, setMessages] = useState([]);
const [input, setInput] = useState('');
const [loading, setLoading] = useState(false);
const chatEndRef = useRef(null);
// Function to scroll to the latest message
const scrollToBottom = () => {
chatEndRef.current?.scrollIntoView({ behavior: 'smooth' });
};
// Function to send a message
const sendMessage = async () => {
if (input.trim() === '') return;
const userMessage = { sender: 'user', text: input };
setMessages([...messages, userMessage]);
setInput('');
setLoading(true);
try {
const response = await axios.post('/api/chat', { message: input });
const botMessage = { sender: 'bot', text: response.data.reply };
setMessages((prevMessages) => [...prevMessages, botMessage]);
} catch (error) {
const errorMessage = { sender: 'bot', text: 'Error: Could not send message. Please try again later.' };
setMessages((prevMessages) => [...prevMessages, errorMessage]);
} finally {
setLoading(false);
scrollToBottom();
}
};
// Handle pressing the Enter key
const handleKeyPress = (e) => {
if (e.key === 'Enter') {
sendMessage();
}
};

useEffect(() => {
scrollToBottom();
}, [messages]);

return (
<div className="chat-support">
<div className="chat-header">
<h3>Chat Support</h3>
</div>
<div className="chat-messages">
{messages.map((message, index) => (
<div
key={index}
className={`chat-message ${message.sender === 'user' ? 'user-message' : 'bot-message'}`}
>
{message.text}
</div>
))}
<div ref={chatEndRef} />
</div>
<div className="chat-input">
<input
type="text"
placeholder="Type your message..."
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={handleKeyPress}
/>
<button onClick={sendMessage} disabled={loading}>
{loading ? 'Sending...' : 'Send'}
</button>
</div>
</div>
);
};

export default ChatSupport;
Loading