|
1 | | -const form = document.getElementById("chat-form"); |
2 | | -const textarea = document.getElementById("message"); |
3 | | -const chatContainer = document.getElementById("chat-container"); |
4 | | -const statusDiv = document.getElementById("status"); |
| 1 | +document.addEventListener("DOMContentLoaded", function () { |
| 2 | + const form = document.getElementById("chat-form"); |
| 3 | + const input = document.getElementById("user-input"); |
| 4 | + const chatBox = document.getElementById("chat-box"); |
5 | 5 |
|
6 | | -form.addEventListener("submit", async function (e) { |
7 | | - e.preventDefault(); |
8 | | - await submitForm(); |
9 | | -}); |
10 | | - |
11 | | -textarea.addEventListener("keydown", function (e) { |
12 | | - if (e.key === "Enter" && !e.shiftKey) { |
| 6 | + form.addEventListener("submit", async function (e) { |
13 | 7 | e.preventDefault(); |
14 | | - submitForm(); |
15 | | - } |
16 | | -}); |
17 | | - |
18 | | -async function submitForm() { |
19 | | - const message = textarea.value.trim(); |
20 | | - if (!message) return; |
21 | | - |
22 | | - appendMessage("user", message); |
23 | | - textarea.value = ""; |
24 | | - |
25 | | - statusDiv.textContent = "AI is aan het typen..."; |
26 | | - |
27 | | - try { |
28 | | - const response = await fetch("https://chatproxy.azurewebsites.net/api/chatproxy", { |
29 | | - method: "POST", |
30 | | - headers: { |
31 | | - "Content-Type": "application/json", |
32 | | - }, |
33 | | - body: JSON.stringify({ message: message }) |
34 | | - }); |
35 | | - |
36 | | - const data = await response.text(); |
37 | | - |
38 | | - if (!response.ok) { |
39 | | - throw new Error(`Serverfout: ${data}`); |
| 8 | + const userMessage = input.value.trim(); |
| 9 | + if (!userMessage) return; |
| 10 | + |
| 11 | + appendMessage("user", userMessage); |
| 12 | + input.value = ""; |
| 13 | + |
| 14 | + try { |
| 15 | + const response = await fetch("https://chatproxy.azurewebsites.net/api/chatproxy", { |
| 16 | + method: "POST", |
| 17 | + headers: { |
| 18 | + "Content-Type": "application/json" |
| 19 | + }, |
| 20 | + body: JSON.stringify({ message: userMessage }) |
| 21 | + }); |
| 22 | + |
| 23 | + if (!response.ok) { |
| 24 | + throw new Error(`Serverfout: ${response.status}`); |
| 25 | + } |
| 26 | + |
| 27 | + const text = await response.text(); |
| 28 | + appendMessage("assistant", text); |
| 29 | + } catch (error) { |
| 30 | + console.error("Fout in fetch:", error); |
| 31 | + appendMessage("assistant", "⚠️ Er ging iets mis bij het ophalen van het antwoord."); |
40 | 32 | } |
41 | | - |
42 | | - appendMessage("assistant", data); |
43 | | - } catch (error) { |
44 | | - appendMessage("assistant", `Fout: ${error.message}`); |
45 | | - } finally { |
46 | | - statusDiv.textContent = ""; |
| 33 | + }); |
| 34 | + |
| 35 | + function appendMessage(role, text) { |
| 36 | + const messageElem = document.createElement("div"); |
| 37 | + messageElem.className = role === "user" ? "user-message" : "assistant-message"; |
| 38 | + messageElem.innerText = text; |
| 39 | + chatBox.appendChild(messageElem); |
| 40 | + chatBox.scrollTop = chatBox.scrollHeight; |
47 | 41 | } |
48 | | -} |
49 | | - |
50 | | -function appendMessage(role, text) { |
51 | | - const messageDiv = document.createElement("div"); |
52 | | - messageDiv.className = role; |
53 | | - messageDiv.textContent = text; |
54 | | - chatContainer.appendChild(messageDiv); |
55 | | - chatContainer.scrollTop = chatContainer.scrollHeight; |
56 | | -} |
| 42 | +}); |
0 commit comments