From c24ca07835eda1e32a3ab3561abed1db034dbe46 Mon Sep 17 00:00:00 2001 From: Nishant Kumar Date: Sun, 2 Jun 2024 12:09:46 +0530 Subject: [PATCH] feat: prepopulated Code Templates --- src/pages/EditorComponent.js | 160 +++++++++++++++++++++++++++++++---- 1 file changed, 145 insertions(+), 15 deletions(-) diff --git a/src/pages/EditorComponent.js b/src/pages/EditorComponent.js index ccfcc52..052cda6 100644 --- a/src/pages/EditorComponent.js +++ b/src/pages/EditorComponent.js @@ -4,8 +4,42 @@ import Editor from "@monaco-editor/react"; import "../components/css/EditorComponent.css"; // Optional for styling import "@fortawesome/fontawesome-free/css/all.css"; import { useSnackbar } from "notistack"; -import { Button, CircularProgress, styled } from "@mui/material"; -import { LANGUAGES, judge0SubmitUrl, rapidApiHost, rapidApiKey } from "../constants/constants"; +import {Button, CircularProgress, styled} from "@mui/material"; + +const judge0SubmitUrl = + process.env.JUDGE0_SUMBISSION_URL || process.env.REACT_APP_RAPID_API_URL; +const rapidApiHost = process.env.REACT_APP_RAPID_API_HOST; +const rapidApiKey = process.env.REACT_APP_RAPID_API_KEY; + +const LANGUAGE_ID_FOR_JAVASCRIPT = 63; +const LANGUAGE_ID_FOR_PYTHON3 = 71; +const LANGUAGE_ID_FOR_CPP = 76; +const LANGUAGES = [ + { + ID: LANGUAGE_ID_FOR_JAVASCRIPT, + NAME: "Javascript", + DEFAULT_LANGUAGE: "javascript", + HELLO_WORLD:"console.log('Hello World');", + }, + { + ID: LANGUAGE_ID_FOR_PYTHON3, + NAME: "Python3", + DEFAULT_LANGUAGE: "python", + HELLO_WORLD:"print('Hello World')" + }, + { + ID: LANGUAGE_ID_FOR_CPP, + NAME: "C++", + DEFAULT_LANGUAGE: "C++(Clang 7.0.1)", + HELLO_WORLD: `#include +using namespace std; +int main() +{ + cout<<"Hello World"< { - const selectedLanguage = LANGUAGES.find(language => language.DEFAULT_LANGUAGE === currentLanguage); + const selectedLanguage = LANGUAGES.find((lang) => lang.DEFAULT_LANGUAGE === currentLanguage); + setLanguageDetails({ LANGUAGE_ID: selectedLanguage.ID, LANGUAGE_NAME: selectedLanguage.NAME, DEFAULT_LANGUAGE: selectedLanguage.DEFAULT_LANGUAGE, }); + setCode(selectedLanguage.HELLO_WORLD); }, [currentLanguage]); // Reference to the Monaco editor instance @@ -44,11 +92,6 @@ function EditorComponent() { submitCode(); }); } - // Function to retrieve logo based on language ID - const getLanguageLogoById = (id) => { - const language = LANGUAGES.find(lang => lang.ID == id); - return language ? language.LOGO : null; - }; // Function to handle code submission async function submitCode() { const codeToSubmit = editorRef.current.getValue(); @@ -96,7 +139,7 @@ function EditorComponent() { }) .then((response) => response.json()) .then((data) => { - if (!data.stdout) { + if(!data.stdout) { enqueueSnackbar("Please check the code", { variant: "error" }); setOutput(data.message); return; @@ -127,7 +170,11 @@ function EditorComponent() {
{/* Current Language Logo */}
- {getLanguageLogoById(languageDetails.LANGUAGE_ID)} + {currentLanguage === LANGUAGES[0].DEFAULT_LANGUAGE ? ( + + ) : ( + + )}
{languageDetails.LANGUAGE_NAME} @@ -163,14 +210,14 @@ function EditorComponent() { language={languageDetails.DEFAULT_LANGUAGE} // Set default language to JavaScript /> - + {loading ? ( ) : ( )} - Run {languageDetails.LANGUAGE_NAME} Code + Run {languageDetails.LANGUAGE_NAME} Code
@@ -211,4 +258,87 @@ const styles = {
   },
 };
 
-export default EditorComponent
+function JavascriptLogo({ width = 40, height = 40 }) {
+  return (
+    
+      
+        
+          
+            
+            
+          
+        
+      
+    
+  );
+}
+
+function PythonLogo({ width = 40, height = 40 }) {
+  return (
+    
+      
+        
+          
+            
+            
+          
+        
+      
+    
+  );
+}
+
+export default EditorComponent;