Skip to content

Commit 843677a

Browse files
committed
Send code from monaco to API + poll for result
1 parent f27df93 commit 843677a

File tree

4 files changed

+84
-16
lines changed

4 files changed

+84
-16
lines changed

package-lock.json

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@types/react": "^17.0.0",
1717
"@types/react-dom": "^17.0.0",
1818
"axios": "^0.21.1",
19+
"monaco-editor": "^0.23.0",
1920
"react": "^17.0.2",
2021
"react-dom": "^17.0.2",
2122
"react-router-dom": "^5.2.0",

src/components/Dashboard.tsx

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { Disclosure, Menu, Transition } from '@headlessui/react';
22
import { BellIcon, MenuIcon, XIcon } from '@heroicons/react/outline';
33
import Editor from '@monaco-editor/react';
4-
import { Fragment } from 'react';
4+
import axios, { AxiosResponse } from 'axios';
5+
import { Fragment, useRef, useState } from 'react';
6+
import useToken from '../utils/useToken';
7+
import Result from './Result';
58

69
const navigation = ['Dashboard', 'Team', 'Projects', 'Calendar', 'Reports'];
710
const profile = ['Your Profile', 'Settings', 'Sign out'];
@@ -11,9 +14,55 @@ function classNames(...classes: any[]) {
1114
}
1215

1316
export default function Example() {
14-
// function showValue() {
15-
// alert(editorRef.current.getValue());
16-
// }
17+
const editorRef: any = useRef<null>(null);
18+
const [jobID, setJobID] = useState('');
19+
const [status, setStatus] = useState('');
20+
const [output, setOutput] = useState('');
21+
const { token } = useToken();
22+
23+
function handleEditorDidMount(editor: any, monaco: any) {
24+
editorRef.current = editor;
25+
}
26+
27+
async function getJobStatus(id: string, timer: NodeJS.Timeout) {
28+
const res: AxiosResponse<{ status: string; output: string }> =
29+
await axios.get(`http://localhost:3000/submissions/${id}`, {
30+
headers: {
31+
Authorization: `Bearer ${token}`,
32+
},
33+
});
34+
setStatus(res.data.status);
35+
setOutput(res.data.output);
36+
if (res.data.status === 'completed') {
37+
clearInterval(timer);
38+
}
39+
}
40+
41+
async function showValue() {
42+
if (editorRef) {
43+
const res: AxiosResponse<{ id: string }> = await axios.post(
44+
'http://localhost:3000/submissions',
45+
{
46+
language: 'cpython3',
47+
code: editorRef.current.getValue(),
48+
},
49+
{
50+
headers: {
51+
Authorization: `Bearer ${token}`,
52+
},
53+
}
54+
);
55+
56+
setJobID(res.data.id);
57+
console.log(res.data.id);
58+
console.log(jobID);
59+
60+
const timer = setInterval(() => {
61+
getJobStatus(res.data.id, timer);
62+
}, 500);
63+
}
64+
}
65+
1766
return (
1867
<div>
1968
<Disclosure as="nav" className="bg-gray-800">
@@ -199,20 +248,22 @@ export default function Example() {
199248
{/* Replace with your content */}
200249
<div className="px-4 py-6 sm:px-0">
201250
<div className="border-4 border-dashed border-gray-200 rounded-lg h-96">
202-
{/* <button onClick={showValue}>Show value</button> */}
203251
<Editor
204-
height="90vh"
252+
onMount={handleEditorDidMount}
253+
height="30vh"
205254
defaultLanguage="python"
206-
defaultValue={`def _clip_grad(clr, grad, group_grad_clip):
207-
if group_grad_clip > 0:
208-
norm = grad.norm(2).item()
209-
if norm > group_grad_clip:
210-
clr *= group_grad_clip / (norm + 1e-10)
211-
return clr`}
255+
defaultValue={`print('hey!')`}
212256
/>
213257
</div>
214258
</div>
215259
{/* /End replace */}
260+
<button
261+
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
262+
onClick={showValue}
263+
>
264+
Run code
265+
</button>
266+
<Result status={status} output={output}></Result>
216267
</div>
217268
</main>
218269
</div>

src/components/Result.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
3+
interface LayoutProps {
4+
status: string;
5+
output?: string;
6+
}
7+
8+
const Result: React.FC<LayoutProps> = ({ status, output }) => {
9+
return (
10+
<div>
11+
<h2>{status}</h2>
12+
<pre>{output}</pre>
13+
</div>
14+
);
15+
};
16+
17+
export default Result;

0 commit comments

Comments
 (0)