Skip to content

Commit 58d045f

Browse files
committed
Update question history with current progress when user end session without submitting previously
1 parent 5cb3d86 commit 58d045f

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
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 & 7 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,15 +33,16 @@ 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;
4040
checkPartnerStatus: () => void;
4141
setCode: React.Dispatch<React.SetStateAction<string>>;
4242
compilerResult: CompilerResult[];
43-
setCompilerResult: React.Dispatch<React.SetStateAction<CompilerResult[]>>;
4443
isEndSessionModalOpen: boolean;
44+
time: number;
45+
resetCollab: () => void;
4546
};
4647

4748
const CollabContext = createContext<CollabContextType | null>(null);
@@ -66,6 +67,17 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
6667
qnHistoryId,
6768
} = match;
6869

70+
const [time, setTime] = useState<number>(0);
71+
72+
useEffect(() => {
73+
const intervalId = setInterval(
74+
() => setTime((prevTime) => prevTime + 1),
75+
1000
76+
);
77+
78+
return () => clearInterval(intervalId);
79+
}, [time]);
80+
6981
// eslint-disable-next-line
7082
const [_qnHistoryState, qnHistoryDispatch] = useReducer(
7183
qnHistoryReducer,
@@ -76,7 +88,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
7688
const [isEndSessionModalOpen, setIsEndSessionModalOpen] =
7789
useState<boolean>(false);
7890

79-
const handleSubmitSessionClick = async (time: number) => {
91+
const handleSubmitSessionClick = async () => {
8092
try {
8193
const res = await codeExecutionClient.post("/", {
8294
questionId,
@@ -124,9 +136,26 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
124136
setIsEndSessionModalOpen(false);
125137
};
126138

127-
const handleConfirmEndSession = () => {
139+
const handleConfirmEndSession = async () => {
128140
setIsEndSessionModalOpen(false);
129141

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

142174
const checkPartnerStatus = () => {
@@ -148,6 +180,11 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
148180
});
149181
};
150182

183+
const resetCollab = () => {
184+
setCompilerResult([]);
185+
setTime(0);
186+
};
187+
151188
return (
152189
<CollabContext.Provider
153190
value={{
@@ -158,8 +195,9 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
158195
checkPartnerStatus,
159196
setCode,
160197
compilerResult,
161-
setCompilerResult,
162198
isEndSessionModalOpen,
199+
time,
200+
resetCollab,
163201
}}
164202
>
165203
{children}

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 3 additions & 2 deletions
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);
@@ -81,7 +81,8 @@ const CollabSandbox: React.FC = () => {
8181
return;
8282
}
8383
getQuestionById(questionId, dispatch);
84-
setCompilerResult([]);
84+
85+
resetCollab();
8586

8687
const matchId = getMatchId();
8788
if (!matchUser || !matchId) {

0 commit comments

Comments
 (0)