-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
57 lines (43 loc) · 1.14 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
49
50
51
52
53
54
55
56
57
function onSubmit(e) {
e.preventDefault();
document.querySelector('.msg').textContent = '';
document.querySelector('#image').src = '';
const prompt = document.querySelector('#promptInput').value;
if (prompt === '') {
alert('Please add some text');
return;
}
generateImageRequest(prompt);
}
async function generateImageRequest(prompt) {
try {
showSpinner();
const response = await fetch('/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt
}),
});
if (!response.ok) {
removeSpinner();
throw new Error('That image could not be generated');
}
const image = await response.json();
const imageUrl = image.data;
document.querySelector('#image').src = imageUrl;
removeSpinner();
} catch (error) {
//print error to front end
document.querySelector('.msg').textContent = error;
}
}
function showSpinner() {
document.querySelector('.spinner').classList.add('show');
}
function removeSpinner() {
document.querySelector('.spinner').classList.remove('show');
}
document.querySelector('#imagePrompt').addEventListener('submit', onSubmit);