-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
47 lines (37 loc) · 1.05 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
import "./style.css";
const form = document.querySelector("form");
form.addEventListener("submit", async (e) => {
e.preventDefault();
showSpinner();
const data = new FormData(form);
const response = await fetch("http://localhost:8080/api/photo", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt: data.get("prompt"),
}),
});
if (response.ok) {
const { image } = await response.json();
const result = document.querySelector("#result");
result.innerHTML = `<img src="${image}" width="512" />`;
} else {
const error = await response.text();
alert(error);
console.log(error);
}
hideSpinner();
});
function showSpinner() {
const button = document.querySelector("button");
button.disabled = true;
button.innerHTML = 'Generating... <span class="spinner">🤖</span>';
}
function hideSpinner() {
const button = document.querySelector("button");
button.disabled = false;
button.innerHTML = "Generate";
}
// write a readme for this project