Skip to content

Commit 93504ea

Browse files
authored
Merge pull request #104 from guanquann/code-execution-fe
Update history on end session
2 parents dd1d4e9 + 4a30256 commit 93504ea

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

frontend/src/components/CollabSessionControls/index.tsx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,14 @@ import { Button, Stack } from "@mui/material";
22
import Stopwatch from "../Stopwatch";
33
import { useCollab } from "../../contexts/CollabContext";
44
import { USE_COLLAB_ERROR_MESSAGE } from "../../utils/constants";
5-
import { useEffect, useState } from "react";
65

76
const CollabSessionControls: React.FC = () => {
8-
const [time, setTime] = useState<number>(0);
9-
10-
useEffect(() => {
11-
const intervalId = setInterval(
12-
() => setTime((prevTime) => prevTime + 1),
13-
1000
14-
);
15-
16-
return () => clearInterval(intervalId);
17-
}, [time]);
18-
197
const collab = useCollab();
208
if (!collab) {
219
throw new Error(USE_COLLAB_ERROR_MESSAGE);
2210
}
2311

24-
const { handleSubmitSessionClick, handleEndSessionClick } = collab;
12+
const { handleSubmitSessionClick, handleEndSessionClick, time } = collab;
2513

2614
return (
2715
<Stack direction={"row"} alignItems={"center"} spacing={2}>
@@ -33,7 +21,7 @@ const CollabSessionControls: React.FC = () => {
3321
}}
3422
variant="outlined"
3523
color="success"
36-
onClick={() => handleSubmitSessionClick(time)}
24+
onClick={() => handleSubmitSessionClick()}
3725
>
3826
Submit
3927
</Button>

frontend/src/contexts/CollabContext.tsx

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable react-refresh/only-export-components */
22

3-
import React, { createContext, useContext, useState } from "react";
3+
import React, { createContext, useContext, useEffect, useState } from "react";
44
import {
55
USE_MATCH_ERROR_MESSAGE,
66
FAILED_TESTCASE_MESSAGE,
@@ -11,7 +11,7 @@ import {
1111
import { toast } from "react-toastify";
1212

1313
import { useMatch } from "./MatchContext";
14-
import { codeExecutionClient } from "../utils/api";
14+
import { qnHistoryClient, codeExecutionClient } from "../utils/api";
1515
import { useReducer } from "react";
1616
import { updateQnHistoryById } from "../reducers/qnHistoryReducer";
1717
import qnHistoryReducer, { initialQHState } from "../reducers/qnHistoryReducer";
@@ -33,7 +33,7 @@ export type CompilerResult = {
3333
};
3434

3535
type CollabContextType = {
36-
handleSubmitSessionClick: (time: number) => void;
36+
handleSubmitSessionClick: () => void;
3737
handleEndSessionClick: () => void;
3838
handleRejectEndSession: () => void;
3939
handleConfirmEndSession: () => void;
@@ -42,6 +42,8 @@ type CollabContextType = {
4242
compilerResult: CompilerResult[];
4343
setCompilerResult: React.Dispatch<React.SetStateAction<CompilerResult[]>>;
4444
isEndSessionModalOpen: boolean;
45+
time: number;
46+
resetCollab: () => void;
4547
};
4648

4749
const CollabContext = createContext<CollabContextType | null>(null);
@@ -66,6 +68,17 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
6668
qnHistoryId,
6769
} = match;
6870

71+
const [time, setTime] = useState<number>(0);
72+
73+
useEffect(() => {
74+
const intervalId = setInterval(
75+
() => setTime((prevTime) => prevTime + 1),
76+
1000
77+
);
78+
79+
return () => clearInterval(intervalId);
80+
}, [time]);
81+
6982
// eslint-disable-next-line
7083
const [_qnHistoryState, qnHistoryDispatch] = useReducer(
7184
qnHistoryReducer,
@@ -76,7 +89,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
7689
const [isEndSessionModalOpen, setIsEndSessionModalOpen] =
7790
useState<boolean>(false);
7891

79-
const handleSubmitSessionClick = async (time: number) => {
92+
const handleSubmitSessionClick = async () => {
8093
try {
8194
const res = await codeExecutionClient.post("/", {
8295
questionId,
@@ -124,9 +137,26 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
124137
setIsEndSessionModalOpen(false);
125138
};
126139

127-
const handleConfirmEndSession = () => {
140+
const handleConfirmEndSession = async () => {
128141
setIsEndSessionModalOpen(false);
129142

143+
// Get queston history
144+
const data = await qnHistoryClient.get(qnHistoryId as string);
145+
146+
// Only update question history if it has not been submitted before
147+
if (!data.data.qnHistory.code) {
148+
updateQnHistoryById(
149+
qnHistoryId as string,
150+
{
151+
submissionStatus: "Attempted",
152+
dateAttempted: new Date().toISOString(),
153+
timeTaken: time,
154+
code: code.replace(/\t/g, " ".repeat(4)),
155+
},
156+
qnHistoryDispatch
157+
);
158+
}
159+
130160
// Leave collaboration room
131161
leave(matchUser?.id as string, getMatchId() as string, true);
132162
leave(partner?.id as string, getMatchId() as string, true);
@@ -137,6 +167,9 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
137167
// Delete match data
138168
stopMatch();
139169
appNavigate("/home");
170+
171+
// Reset collab state
172+
resetCollab();
140173
};
141174

142175
const checkPartnerStatus = () => {
@@ -148,6 +181,11 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
148181
});
149182
};
150183

184+
const resetCollab = () => {
185+
setCompilerResult([]);
186+
setTime(0);
187+
};
188+
151189
return (
152190
<CollabContext.Provider
153191
value={{
@@ -160,6 +198,8 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
160198
compilerResult,
161199
setCompilerResult,
162200
isEndSessionModalOpen,
201+
time,
202+
resetCollab,
163203
}}
164204
>
165205
{children}

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ const CollabSandbox: React.FC = () => {
6262

6363
const {
6464
compilerResult,
65-
setCompilerResult,
6665
handleRejectEndSession,
6766
handleConfirmEndSession,
6867
checkPartnerStatus,
6968
isEndSessionModalOpen,
69+
resetCollab,
7070
} = collab;
7171

7272
const [state, dispatch] = useReducer(reducer, initialState);
@@ -83,6 +83,8 @@ const CollabSandbox: React.FC = () => {
8383
getQuestionById(questionId, dispatch);
8484
setCompilerResult([]);
8585

86+
resetCollab();
87+
8688
const matchId = getMatchId();
8789
if (!matchUser || !matchId) {
8890
return;

0 commit comments

Comments
 (0)