38676-vm/assets/js/main.js
2026-03-17 01:52:50 +00:00

66 lines
2.3 KiB
JavaScript

async function loadProfile(userId) {
console.log("loadProfile called for user:", userId);
const modal = document.getElementById('profileModal');
const content = document.getElementById('profileModalContent');
if (!modal || !content) {
console.error("Profile modal elements not found");
return;
}
try {
const response = await fetch('api/get_profile.php?user_id=' + userId);
const data = await response.json();
if (data.html) {
content.innerHTML = data.html;
modal.style.display = 'flex';
} else {
console.error("No HTML returned from profile API");
}
} catch (error) {
console.error('Error loading profile:', error);
}
}
document.addEventListener('DOMContentLoaded', () => {
const chatForm = document.getElementById('chat-form');
const chatInput = document.getElementById('chat-input');
const chatMessages = document.getElementById('chat-messages');
const appendMessage = (text, sender) => {
if (!chatMessages) return;
const msgDiv = document.createElement('div');
msgDiv.classList.add('message', sender);
msgDiv.textContent = text;
chatMessages.appendChild(msgDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
};
if (chatForm) {
chatForm.addEventListener('submit', async (e) => {
e.preventDefault();
const message = chatInput.value.trim();
if (!message) return;
appendMessage(message, 'visitor');
chatInput.value = '';
try {
const response = await fetch('api/chat.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message })
});
const data = await response.json();
// Artificial delay for realism
setTimeout(() => {
appendMessage(data.reply, 'bot');
}, 500);
} catch (error) {
console.error('Error:', error);
appendMessage("Désolé, une erreur est survenue. Veuillez réessayer.", 'bot');
}
});
}
});