Changed around line 1
+ const MONGODB_URI = 'mongodb+srv://dvaughnhouse:CRKV3zAwxL5SR614@cluster0.o3qrq.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0';
+ const PORT = 3000;
+
+ document.getElementById('chat-form').addEventListener('submit', async (e) => {
+ e.preventDefault();
+
+ const userInput = document.getElementById('user-input').value;
+ if (!userInput) return;
+
+ // Add user message to chat history
+ const chatHistory = document.getElementById('chat-history');
+ const userMessage = document.createElement('div');
+ userMessage.className = 'message user-message';
+ userMessage.textContent = userInput;
+ chatHistory.appendChild(userMessage);
+
+ // Clear input
+ document.getElementById('user-input').value = '';
+
+ // Save to MongoDB
+ try {
+ const response = await fetch(`http://localhost:${PORT}/save`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({ message: userInput }),
+ });
+
+ if (!response.ok) throw new Error('Failed to save message');
+
+ const data = await response.json();
+ const botMessage = document.createElement('div');
+ botMessage.className = 'message bot-message';
+ botMessage.textContent = `Message saved with ID: ${data.insertedId}`;
+ chatHistory.appendChild(botMessage);
+ } catch (error) {
+ console.error('Error:', error);
+ const errorMessage = document.createElement('div');
+ errorMessage.className = 'message bot-message';
+ errorMessage.textContent = 'Error saving message to database';
+ chatHistory.appendChild(errorMessage);
+ }
+
+ // Scroll to bottom
+ chatHistory.scrollTop = chatHistory.scrollHeight;
+ });