-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathEditor.js
More file actions
142 lines (130 loc) · 3.84 KB
/
Copy pathEditor.js
File metadata and controls
142 lines (130 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React from 'react';
import { Form, FormGroup, Col, Button } from 'react-bootstrap';
import LangSelector from './controls/LangSelector';
import CodeEditor from './controls/CodeEditor';
import AlertDismissable from './controls/AlertDismissable';
import OutputBox from './controls/OutputBox';
import StatusImage from './controls/StatusImage';
import CompilerApi from '../api/CompilerApi';
let languages = ['JavaScript', 'Python', 'Java', 'C', 'C++'];
const languagesProd = ['JavaScript', 'Python'];
class Editor extends React.Component {
constructor(props) {
super(props);
console.log(`env: ${process.env.NODE_ENV}`);
if (process.env.NODE_ENV === 'production') {
languages = languagesProd;
}
this.state = {
selectedLang: 0, // JavaScript
task: {
lang: 'javascript',
code: '',
},
response: {
status: '0',
message: '',
},
};
this.handleRun = this.handleRun.bind(this);
this.updateSolution = this.updateSolution.bind(this);
this.handleLangChange = this.handleLangChange.bind(this);
this.handleCodeChange = this.handleCodeChange.bind(this);
}
componentDidMount() {
CompilerApi.getTask('javascript')
// .then(res => res.json())
.then((task) => {
console.log(task);
this.setState({ task });
});
}
handleCodeChange(code) {
const { task } = this.state;
task.code = code;
console.log(code);
return this.setState({ task });
}
handleRun(event) {
event.preventDefault();
const { task } = this.state;
console.log(task);
CompilerApi.run(task)
.then((res) => {
this.setState({ response: res });
})
.catch((error) => {
console.log(error);
// this.handleError(error);
});
}
updateSolution(event) {
// event.preventDefault();
console.log(this.state.task);
const field = event.target.name;
const { task } = this.state;
task[field] = event.target.value;
return this.setState({ task });
}
handleLangChange(event) {
const index = parseInt(event.target.value, 10);
CompilerApi.getTask(languages[index]).then((task) => {
console.log(task);
this.setState({ task });
});
const response = { status: '0', message: '' };
this.setState({ response });
return this.setState({ selectedLang: index });
}
render() {
return (
<div className="container">
<Form horizontal>
<FormGroup controlId="code">
<Col sm={12}>
<LangSelector
langs={languages}
selectedIndex={this.state.selectedLang}
onChange={this.handleLangChange}
/>
</Col>
</FormGroup>
<FormGroup controlId="code">
<Col sm={12}>
<CodeEditor onChange={this.handleCodeChange} code={this.state.task.code} />
</Col>
</FormGroup>
<FormGroup>
<Col sm={2}>
<Button bsStyle="primary" type="button" onClick={this.handleRun}>
Run
</Button>
<StatusImage
hasError={this.state.response.status !== '0'}
message={this.state.response.message}
/>
</Col>
<Col sm={10} />
</FormGroup>
<FormGroup>
<Col sm={12}>
<AlertDismissable
show={this.state.response.status !== '0'}
message={this.state.response.message}
/>
</Col>
</FormGroup>
<FormGroup>
<Col sm={12}>
<OutputBox
show={this.state.response.status === '0'}
message={this.state.response.message}
/>
</Col>
</FormGroup>
</Form>
</div>
);
}
}
export default Editor;