Skip to content

Commit 0d84fe7

Browse files
authored
Merge pull request #11 from dannyhp1/develop
Refactored code.
2 parents efd784b + 820bc95 commit 0d84fe7

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

src/App/App.js

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,92 @@ import ResultsEditor from '../components/ResultsEditor';
66
import { Grid, Button, FormControl, FormControlLabel, Checkbox, Select, MenuItem } from '@material-ui/core';
77
import code from '../resources/code';
88
import languages from '../resources/languages';
9-
109
import './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

1525
class 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
}

src/components/CodeEditor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CodeEditor extends Component {
1818
editorHeight: '82.5vh',
1919
editorWidth: 'auto'
2020
}
21-
this.onResize = this.onResize.bind(this);
21+
this.onResize = this.onResize.bind(this)
2222
}
2323

2424
onResize = (w, h) => {
@@ -29,7 +29,7 @@ class CodeEditor extends Component {
2929
}
3030

3131
onChange = (value) => {
32-
this.props.onChange(value);
32+
this.props.onChange(value)
3333
}
3434

3535
render() {

src/components/Header.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { Component } from 'react'
2-
import { makeStyles, AppBar, Toolbar, Typography, Button, IconButton } from '@material-ui/core';
1+
import React from 'react'
2+
import { makeStyles, AppBar, Toolbar, Typography, IconButton } from '@material-ui/core';
33
import logo from '../resources/logo.png'
44

55
const useStyles = makeStyles(theme => ({

src/components/ResultsEditor.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ResultsEditor extends Component {
1111
editorHeight: '82.5vh',
1212
editorWidth: 'auto'
1313
}
14-
this.onResize = this.onResize.bind(this);
14+
this.onResize = this.onResize.bind(this)
1515
}
1616

1717
onResize = (w, h) => {
@@ -21,10 +21,6 @@ class ResultsEditor extends Component {
2121
})
2222
}
2323

24-
onChange = (value) => {
25-
26-
}
27-
2824
render() {
2925
return (
3026
<AceEditor

0 commit comments

Comments
 (0)