Skip to content

Commit c24ca07

Browse files
committed
feat: prepopulated Code Templates
1 parent b83619e commit c24ca07

File tree

1 file changed

+145
-15
lines changed

1 file changed

+145
-15
lines changed

src/pages/EditorComponent.js

Lines changed: 145 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,80 @@ import Editor from "@monaco-editor/react";
44
import "../components/css/EditorComponent.css"; // Optional for styling
55
import "@fortawesome/fontawesome-free/css/all.css";
66
import { useSnackbar } from "notistack";
7-
import { Button, CircularProgress, styled } from "@mui/material";
8-
import { LANGUAGES, judge0SubmitUrl, rapidApiHost, rapidApiKey } from "../constants/constants";
7+
import {Button, CircularProgress, styled} from "@mui/material";
8+
9+
const judge0SubmitUrl =
10+
process.env.JUDGE0_SUMBISSION_URL || process.env.REACT_APP_RAPID_API_URL;
11+
const rapidApiHost = process.env.REACT_APP_RAPID_API_HOST;
12+
const rapidApiKey = process.env.REACT_APP_RAPID_API_KEY;
13+
14+
const LANGUAGE_ID_FOR_JAVASCRIPT = 63;
15+
const LANGUAGE_ID_FOR_PYTHON3 = 71;
16+
const LANGUAGE_ID_FOR_CPP = 76;
17+
const LANGUAGES = [
18+
{
19+
ID: LANGUAGE_ID_FOR_JAVASCRIPT,
20+
NAME: "Javascript",
21+
DEFAULT_LANGUAGE: "javascript",
22+
HELLO_WORLD:"console.log('Hello World');",
23+
},
24+
{
25+
ID: LANGUAGE_ID_FOR_PYTHON3,
26+
NAME: "Python3",
27+
DEFAULT_LANGUAGE: "python",
28+
HELLO_WORLD:"print('Hello World')"
29+
},
30+
{
31+
ID: LANGUAGE_ID_FOR_CPP,
32+
NAME: "C++",
33+
DEFAULT_LANGUAGE: "C++(Clang 7.0.1)",
34+
HELLO_WORLD: `#include<iostream>
35+
using namespace std;
36+
int main()
37+
{
38+
cout<<"Hello World"<<endl;
39+
}
40+
`
41+
},
42+
];
943

1044
const StyledButton = styled(Button)({
1145
display: "flex",
1246
alignItems: "center",
1347
justifyContent: "center",
1448
gap: "0.5rem",
1549
});
50+
// need to incorporate toggle
51+
// const LANGUAGE = LANGUAGES[0];
52+
53+
// const LANGUAGE_ID = LANGUAGE["ID"];
54+
// const LANGUAGE_NAME = LANGUAGE["NAME"];
55+
// const DEFAULT_LANGUAGE = LANGUAGE["DEFAULT_LANGUAGE"];
1656

