@@ -6,135 +6,136 @@ import "./COdeEditor.css";
6
6
import QueryButtons from "../QueryButtons/QueryButtons" ;
7
7
8
8
const languageCommentsMap = new Map ( [
9
- [ "javascript" , "// Paste/Enter your code here" ] ,
10
- [ "python" , "# Paste/Enter your code here" ] ,
11
- [ "java" , "// Paste/Enter your code here" ] ,
12
- [ "html" , "<!-- Paste/Enter your code here -->" ] ,
13
- [ "css" , "/* Paste/Enter your code here */" ] ,
14
- [ "ruby" , "# Paste/Enter your code here" ] ,
15
- // Add more languages as needed
9
+ [ "javascript" , "// Paste/Enter your code here" ] ,
10
+ [ "python" , "# Paste/Enter your code here" ] ,
11
+ [ "java" , "// Paste/Enter your code here" ] ,
12
+ [ "html" , "<!-- Paste/Enter your code here -->" ] ,
13
+ [ "css" , "/* Paste/Enter your code here */" ] ,
14
+ [ "ruby" , "# Paste/Enter your code here" ] ,
15
+ // Add more languages as needed
16
16
] ) ;
17
17
18
18
export default function CodeEditor ( { defaultCode } ) {
19
- const editorRef = useRef ( null ) ;
20
- const dispatch = useDispatch ( ) ;
21
- const [ language , setLanguage ] = useState ( "Python" ) ;
22
- const [ code , setCode ] = useState ( defaultCode ) ;
19
+ const editorRef = useRef ( null ) ;
20
+ const dispatch = useDispatch ( ) ;
21
+ const [ language , setLanguage ] = useState ( "Python" ) ;
22
+ const [ code , setCode ] = useState ( defaultCode ) ;
23
23
24
- function handleEditorDidMount ( editor , monaco ) {
25
- editorRef . current = editor ;
26
- console . log ( "onMount: the editor instance:" , editor ) ;
27
- console . log ( "onMount: the monaco instance:" , monaco ) ;
28
- }
24
+ function handleEditorDidMount ( editor , monaco ) {
25
+ editorRef . current = editor ;
26
+ console . log ( "onMount: the editor instance:" , editor ) ;
27
+ console . log ( "onMount: the monaco instance:" , monaco ) ;
28
+ }
29
29
30
- function handleEditorWillMount ( monaco ) {
31
- console . log ( "beforeMount: the monaco instance:" , monaco ) ;
32
- }
30
+ function handleEditorWillMount ( monaco ) {
31
+ console . log ( "beforeMount: the monaco instance:" , monaco ) ;
32
+ }
33
33
34
- function handleEditorValidation ( markers ) {
35
- // model markers
36
- markers . forEach ( ( marker ) => console . log ( "onValidate:" , marker . message ) ) ;
37
- }
34
+ function handleEditorValidation ( markers ) {
35
+ // model markers
36
+ markers . forEach ( ( marker ) => console . log ( "onValidate:" , marker . message ) ) ;
37
+ }
38
38
39
- function removeWordFromStart ( inputString , targetWord ) {
40
- if ( inputString . startsWith ( targetWord ) ) {
41
- return inputString . slice ( targetWord . length ) . trim ( ) ;
42
- }
43
- return inputString ;
44
- }
39
+ function removeWordFromStart ( inputString , targetWord ) {
40
+ if ( inputString . startsWith ( targetWord ) ) {
41
+ return inputString . slice ( targetWord . length ) . trim ( ) ;
42
+ }
43
+ return inputString ;
44
+ }
45
45
46
- function sanitizeInput ( inputString ) {
47
- const cleanedString = removeWordFromStart (
48
- inputString ,
49
- languageCommentsMap . get ( language )
50
- ) ;
51
- // Using the replace method with a regular expression to replace all occurrences
52
- const sanitizedString = cleanedString . replace ( / ' / g, '"' ) ;
53
- return sanitizedString ;
54
- }
46
+ function sanitizeInput ( inputString ) {
47
+ const cleanedString = removeWordFromStart (
48
+ inputString ,
49
+ languageCommentsMap . get ( language )
50
+ ) ;
51
+ // Using the replace method with a regular expression to replace all occurrences
52
+ const sanitizedString = cleanedString . replace ( / ' / g, '"' ) ;
53
+ return sanitizedString ;
54
+ }
55
55
56
- // handle when user clicks on "Generate" button
57
- function handleGenerate ( ) {
58
- let content = sanitizeInput ( code ) ;
56
+ // handle when user clicks on "Generate" button
57
+ function handleGenerate ( ) {
58
+ let content = sanitizeInput ( code ) ;
59
59
60
- const url = "http://localhost:5000/openai" ;
61
- const payload = {
62
- language : language ,
63
- content : content ,
64
- } ;
65
- console . log ( "Payload:" , payload ) ;
60
+ const url = "http://localhost:5000/openai" ;
61
+ const payload = {
62
+ language : language ,
63
+ content : content ,
64
+ type : "code" ,
65
+ } ;
66
+ console . log ( "Payload:" , payload ) ;
66
67
67
- dispatch ( { type : "SET_LOADING" , payload : true } ) ;
68
- fetch ( url , {
69
- method : "POST" ,
70
- headers : {
71
- "Content-Type" : "application/json" ,
72
- } ,
73
- body : JSON . stringify ( payload ) , // Convert the data to JSON format
74
- } )
75
- . then ( ( response ) => {
76
- if ( ! response . ok ) {
77
- throw new Error ( `HTTP error! Status: ${ response . status } ` ) ;
78
- }
79
- return response . json ( ) ; // Parse the response body as JSON
80
- } )
81
- . then ( ( data ) => {
82
- console . log ( "Query success:" , data ) ;
83
- dispatch ( {
84
- type : "DISPLAY_CODE_OUTPUT" ,
85
- payload : {
86
- code : data . response . code ,
87
- language : "c" ,
88
- } ,
89
- } ) ;
90
- dispatch ( { type : "SET_LOADING" , payload : false } ) ;
91
- } )
92
- . catch ( ( error ) => {
93
- console . error ( "Error fetching response:" , error ) ;
94
- dispatch ( { type : "SET_LOADING" , payload : false } ) ;
95
- } ) ;
96
- }
68
+ dispatch ( { type : "SET_LOADING" , payload : true } ) ;
69
+ fetch ( url , {
70
+ method : "POST" ,
71
+ headers : {
72
+ "Content-Type" : "application/json" ,
73
+ } ,
74
+ body : JSON . stringify ( payload ) , // Convert the data to JSON format
75
+ } )
76
+ . then ( ( response ) => {
77
+ if ( ! response . ok ) {
78
+ throw new Error ( `HTTP error! Status: ${ response . status } ` ) ;
79
+ }
80
+ return response . json ( ) ; // Parse the response body as JSON
81
+ } )
82
+ . then ( ( data ) => {
83
+ console . log ( "Query success:" , data ) ;
84
+ dispatch ( {
85
+ type : "DISPLAY_CODE_OUTPUT" ,
86
+ payload : {
87
+ code : data . response . code ,
88
+ language : "c" ,
89
+ } ,
90
+ } ) ;
91
+ dispatch ( { type : "SET_LOADING" , payload : false } ) ;
92
+ } )
93
+ . catch ( ( error ) => {
94
+ console . error ( "Error fetching response:" , error ) ;
95
+ dispatch ( { type : "SET_LOADING" , payload : false } ) ;
96
+ } ) ;
97
+ }
97
98
98
- function handleDropdownClick ( choice ) {
99
- console . log ( `Clicked on dropdown:` , choice ) ;
100
- setLanguage ( choice ) ;
101
- setCode ( languageCommentsMap . get ( choice ) ) ;
102
- }
99
+ function handleDropdownClick ( choice ) {
100
+ console . log ( `Clicked on dropdown:` , choice ) ;
101
+ setLanguage ( choice ) ;
102
+ setCode ( languageCommentsMap . get ( choice ) ) ;
103
+ }
103
104
104
- return (
105
- < >
106
- < div style = { { marginBottom : "1rem" } } >
107
- < MenuDropdown
108
- title = { language }
109
- replaceTitle = { true }
110
- items = { [
111
- {
112
- label : "JavaScript" ,
113
- action : ( ) => handleDropdownClick ( "javascript" ) ,
114
- } ,
115
- {
116
- label : "Python" ,
117
- action : ( ) => handleDropdownClick ( "python" ) ,
118
- } ,
119
- ] }
120
- />
121
- </ div >
105
+ return (
106
+ < >
107
+ < div style = { { marginBottom : "1rem" } } >
108
+ < MenuDropdown
109
+ title = { language }
110
+ replaceTitle = { true }
111
+ items = { [
112
+ {
113
+ label : "JavaScript" ,
114
+ action : ( ) => handleDropdownClick ( "javascript" ) ,
115
+ } ,
116
+ {
117
+ label : "Python" ,
118
+ action : ( ) => handleDropdownClick ( "python" ) ,
119
+ } ,
120
+ ] }
121
+ />
122
+ </ div >
122
123
123
- < Editor
124
- height = "65vh"
125
- theme = "vs-dark"
126
- className = "container"
127
- language = { language }
128
- defaultLanguage = "python"
129
- defaultValue = { defaultCode }
130
- value = { code }
131
- onChange = { ( v , e ) => setCode ( v ) }
132
- onMount = { handleEditorDidMount }
133
- beforeMount = { handleEditorWillMount }
134
- onValidate = { handleEditorValidation }
135
- />
124
+ < Editor
125
+ height = "65vh"
126
+ theme = "vs-dark"
127
+ className = "container"
128
+ language = { language }
129
+ defaultLanguage = "python"
130
+ defaultValue = { defaultCode }
131
+ value = { code }
132
+ onChange = { ( v , e ) => setCode ( v ) }
133
+ onMount = { handleEditorDidMount }
134
+ beforeMount = { handleEditorWillMount }
135
+ onValidate = { handleEditorValidation }
136
+ />
136
137
137
- < QueryButtons handleClick = { handleGenerate } > </ QueryButtons >
138
- </ >
139
- ) ;
138
+ < QueryButtons handleClick = { handleGenerate } > </ QueryButtons >
139
+ </ >
140
+ ) ;
140
141
}
0 commit comments