Skip to content

Commit e07bbfa

Browse files
authored
Create main.js
1 parent 8282b64 commit e07bbfa

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

public/js/main.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function onSubmit(event) {
2+
event.preventDefault();
3+
4+
document.querySelector('.result').textContent = '';
5+
6+
const prompt = document.querySelector('#prompt').value;
7+
8+
if (prompt === '') {
9+
alert('Please add some text!');
10+
return;
11+
}
12+
13+
generateQueryRequest(prompt);
14+
}
15+
16+
async function generateQueryRequest(prompt) {
17+
try {
18+
const response = await fetch('/openai/generatesqlquery', {
19+
method: "POST",
20+
headers: {
21+
"Content-Type": "application/json",
22+
},
23+
body: JSON.stringify({ prompt }),
24+
});
25+
26+
if (!response.ok) {
27+
throw new Error('That query could not be generated');
28+
}
29+
30+
const data = await response.json();
31+
32+
const SQLQuery = data.result;
33+
34+
document.querySelector('.result').textContent = SQLQuery;
35+
} catch (error) {
36+
document.querySelector('.result').textContent = error;
37+
}
38+
}
39+
40+
document.querySelector('#input-form').addEventListener('submit', onSubmit);

0 commit comments

Comments
 (0)