1757
function EditorComponent() {
1858
// State variables for code, output, and potential error messages
1959
const [code, setCode] = useState(null); // Consider setting an initial value if needed
2060
const [output, setOutput] = useState("");
21-
const [currentLanguage, setCurrentLanguage] = useState(LANGUAGES[0].DEFAULT_LANGUAGE);
22-
const [languageDetails, setLanguageDetails] = useState(LANGUAGES[0]);
61+
const [currentLanguage, setCurrentLanguage] = useState(
62+
LANGUAGES[0].DEFAULT_LANGUAGE
63+
);
64+
const [languageDetails, setLanguageDetails] = useState({
65+
LANGUAGE_ID: "",
66+
LANGUAGE_NAME: "",
67+
DEFAULT_LANGUAGE: "",
68+
});
2369
const [loading, setLoading] = useState(false);
2470
const { enqueueSnackbar } = useSnackbar();
2571

2672
useEffect(() => {
27-
const selectedLanguage = LANGUAGES.find(language => language.DEFAULT_LANGUAGE === currentLanguage);
73+
const selectedLanguage = LANGUAGES.find((lang) => lang.DEFAULT_LANGUAGE === currentLanguage);
74+
2875
setLanguageDetails({
2976
LANGUAGE_ID: selectedLanguage.ID,
3077
LANGUAGE_NAME: selectedLanguage.NAME,
3178
DEFAULT_LANGUAGE: selectedLanguage.DEFAULT_LANGUAGE,
3279
});
80+
setCode(selectedLanguage.HELLO_WORLD);
3381
}, [currentLanguage]);
3482

3583
// Reference to the Monaco editor instance
@@ -44,11 +92,6 @@ function EditorComponent() {
4492
submitCode();
4593
});
4694
}
47-
// Function to retrieve logo based on language ID
48-
const getLanguageLogoById = (id) => {
49-
const language = LANGUAGES.find(lang => lang.ID == id);
50-
return language ? language.LOGO : null;
51-
};
5295
// Function to handle code submission
5396
async function submitCode() {
5497
const codeToSubmit = editorRef.current.getValue();
@@ -96,7 +139,7 @@ function EditorComponent() {
96139
})
97140
.then((response) => response.json())
98141
.then((data) => {
99-
if (!data.stdout) {
142+
if(!data.stdout) {
100143
enqueueSnackbar("Please check the code", { variant: "error" });
101144
setOutput(data.message);
102145
return;
@@ -127,7 +170,11 @@ function EditorComponent() {
127170
<div style={styles.flexBetween}>
128171
{/* Current Language Logo */}
129172
<div style={styles.flexStart}>
130-
{getLanguageLogoById(languageDetails.LANGUAGE_ID)}
173+
{currentLanguage === LANGUAGES[0].DEFAULT_LANGUAGE ? (
174+
<JavascriptLogo />
175+
) : (
176+
<PythonLogo />
177+
)}
131178

132179
<div style={{ fontWeight: "bold" }}>
133180
{languageDetails.LANGUAGE_NAME}
@@ -163,14 +210,14 @@ function EditorComponent() {
163210
language={languageDetails.DEFAULT_LANGUAGE} // Set default language to JavaScript
164211
/>
165212
<StyledButton onClick={submitCode} style={styles.button} variant="contained" color="primary" disabled={loading}>
166-
<span>
213+
<span>
167214
{loading ? (
168215
<CircularProgress size={16} />
169216
) : (
170217
<FaPlay size="16" />
171218
)}
172219
</span>
173-
Run {languageDetails.LANGUAGE_NAME} Code
220+
Run {languageDetails.LANGUAGE_NAME} Code
174221
</StyledButton>
175222
<div className="output">
176223
<pre>
@@ -211,4 +258,87 @@ const styles = {
211258
},
212259
};
213260

214-
export default EditorComponent
261+
function JavascriptLogo({ width = 40, height = 40 }) {
262+
return (
263+
<svg
264+
xmlns="http://www.w3.org/2000/svg"
265+
x="0px"
266+
y="0px"
267+
width={width}
268+
height={height}
269+
viewBox="0,0,256,256"
270+
>
271+
<g transform="">
272+
<g
273+
fill="none"
274+
fillRule="nonzero"
275+
stroke="none"
276+
strokeWidth="1"
277+
strokeLinecap="butt"
278+
strokeLinejoin="miter"
279+
strokeMiterlimit="10"
280+
strokeDasharray=""
281+
strokeDashoffset="0"
282+
fontFamily="none"
283+
fontWeight="none"
284+
fontSize="none"
285+
textAnchor="none"
286+
style={{ mixBlendMode: "normal" }}
287+
>
288+
<g transform="scale(5.33333,5.33333)">
289+
<path d="M6,42v-36h36v36z" fill="#ffd600"></path>
290+
<path
291+
d="M29.538,32.947c0.692,1.124 1.444,2.201 3.037,2.201c1.338,0 2.04,-0.665 2.04,-1.585c0,-1.101 -0.726,-1.492 -2.198,-2.133l-0.807,-0.344c-2.329,-0.988 -3.878,-2.226 -3.878,-4.841c0,-2.41 1.845,-4.244 4.728,-4.244c2.053,0 3.528,0.711 4.592,2.573l-2.514,1.607c-0.553,-0.988 -1.151,-1.377 -2.078,-1.377c-0.946,0 -1.545,0.597 -1.545,1.377c0,0.964 0.6,1.354 1.985,1.951l0.807,0.344c2.745,1.169 4.293,2.363 4.293,5.047c0,2.892 -2.284,4.477 -5.35,4.477c-2.999,0 -4.702,-1.505 -5.65,-3.368zM17.952,33.029c0.506,0.906 1.275,1.603 2.381,1.603c1.058,0 1.667,-0.418 1.667,-2.043v-10.589h3.333v11.101c0,3.367 -1.953,4.899 -4.805,4.899c-2.577,0 -4.437,-1.746 -5.195,-3.368z"
292+
fill="#000001"
293+
></path>
294+
</g>
295+
</g>
296+
</g>
297+
</svg>
298+
);
299+
}
300+
301+
function PythonLogo({ width = 40, height = 40 }) {
302+
return (
303+
<svg
304+
xmlns="http://www.w3.org/2000/svg"
305+
x="0px"
306+
y="0px"
307+
width={width}
308+
height={height}
309+
viewBox="0,0,256,256"
310+
>
311+
<g transform="translate(30.72,30.72) scale(0.76,0.76)">
312+
<g
313+
fill="none"
314+
fillRule="nonzero"
315+
stroke="none"
316+
strokeWidth="1"
317+
strokeLinecap="butt"
318+
strokeLinejoin="miter"
319+
strokeMiterlimit="10"
320+
strokeDasharray=""
321+
strokeDashoffset="0"
322+
fontFamily="none"
323+
fontWeight="none"
324+
fontSize="none"
325+
textAnchor="none"
326+
style={{ mixBlendMode: "normal" }}
327+
>
328+
<g transform="scale(5.33333,5.33333)">
329+
<path
330+
d="M24.047,5c-1.555,0.005 -2.633,0.142 -3.936,0.367c-3.848,0.67 -4.549,2.077 -4.549,4.67v3.963h9v2h-9.342h-4.35c-2.636,0 -4.943,1.242 -5.674,4.219c-0.826,3.417 -0.863,5.557 0,9.125c0.655,2.661 2.098,4.656 4.735,4.656h3.632v-5.104c0,-2.966 2.686,-5.896 5.764,-5.896h7.236c2.523,0 5,-1.862 5,-4.377v-8.586c0,-2.439 -1.759,-4.263 -4.218,-4.672c0.061,-0.006 -1.756,-0.371 -3.298,-0.365zM19.063,9c0.821,0 1.5,0.677 1.5,1.502c0,0.833 -0.679,1.498 -1.5,1.498c-0.837,0 -1.5,-0.664 -1.5,-1.498c0,-0.822 0.663,-1.502 1.5,-1.502z"
331+
fill="#0277bd"
332+
></path>
333+
<path
334+
d="M23.078,43c1.555,-0.005 2.633,-0.142 3.936,-0.367c3.848,-0.67 4.549,-2.077 4.549,-4.67v-3.963h-9v-2h9.343h4.35c2.636,0 4.943,-1.242 5.674,-4.219c0.826,-3.417 0.863,-5.557 0,-9.125c-0.656,-2.661 -2.099,-4.656 -4.736,-4.656h-3.632v5.104c0,2.966 -2.686,5.896 -5.764,5.896h-7.236c-2.523,0 -5,1.862 -5,4.377v8.586c0,2.439 1.759,4.263 4.218,4.672c-0.061,0.006 1.756,0.371 3.298,0.365zM28.063,39c-0.821,0 -1.5,-0.677 -1.5,-1.502c0,-0.833 0.679,-1.498 1.5,-1.498c0.837,0 1.5,0.664 1.5,1.498c0,0.822 -0.664,1.502 -1.5,1.502z"
335+
fill="#ffc107"
336+
></path>
337+
</g>
338+
</g>
339+
</g>
340+
</svg>
341+
);
342+
}
343+
344+
export default EditorComponent;

0 commit comments

Comments
 (0)