Skip to content

Commit

Permalink
feat: Implement red border if error
Browse files Browse the repository at this point in the history
  • Loading branch information
stepkos committed May 30, 2024
1 parent bf3eb88 commit 546029a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import {useState} from "react";
function App() {

const [code, setCode] = useState("")
const [isError, setIsError] = useState(false);


const handleExecuteCode = (newCode) => {
setCode(newCode);
};

return (
<div className="App">
<CubeScene code={code} />
<CodeEditor onExecute={handleExecuteCode} />
<CubeScene code={code} setIsError={setIsError} />
<CodeEditor onExecute={handleExecuteCode} isError={isError} />
</div>
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/CodeEditor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useState} from "react";


const CodeEditor = ({ onExecute }) => {
const CodeEditor = ({ onExecute, isError }) => {

const [code, setCode] = useState("")

Expand All @@ -18,6 +18,7 @@ const CodeEditor = ({ onExecute }) => {
<textarea
value={code}
onChange={handleChange}
style={{ borderColor: isError ? "red" : "black" }}
/>
<button onClick={handleExecute}>Execute Code</button>
</div>
Expand Down
8 changes: 5 additions & 3 deletions src/components/CubeScene.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// src/ThreeScene.js
import React, { useRef, useEffect } from 'react';
import React, {useRef, useEffect} from 'react';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import './../App.css';

const CubeScene = ({ code }) => {
const CubeScene = ({ code, setIsError }) => {
const mountRef = useRef(null);

useEffect(() => {
Expand Down Expand Up @@ -52,8 +52,10 @@ const CubeScene = ({ code }) => {
try {
/* eslint-disable no-eval */
eval(code);
setIsError(false);
} catch (error) {
console.error("Error executing code: ", error);
setIsError(true);
}
// cube.rotation.y += 0.01;
renderer.render(scene, camera);
Expand All @@ -77,7 +79,7 @@ const CubeScene = ({ code }) => {
currentMount.removeChild(renderer.domElement);
}
};
}, [code]);
}, [code, setIsError]);

return <div className="cube-scene" ref={mountRef}></div>;
};
Expand Down

0 comments on commit 546029a

Please sign in to comment.