|
1 | 1 | import { useRef, useState } from "react";
|
2 | 2 | import Editor from "@monaco-editor/react";
|
3 |
| -import CodeEditorDropdown from "./Dropdown"; |
| 3 | +// import CodeEditorDropdown from "./Dropdown"; |
4 | 4 | import CodeEditorButton from "./Button";
|
5 | 5 | import MenuDropdown from "../MenuDropdown/MenuDropdown";
|
6 | 6 |
|
7 | 7 | const languageCommentsMap = new Map([
|
8 |
| - ["javascript", "// Paste/Enter your code here"], |
9 |
| - ["python", "# Paste/Enter your code here"], |
10 |
| - ["java", "// Paste/Enter your code here"], |
11 |
| - ["html", "<!-- Paste/Enter your code here -->"], |
12 |
| - ["css", "/* Paste/Enter your code here */"], |
13 |
| - ["ruby", "# Paste/Enter your code here"], |
14 |
| - // Add more languages as needed |
| 8 | + ["javascript", "// Paste/Enter your code here"], |
| 9 | + ["python", "# Paste/Enter your code here"], |
| 10 | + ["java", "// Paste/Enter your code here"], |
| 11 | + ["html", "<!-- Paste/Enter your code here -->"], |
| 12 | + ["css", "/* Paste/Enter your code here */"], |
| 13 | + ["ruby", "# Paste/Enter your code here"], |
| 14 | + // Add more languages as needed |
15 | 15 | ]);
|
16 | 16 |
|
17 | 17 | const CodeEditor = ({ defaultCode }) => {
|
18 |
| - const editorRef = useRef(null); |
19 |
| - const [language, setLanguage] = useState("python"); |
20 |
| - const [code, setCode] = useState(defaultCode); |
| 18 | + const editorRef = useRef(null); |
| 19 | + const [language, setLanguage] = useState("Python"); |
| 20 | + const [code, setCode] = useState(defaultCode); |
21 | 21 |
|
22 |
| - function handleEditorChange(value, event) { |
23 |
| - // here is the current value |
24 |
| - console.log(value); |
25 |
| - console.log(event); |
26 |
| - } |
| 22 | + function handleEditorDidMount(editor, monaco) { |
| 23 | + editorRef.current = editor; |
| 24 | + console.log("onMount: the editor instance:", editor); |
| 25 | + console.log("onMount: the monaco instance:", monaco); |
| 26 | + } |
27 | 27 |
|
28 |
| - function handleEditorDidMount(editor, monaco) { |
29 |
| - editorRef.current = editor; |
30 |
| - console.log("onMount: the editor instance:", editor); |
31 |
| - console.log("onMount: the monaco instance:", monaco); |
32 |
| - } |
| 28 | + function handleEditorWillMount(monaco) { |
| 29 | + console.log("beforeMount: the monaco instance:", monaco); |
| 30 | + } |
33 | 31 |
|
34 |
| - function handleEditorWillMount(monaco) { |
35 |
| - console.log("beforeMount: the monaco instance:", monaco); |
36 |
| - } |
| 32 | + function handleEditorValidation(markers) { |
| 33 | + // model markers |
| 34 | + markers.forEach((marker) => console.log("onValidate:", marker.message)); |
| 35 | + } |
37 | 36 |
|
38 |
| - function handleEditorValidation(markers) { |
39 |
| - // model markers |
40 |
| - markers.forEach((marker) => console.log("onValidate:", marker.message)); |
41 |
| - } |
| 37 | + function removeWordFromStart(inputString, targetWord) { |
| 38 | + if (inputString.startsWith(targetWord)) { |
| 39 | + return inputString.slice(targetWord.length).trim(); |
| 40 | + } |
| 41 | + return inputString; |
| 42 | + } |
42 | 43 |
|
43 |
| - // for submit later |
44 |
| - function showValue() { |
45 |
| - alert(editorRef.current.getValue()); |
46 |
| - } |
| 44 | + // handle when user clicks on "Generate" button |
| 45 | + function handleGenerate() { |
| 46 | + let content = removeWordFromStart(code, languageCommentsMap.get(language)); |
47 | 47 |
|
48 |
| - function handleDropdownClick(choice) { |
49 |
| - console.log(`Clicked on dropdown:`, choice); |
50 |
| - setLanguage(choice); |
51 |
| - setCode(languageCommentsMap.get(choice)); |
52 |
| - } |
| 48 | + const url = "http://localhost:5000/openai"; |
| 49 | + const payload = { |
| 50 | + language: language, |
| 51 | + content: content, |
| 52 | + }; |
| 53 | + console.log("Payload:", payload); |
53 | 54 |
|
54 |
| - return ( |
55 |
| - <> |
56 |
| - <CodeEditorDropdown |
57 |
| - handleClick={handleDropdownClick} |
58 |
| - activeLanguage={language} |
59 |
| - /> |
| 55 | + fetch(url, { |
| 56 | + method: "POST", |
| 57 | + headers: { |
| 58 | + "Content-Type": "application/json", |
| 59 | + }, |
| 60 | + body: JSON.stringify(payload), // Convert the data to JSON format |
| 61 | + }) |
| 62 | + .then((response) => { |
| 63 | + if (!response.ok) { |
| 64 | + throw new Error(`HTTP error! Status: ${response.status}`); |
| 65 | + } |
| 66 | + return response.json(); // Parse the response body as JSON |
| 67 | + }) |
| 68 | + .then((data) => { |
| 69 | + console.log("Success:", data); |
| 70 | + }) |
| 71 | + .catch((error) => { |
| 72 | + console.error("Error:", error); |
| 73 | + }); |
| 74 | + } |
60 | 75 |
|
61 |
| - <MenuDropdown |
62 |
| - title={"Language"} |
63 |
| - replaceTitle={true} |
64 |
| - items={[ |
65 |
| - { label: "JavaScript", action: () => {} }, |
66 |
| - { label: "Python", action: () => {} }, |
67 |
| - { label: "CPP", action: () => {} }, |
68 |
| - ]} |
69 |
| - /> |
| 76 | + function handleDropdownClick(choice) { |
| 77 | + console.log(`Clicked on dropdown:`, choice); |
| 78 | + setLanguage(choice); |
| 79 | + setCode(languageCommentsMap.get(choice)); |
| 80 | + } |
70 | 81 |
|
71 |
| - <Editor |
72 |
| - height="60vh" |
73 |
| - theme="vs-dark" |
74 |
| - language={language} |
75 |
| - defaultLanguage="python" |
76 |
| - defaultValue={defaultCode} |
77 |
| - value={code} |
78 |
| - onChange={handleEditorChange} |
79 |
| - onMount={handleEditorDidMount} |
80 |
| - beforeMount={handleEditorWillMount} |
81 |
| - onValidate={handleEditorValidation} |
82 |
| - /> |
| 82 | + return ( |
| 83 | + <> |
| 84 | + <div style={{ marginBottom: "1rem" }}> |
| 85 | + <MenuDropdown |
| 86 | + title={language} |
| 87 | + replaceTitle={true} |
| 88 | + items={[ |
| 89 | + { |
| 90 | + label: "JavaScript", |
| 91 | + action: () => handleDropdownClick("javascript"), |
| 92 | + }, |
| 93 | + { |
| 94 | + label: "Python", |
| 95 | + action: () => handleDropdownClick("python"), |
| 96 | + }, |
| 97 | + ]} |
| 98 | + /> |
| 99 | + </div> |
83 | 100 |
|
84 |
| - <CodeEditorButton /> |
85 |
| - </> |
86 |
| - ); |
| 101 | + <Editor |
| 102 | + height="60vh" |
| 103 | + theme="vs-dark" |
| 104 | + language={language} |
| 105 | + defaultLanguage="python" |
| 106 | + defaultValue={defaultCode} |
| 107 | + value={code} |
| 108 | + onChange={(v, e) => setCode(v)} |
| 109 | + onMount={handleEditorDidMount} |
| 110 | + beforeMount={handleEditorWillMount} |
| 111 | + onValidate={handleEditorValidation} |
| 112 | + /> |
| 113 | + |
| 114 | + <CodeEditorButton handleClick={handleGenerate} /> |
| 115 | + </> |
| 116 | + ); |
87 | 117 | };
|
88 | 118 |
|
89 | 119 | export default CodeEditor;
|
0 commit comments