-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
48 lines (39 loc) · 1.62 KB
/
main.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
import { callApi } from "./api-requests.js";
const answer = document.getElementById("answer");
const question = document.getElementById("question");
let questionList = document.querySelector(".questionList");
let userInput = document.getElementById("input");
let history = document.querySelector(".history")
export default async function formSubmit(e) {
e.preventDefault();
console.log("stringSubmitted");
callApi(userInput.value).then((response) => {
// console.log("userInput inside call", userInput.value);
// console.log("main.js callApi response", response);
let listItem = document.createElement("li");
listItem.classList = "historicItem";
let newQuestion = document.createElement("p");
newQuestion.classList = "newQuestion";
newQuestion.innerHTML = userInput.value;
userInput.value = "";
let newAnswer = document.createElement("p");
newAnswer.classList = "newAnswer";
newAnswer.innerHTML = response.choices[0].message.content;
listItem.appendChild(newQuestion);
listItem.appendChild(newAnswer);
questionList.prepend(listItem);
history.style.display = "flex"
});
}
document.addEventListener("DOMContentLoaded", function () {
document.querySelector(".form").addEventListener("submit", formSubmit);
});
//placing each historic question and answer into an individual container //
function addToList(content) {
const questionList = document.querySelector('.history .questionList');
const listItem = document.createElement('li');
const div = document.createElement('div');
div.textContent = content;
listItem.appendChild(div);
questionList.appendChild(listItem);
}