-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCodebox.jsx
More file actions
92 lines (82 loc) · 2.63 KB
/
Copy pathCodebox.jsx
File metadata and controls
92 lines (82 loc) · 2.63 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
import { useState } from "react";
import SamplesData from "../data/Samples";
import Editor from "./Editor";
import {
Button, Col, Container, InputGroup, Row
} from "reactstrap";
import { FaExternalLinkAlt } from 'react-icons/fa';
import PulseLoader from 'react-spinners/PulseLoader';
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function Codebox({ flix }) {
const [initialChoice] = useState(() => getRandomInt(SamplesData.length));
const [choice, setChoice] = useState(initialChoice);
const [input, setInput] = useState(SamplesData[initialChoice].code);
const [output, setOutput] = useState(undefined);
function onDropdownChoice(event) {
let newChoice = Number(event.target.value);
setChoice(newChoice);
setInput(SamplesData[newChoice].code);
setOutput(undefined);
}
function onRunClick() {
setOutput(null);
flix.run(input, data => setOutput(data));
}
function getRunButton() {
return <a href={"http://play.flix.dev/"}><Button color="success" size="sm">
Play <FaExternalLinkAlt className="ml-2"/>
</Button>
</a>;
}
function getDropDown() {
return <select
value={choice}
onChange={onDropdownChoice}
style={{"textOverflow": "ellipsis"}}
className="mr-2 w-75">
{SamplesData.map((sample, index) =>
<option key={index} value={index}>{sample.name}</option>)
}
</select>
}
function getEditor() {
return <Editor key={input.leading}
code={input}
notifyTextChanged={setInput}/>
}
function getOutput() {
if (output === undefined) {
return undefined;
} else if (output === null) {
return <Row>
<Col md="12" className="text-center">
<PulseLoader
size={16}
color={'#28a745'}
loading={true}
className="loader"
/>
</Col>
</Row>
} else {
return (
<Container className="mt-3 editor-output">
<h5>Standard Output</h5>
<pre>{output.result}</pre>
</Container>);
}
}
return (
<div>
<InputGroup className="mt-2 mb-3">
{getDropDown()}
{getRunButton()}
</InputGroup>
{getEditor()}
{getOutput()}
</div>
);
}
export default Codebox;