11import { Disclosure , Menu , Transition } from '@headlessui/react' ;
22import { BellIcon , MenuIcon , XIcon } from '@heroicons/react/outline' ;
33import 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
69const navigation = [ 'Dashboard' , 'Team' , 'Projects' , 'Calendar' , 'Reports' ] ;
710const profile = [ 'Your Profile' , 'Settings' , 'Sign out' ] ;
@@ -11,9 +14,55 @@ function classNames(...classes: any[]) {
1114}
1215
1316export 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 >
0 commit comments