Skip to content

Commit dd1d4e9

Browse files
authored
Merge pull request #103 from guanquann/code-execution-fe
Code execution FE
2 parents 427b044 + 798819c commit dd1d4e9

File tree

3 files changed

+63
-30
lines changed

3 files changed

+63
-30
lines changed

frontend/src/components/TestCase/index.tsx

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Box, styled, Typography } from "@mui/material";
2+
import { CompilerResult } from "../../contexts/CollabContext";
23

34
type TestCaseProps = {
45
input: string;
5-
output?: string;
6-
stdout: string;
7-
result?: string;
6+
expected: string;
7+
result: CompilerResult;
88
};
99

1010
const StyledBox = styled(Box)(({ theme }) => ({
@@ -18,34 +18,58 @@ const StyledTypography = styled(Typography)(({ theme }) => ({
1818
whiteSpace: "pre-line",
1919
}));
2020

21-
const TestCase: React.FC<TestCaseProps> = ({
22-
input,
23-
output,
24-
stdout,
25-
result,
26-
}) => {
21+
const TestCase: React.FC<TestCaseProps> = ({ input, expected, result }) => {
2722
return (
2823
<Box>
29-
<StyledBox sx={(theme) => ({ marginBottom: theme.spacing(2) })}>
24+
{"isMatch" in result && result.isMatch && (
25+
<StyledBox>
26+
<Typography variant="h4" color="success">
27+
Accepted
28+
</Typography>
29+
</StyledBox>
30+
)}
31+
{"isMatch" in result && !result.isMatch && (
32+
<StyledBox>
33+
<Typography variant="h4" color="error">
34+
Wrong Answer
35+
</Typography>
36+
</StyledBox>
37+
)}
38+
{result.stderr && (
39+
<StyledBox>
40+
<Typography variant="overline">Error</Typography>
41+
<StyledTypography
42+
fontFamily={"monospace"}
43+
sx={{ backgroundColor: "#EDAFAF" }}
44+
>
45+
{result.stderr}
46+
</StyledTypography>
47+
</StyledBox>
48+
)}
49+
<StyledBox>
3050
<Typography variant="overline">Input</Typography>
3151
<StyledTypography fontFamily={"monospace"}>{input}</StyledTypography>
3252
</StyledBox>
33-
{output && (
53+
<StyledBox>
54+
<Typography variant="overline">Expected</Typography>
55+
<StyledTypography fontFamily={"monospace"}>{expected}</StyledTypography>
56+
</StyledBox>
57+
{"actualResult" in result && (
3458
<StyledBox>
35-
<Typography variant="overline">Output</Typography>
36-
<StyledTypography fontFamily={"monospace"}>{output}</StyledTypography>
59+
<Typography variant="overline">Actual</Typography>
60+
<StyledTypography fontFamily={"monospace"}>
61+
{result.actualResult || "\u00A0"}
62+
</StyledTypography>
3763
</StyledBox>
3864
)}
39-
{stdout && (
65+
{"stdout" in result && (
4066
<StyledBox>
4167
<Typography variant="overline">Stdout</Typography>
42-
<StyledTypography fontFamily={"monospace"}>{stdout}</StyledTypography>
68+
<StyledTypography fontFamily={"monospace"}>
69+
{result.stdout || "\u00A0"}
70+
</StyledTypography>
4371
</StyledBox>
4472
)}
45-
<StyledBox>
46-
<Typography variant="overline">Expected</Typography>
47-
<StyledTypography fontFamily={"monospace"}>{result}</StyledTypography>
48-
</StyledBox>
4973
</Box>
5074
);
5175
};

frontend/src/contexts/CollabContext.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { CollabEvents, collabSocket, leave } from "../utils/collabSocket";
1919
import { communicationSocket } from "../utils/communicationSocket";
2020
import useAppNavigate from "../components/UseAppNavigate";
2121

22-
type CompilerResult = {
22+
export type CompilerResult = {
2323
status: string;
2424
exception: string | null;
2525
stdout: string;
@@ -29,6 +29,7 @@ type CompilerResult = {
2929
stout: string;
3030
actualResult: string;
3131
expectedResult: string;
32+
isMatch: boolean;
3233
};
3334

3435
type CollabContextType = {
@@ -39,6 +40,7 @@ type CollabContextType = {
3940
checkPartnerStatus: () => void;
4041
setCode: React.Dispatch<React.SetStateAction<string>>;
4142
compilerResult: CompilerResult[];
43+
setCompilerResult: React.Dispatch<React.SetStateAction<CompilerResult[]>>;
4244
isEndSessionModalOpen: boolean;
4345
};
4446

@@ -78,12 +80,12 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
7880
try {
7981
const res = await codeExecutionClient.post("/", {
8082
questionId,
81-
code,
83+
// Replace tabs with 4 spaces to prevent formatting issues
84+
code: code.replace(/\t/g, " ".repeat(4)),
8285
language: matchCriteria?.language.toLowerCase(),
8386
});
84-
85-
console.log(res.data.data);
86-
setCompilerResult(res.data.data);
87+
console.log([...res.data.data]);
88+
setCompilerResult([...res.data.data]);
8789

8890
let isMatch = true;
8991
for (let i = 0; i < res.data.data.length; i++) {
@@ -156,6 +158,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
156158
checkPartnerStatus,
157159
setCode,
158160
compilerResult,
161+
setCompilerResult,
159162
isEndSessionModalOpen,
160163
}}
161164
>

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
Tabs,
1313
} from "@mui/material";
1414
import classes from "./index.module.css";
15-
import { useCollab } from "../../contexts/CollabContext";
15+
import { CompilerResult, useCollab } from "../../contexts/CollabContext";
1616
import { useMatch } from "../../contexts/MatchContext";
1717
import {
1818
COLLAB_CONNECTION_ERROR,
@@ -61,6 +61,8 @@ const CollabSandbox: React.FC = () => {
6161
}
6262

6363
const {
64+
compilerResult,
65+
setCompilerResult,
6466
handleRejectEndSession,
6567
handleConfirmEndSession,
6668
checkPartnerStatus,
@@ -79,6 +81,7 @@ const CollabSandbox: React.FC = () => {
7981
return;
8082
}
8183
getQuestionById(questionId, dispatch);
84+
setCompilerResult([]);
8285

8386
const matchId = getMatchId();
8487
if (!matchUser || !matchId) {
@@ -95,7 +98,7 @@ const CollabSandbox: React.FC = () => {
9598
toast.error(COLLAB_CONNECTION_ERROR);
9699
setIsConnecting(false);
97100
}
98-
} catch (error) {
101+
} catch {
99102
toast.error(COLLAB_CONNECTION_ERROR);
100103
setIsConnecting(false);
101104
}
@@ -122,7 +125,7 @@ const CollabSandbox: React.FC = () => {
122125
return <Navigate to="/home" replace />;
123126
}
124127

125-
if (!selectedQuestion || !editorState) {
128+
if (!selectedQuestion || !editorState || !compilerResult) {
126129
return <Loader />;
127130
}
128131

@@ -262,9 +265,12 @@ const CollabSandbox: React.FC = () => {
262265
{/* display result of each test case in the output (result) and stdout (any print statements executed) */}
263266
<TestCase
264267
input={selectedQuestion.inputs[selectedTestcase]}
265-
output={""}
266-
stdout={""}
267-
result={selectedQuestion.outputs[selectedTestcase]}
268+
expected={selectedQuestion.outputs[selectedTestcase]}
269+
result={
270+
compilerResult.length > 0
271+
? compilerResult[selectedTestcase]
272+
: ({} as CompilerResult)
273+
}
268274
/>
269275
</TabPanel>
270276
<TabPanel value={selectedTab} selected="chat">

0 commit comments

Comments
 (0)