@@ -6,65 +6,92 @@ import ResultsEditor from '../components/ResultsEditor';
66import { Grid , Button , FormControl , FormControlLabel , Checkbox , Select , MenuItem } from '@material-ui/core' ;
77import code from '../resources/code' ;
88import languages from '../resources/languages' ;
9-
109import './App.css' ;
1110
12- const devPostUrl = 'http://localhost:5000/execute'
13- const postUrl = 'https://dev.dannyhp.com:8443/execute' ;
11+ // Backend servers to execute code.
12+ const DEV_POST_URL = 'http://localhost:5000/execute'
13+ const PROD_POST_URL = 'https://dev.dannyhp.com:8443/execute'
14+ const POST_URL = PROD_POST_URL
15+
16+ // Default settings on page loadup.
17+ const DEFAULT_LANGUAGE = 'java'
18+ const DEFAULT_AUTOCOMPLETE = false
19+ const DEFAULT_PRACTICE = false
20+
21+ // Notification messages.
22+ const EXECUTING_CODE_MESSAGE = 'Running your code...'
23+ const EXECUTING_CODE_ERROR = 'Code cannot be executed. Network connection to server cannot be established.\n'
1424
1525class App extends Component {
1626 constructor ( props ) {
1727 super ( props )
1828
19- // Default language set to Java; AceEditor does not support C++ yet .
20- this . state = this . getInitialState ( ) ;
29+ // Default language set to Java; coderpad now supports C++, Java, and Python .
30+ this . state = this . getInitialState ( )
2131 }
2232
2333 getInitialState = ( ) => {
2434 return ( {
25- language : 'java' ,
35+ language : DEFAULT_LANGUAGE ,
2636 source : { 'java' : code [ 'java' ] , 'python' : code [ 'python' ] , 'c_cpp' : code [ 'c_cpp' ] } ,
2737 results : [ ] ,
2838 disabled : false ,
29- practice : false ,
30- autocomplete : true
39+ practice : DEFAULT_PRACTICE ,
40+ autocomplete : DEFAULT_AUTOCOMPLETE
3141 } )
3242 }
3343
44+ /**
45+ * Changes the current source code.
46+ * @param string value The source code as a string.
47+ */
3448 onChangeCode = ( value ) => {
35- let currentSource = this . state . source ;
36- currentSource [ this . state . language ] = value ;
49+ let currentSource = this . state . source
50+ currentSource [ this . state . language ] = value
3751
3852 this . setState ( {
3953 ...this . state ,
4054 source : currentSource
4155 } )
4256 }
4357
58+ /**
59+ * Changes the programming language.
60+ * @param event event The event of switching select box.
61+ */
4462 onChangeLanguage = ( event ) => {
4563 this . setState ( {
4664 ...this . state ,
4765 language : event . target . value
4866 } )
4967 }
5068
69+ /**
70+ * Enables/disables practice mode.
71+ */
5172 onChangePractice = ( ) => {
5273 this . setState ( {
5374 ...this . state ,
5475 practice : ! this . state . practice
5576 } )
5677 }
5778
79+ /**
80+ * Enables/disables autocomplete.
81+ */
5882 onChangeAutocomplete = ( ) => {
5983 this . setState ( {
6084 ...this . state ,
6185 autocomplete : ! this . state . autocomplete
6286 } )
6387 }
6488
89+ /**
90+ * Adds code executing message and disables run button.
91+ */
6592 setRunningStatus = ( ) => {
66- let currentResults = this . state . results ;
67- currentResults . unshift ( 'Running your code...' )
93+ let currentResults = this . state . results
94+ currentResults . unshift ( EXECUTING_CODE_MESSAGE )
6895
6996 this . setState ( {
7097 ...this . state ,
@@ -73,8 +100,11 @@ class App extends Component {
73100 } )
74101 }
75102
103+ /**
104+ * Removes code executing message and reenables run button.
105+ */
76106 setFinishedStatus = ( ) => {
77- let currentResults = this . state . results ;
107+ let currentResults = this . state . results
78108 currentResults . shift ( )
79109
80110 this . setState ( {
@@ -84,15 +114,18 @@ class App extends Component {
84114 } )
85115 }
86116
117+ /**
118+ * Makes request to backend server and parses code execution results.
119+ */
87120 executeCode = ( ) => {
88121 this . setRunningStatus ( )
89122
90- axios . post ( postUrl , {
123+ axios . post ( POST_URL , {
91124 language : this . state . language ,
92125 code : this . state . source [ this . state . language ] ,
93126 } ) . then ( response => {
94127 const build = response [ 'data' ] [ 'build' ]
95- const error = response [ 'data' ] [ 'error' ]
128+ // const error = response['data']['error']
96129 const run = response [ 'data' ] [ 'run' ]
97130
98131 const result = { build : null , message : null }
@@ -110,7 +143,7 @@ class App extends Component {
110143 this . setFinishedStatus ( )
111144
112145 let currentResults = this . state . results
113- currentResults . unshift ( 'Code cannot be executed. Network connection to server cannot be established.\n' )
146+ currentResults . unshift ( EXECUTING_CODE_ERROR )
114147
115148 this . setState ( {
116149 ...this . state ,
@@ -123,6 +156,9 @@ class App extends Component {
123156 // TODO: Code to download source code.
124157 }
125158
159+ /**
160+ * Adds the result message to the log.
161+ */
126162 addToLog = ( result ) => {
127163 // Before processing the new result, we must pop the message 'Running your code...' off and re-enable the button.
128164 this . setFinishedStatus ( )
@@ -136,9 +172,9 @@ class App extends Component {
136172 }
137173
138174 if ( result [ 'build' ] === true ) {
139- currentResults . unshift ( '[' + languages [ this . state . language ] + '] ' + ' Build successfully completed!\nStandard output:\n' + result [ 'message' ] )
175+ currentResults . unshift ( '[' + languages [ this . state . language ] + '] Build successfully completed!\nStandard output:\n' + result [ 'message' ] )
140176 } else {
141- currentResults . unshift ( '[' + languages [ this . state . language ] + '] ' + ' Build failed!\nBuild errors:\n' + result [ 'message' ] )
177+ currentResults . unshift ( '[' + languages [ this . state . language ] + '] Build failed!\nBuild errors:\n' + result [ 'message' ] )
142178 }
143179
144180 this . setState ( {
@@ -147,6 +183,9 @@ class App extends Component {
147183 } )
148184 }
149185
186+ /**
187+ * Compiles the list of logs into a single string.
188+ */
150189 getLogs = ( ) => {
151190 return this . state . results . join ( '\n\n' )
152191 }
0 commit comments