-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (42 loc) · 1.47 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const form = document.getElementById("inquiry-form");
const inquiryInput = document.getElementById("inquiry");
const appTypeInput = document.getElementById("app-type");
const idInput = document.getElementById("id");
const chatIdInput = document.getElementById("chat-id");
const conversationListElement = document.getElementById("conversation-list");
const apiEndpoint = "/server/handle-inquiry.php";
form.addEventListener("submit", handleSubmit);
async function handleSubmit(event) {
event.preventDefault();
const { value: inquiry } = inquiryInput;
const { value: appType } = appTypeInput;
const { value: user_id } = idInput;
const { value: chat_id } = chatIdInput;
if (!inquiry) {
alert("Please enter an inquiry");
return;
}
try {
const formData = new URLSearchParams();
formData.append("user_msg", inquiry);
formData.append("app_type", appType);
formData.append("user_id", user_id);
formData.append("chat_id", chat_id);
const response = await fetch(apiEndpoint, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: formData,
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.error);
}
let historyHTML = "";
for (let msg of data.history) {
historyHTML += `<b>${msg.from}:</b> ${msg.message}<br/>`;
}
conversationListElement.innerHTML = historyHTML;
} catch (error) {
alert("Error: " + error.message);
}
}