|
1 | | -const chat = document.getElementById("chat"); |
2 | | -const form = document.getElementById("input-form"); |
3 | | -const input = document.getElementById("user-input"); |
| 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"); |
4 | 5 |
|
5 | | -form.addEventListener("submit", async (e) => { |
| 6 | +form.addEventListener("submit", async function (e) { |
6 | 7 | e.preventDefault(); |
7 | | - const message = input.value.trim(); |
| 8 | + await submitForm(); |
| 9 | +}); |
| 10 | + |
| 11 | +textarea.addEventListener("keydown", function (e) { |
| 12 | + if (e.key === "Enter" && !e.shiftKey) { |
| 13 | + e.preventDefault(); |
| 14 | + submitForm(); |
| 15 | + } |
| 16 | +}); |
| 17 | + |
| 18 | +async function submitForm() { |
| 19 | + const message = textarea.value.trim(); |
8 | 20 | if (!message) return; |
9 | 21 |
|
10 | | - appendMessage("Gebruiker", message); |
11 | | - input.value = ""; |
| 22 | + appendMessage("user", message); |
| 23 | + textarea.value = ""; |
| 24 | + |
| 25 | + statusDiv.textContent = "AI is aan het typen..."; |
12 | 26 |
|
13 | 27 | try { |
14 | 28 | const response = await fetch("https://chatproxy.azurewebsites.net/api/chatproxy", { |
15 | 29 | method: "POST", |
16 | | - headers: { "Content-Type": "application/json" }, |
17 | | - body: JSON.stringify({ message }) |
| 30 | + headers: { |
| 31 | + "Content-Type": "application/json", |
| 32 | + }, |
| 33 | + body: JSON.stringify({ message: message }) |
18 | 34 | }); |
19 | 35 |
|
| 36 | + const data = await response.text(); |
| 37 | + |
20 | 38 | if (!response.ok) { |
21 | | - const errorText = await response.text(); |
22 | | - console.error("Responsetekst:", errorText); |
23 | | - throw new Error(`Serverfout: ${response.status}`); |
| 39 | + throw new Error(`Serverfout: ${data}`); |
24 | 40 | } |
25 | 41 |
|
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); |
| 42 | + appendMessage("assistant", data); |
| 43 | + } catch (error) { |
| 44 | + appendMessage("assistant", `Fout: ${error.message}`); |
| 45 | + } finally { |
| 46 | + statusDiv.textContent = ""; |
31 | 47 | } |
32 | | -}); |
| 48 | +} |
33 | 49 |
|
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; |
| 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; |
39 | 56 | } |
0 commit comments