|
1 | | -import styles from './index.module.css' |
2 | | -import sqlLogo from './assets/sql-logo.png' |
3 | | -import { useState } from 'react' |
| 1 | +import styles from './index.module.css'; |
| 2 | +import sqlLogo from './assets/sql-logo.png'; |
| 3 | +import { useState } from 'react'; |
4 | 4 |
|
5 | 5 | function App() { |
6 | | -//Declares a state variable called queryDescription and a function to update it called setQueryDescription |
7 | | -//useState is a hook that allows you to add state to a functional component |
8 | | -//The initial value of queryDescription is an empty string |
9 | | -//The useState function returns an array with two elements: the current state value and a function to update it |
10 | | -// syntax : const [state, setState] = useState(initialState) |
| 6 | + const [queryDescription, setQueryDescription] = useState(""); |
| 7 | + const [sqlQuery, setSqlQuery] = useState(""); |
11 | 8 |
|
12 | | -const {queryDescription,setQueryDescription} = useState("") |
| 9 | + const onSubmit = async (e) => { |
| 10 | + e.preventDefault(); |
| 11 | + console.log('Form submitted:', queryDescription); |
13 | 12 |
|
14 | | -const onSubmit = (e) =>{ |
15 | | - e.preventDefault() //this prevents the default behavior of the form submission |
16 | | - console.log('Form submitted:', queryDescription ) |
| 13 | + const cleanSql = sqlQuery.replace(/```sql([\s\S]*?)```/i, (m, p1) => p1.trim()) || sqlQuery; |
| 14 | + setSqlQuery(cleanSql); |
17 | 15 |
|
18 | 16 |
|
19 | | -} |
| 17 | + console.log('SQL Query returned from Server:', generatedQuery); |
| 18 | + }; |
| 19 | + |
| 20 | + const generateQuery = async () => { |
| 21 | + const response = await fetch("http://localhost:3005/generate", { |
| 22 | + method: "POST", |
| 23 | + headers: { |
| 24 | + "Content-Type": "application/json", |
| 25 | + }, |
| 26 | + body: JSON.stringify({ |
| 27 | + queryDescription: queryDescription, |
| 28 | + }), |
| 29 | + }); |
| 30 | + |
| 31 | + const data = await response.json(); |
| 32 | + return data.response.trim(); |
| 33 | + }; |
| 34 | + |
20 | 35 | return ( |
21 | | - // below is the main component of the app |
22 | | - // it is the main entry point of the app |
23 | | - //we are importing the main component from the index.module.css file |
24 | | - <main className={styles.main}> |
25 | | - |
26 | | - <img src = {sqlLogo} alt=""className = {styles.icon} /> |
27 | | - <h3>Generate SQL with AI</h3> |
28 | | - |
29 | | - <form onSubmit={onSubmit}> |
30 | | - <input |
31 | | - type="text" |
32 | | - name = "query-description" |
33 | | - placeholder="Describe your query" |
34 | | - //onchange is an event handler that is called when the value of the input changes |
35 | | - //it takes a function as an argument |
36 | | - //the function takes an event object as an argument |
37 | | - //the event object has a target property that is the input element |
38 | | - //the value of the input is set to the queryDescription state variable |
39 | | - //syntax : <input onChange={handleChangeFunction} > |
40 | | - onChange = {(e) => setQueryDescription(e.target.value)} |
41 | | - /> |
42 | | - |
43 | | - <input type="submit" value="Generate SQL Query" /> |
44 | | - </form> |
45 | | - </main> |
46 | | - ) |
| 36 | + <main className={styles.main}> |
| 37 | + <img src={sqlLogo} alt="SQL Logo" className={styles.icon} /> |
| 38 | + <h3>Generate SQL with AI</h3> |
| 39 | + |
| 40 | + <form onSubmit={onSubmit}> |
| 41 | + <input |
| 42 | + type="text" |
| 43 | + name="query-description" |
| 44 | + placeholder="Describe your query" |
| 45 | + onChange={(e) => setQueryDescription(e.target.value)} |
| 46 | + /> |
| 47 | + <input type="submit" value="Generate SQL Query" /> |
| 48 | + </form> |
| 49 | + |
| 50 | + {/* this code right below is to display SQL result */} |
| 51 | + {sqlQuery && ( |
| 52 | + <div className={styles.codeContainer}> |
| 53 | + <pre className={styles.codeBlock}>{sqlQuery}</pre> |
| 54 | + <button |
| 55 | + className={styles.copyButton} |
| 56 | + onClick={() => navigator.clipboard.writeText(sqlQuery)} |
| 57 | + > |
| 58 | + Copy |
| 59 | + </button> |
| 60 | + </div> |
| 61 | + |
| 62 | + |
| 63 | + )} |
| 64 | + </main> |
| 65 | + ); |
47 | 66 | } |
48 | 67 |
|
49 | | -export default App |
| 68 | +export default App; |
0 commit comments