File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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 ) ;
You can’t perform that action at this time.
0 commit comments