|
1 | | -const chat = document.getElementById("chat"); |
2 | | -const form = document.getElementById("input-form"); |
3 | | -const input = document.getElementById("user-input"); |
| 1 | +// script.js - Werkende basisversie met minimale aanpassingen voor UX |
4 | 2 |
|
5 | | -form.addEventListener("submit", async (e) => { |
6 | | - e.preventDefault(); |
7 | | - const message = input.value.trim(); |
8 | | - if (!message) return; |
| 3 | +document.addEventListener("DOMContentLoaded", function () { |
| 4 | + const form = document.getElementById("chat-form"); |
| 5 | + const input = document.getElementById("user-input"); |
| 6 | + const chatBox = document.getElementById("chat-box"); |
9 | 7 |
|
10 | | - appendMessage("Gebruiker", message); |
11 | | - input.value = ""; |
| 8 | + function appendMessage(sender, text) { |
| 9 | + const messageElem = document.createElement("div"); |
| 10 | + messageElem.className = sender === "user" ? "user-message" : "bot-message"; |
| 11 | + messageElem.textContent = text; |
| 12 | + chatBox.appendChild(messageElem); |
| 13 | + chatBox.scrollTop = chatBox.scrollHeight; |
| 14 | + } |
12 | 15 |
|
13 | | - try { |
14 | | - const response = await fetch("https://chatproxy.azurewebsites.net/api/chatproxy", { |
15 | | - method: "POST", |
16 | | - headers: { "Content-Type": "application/json" }, |
17 | | - body: JSON.stringify({ message }) |
18 | | - }); |
| 16 | + form.addEventListener("submit", async function (event) { |
| 17 | + event.preventDefault(); |
| 18 | + const userInput = input.value.trim(); |
| 19 | + if (!userInput) return; |
19 | 20 |
|
20 | | - if (!response.ok) { |
21 | | - const errorText = await response.text(); |
22 | | - console.error("Responsetekst:", errorText); |
23 | | - throw new Error(`Serverfout: ${response.status}`); |
24 | | - } |
| 21 | + appendMessage("user", userInput); |
| 22 | + input.value = ""; |
25 | 23 |
|
26 | | - const text = await response.text(); |
27 | | - appendMessage("Agent", text); |
28 | | - } catch (err) { |
29 | | - appendMessage("Agent", "Er ging iets mis."); |
30 | | - console.error("Fout in fetch:", err); |
31 | | - } |
32 | | -}); |
| 24 | + try { |
| 25 | + const response = await fetch("https://chatproxy.azurewebsites.net/api/chatproxy", { |
| 26 | + method: "POST", |
| 27 | + headers: { "Content-Type": "application/json" }, |
| 28 | + body: JSON.stringify({ message: userInput }) |
| 29 | + }); |
33 | 30 |
|
34 | | -function appendMessage(sender, text) { |
35 | | - const msg = document.createElement("div"); |
36 | | - msg.innerHTML = `<div class="user">${sender}:</div><div class="agent">${text}</div>`; |
37 | | - chat.appendChild(msg); |
38 | | - chat.scrollTop = chat.scrollHeight; |
39 | | -} |
| 31 | + const result = await response.text(); |
| 32 | + |
| 33 | + if (!response.ok) { |
| 34 | + appendMessage("bot", "(Fout): " + result); |
| 35 | + } else { |
| 36 | + appendMessage("bot", result); |
| 37 | + } |
| 38 | + } catch (error) { |
| 39 | + appendMessage("bot", "(Fout bij ophalen): " + error.message); |
| 40 | + } |
| 41 | + }); |
| 42 | +}); |
0 commit comments