Skip to content

Commit e275aba

Browse files
committed
feat: load last submission for the user on the benchmark
1 parent b292d32 commit e275aba

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/components/Benchmarks/BenchmarkDetail.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { RouteComponentProps } from 'react-router-dom';
33
import Header from '../Page/Header';
44
import Page from '../Page/Page';
55
import Editor from '@monaco-editor/react';
6-
import useProcessInterval from '../../hooks/submissions';
6+
import useProcessInterval, {
7+
useLastSubmissionForUser,
8+
} from '../../hooks/submissions';
79
import Result from '../Dashboard/Result';
810
import { Listbox, Transition } from '@headlessui/react';
911
import { CheckIcon, SelectorIcon } from '@heroicons/react/solid';
@@ -50,6 +52,14 @@ const BenchmarkDetail = ({
5052
editorRef.current = editor;
5153
}
5254

55+
let lastSubmission;
56+
const {
57+
isLoading: isLastSubmissionLoading,
58+
isError: isLastSubmissionError,
59+
data: lastSubmissionData,
60+
error: errorLastSubmission,
61+
} = useLastSubmissionForUser(match.params.id, selected.name);
62+
5363
// Handle code submission and job result polling
5464
const {
5565
mutate,
@@ -85,6 +95,19 @@ const BenchmarkDetail = ({
8595
}
8696
}
8797

98+
if (isLastSubmissionLoading) {
99+
lastSubmission = 'Loading...';
100+
}
101+
102+
if (isLastSubmissionError) {
103+
if (errorLastSubmission) {
104+
lastSubmission = "print('Welcome to Codebench !')";
105+
}
106+
}
107+
if (lastSubmissionData) {
108+
lastSubmission = lastSubmissionData.code;
109+
}
110+
88111
return (
89112
<Page>
90113
<Header
@@ -203,8 +226,8 @@ const BenchmarkDetail = ({
203226
<Editor
204227
onMount={handleEditorDidMount}
205228
height="100%"
206-
defaultLanguage="python"
207-
defaultValue={`print('hey!')`}
229+
value={lastSubmission && lastSubmission}
230+
language={selected.name}
208231
/>
209232
</div>
210233
<div className="justify-self-start">{result && result}</div>

src/hooks/submissions.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,29 @@ function useProcessInterval({
9696
}
9797

9898
export default useProcessInterval;
99+
100+
export function useLastSubmissionForUser(
101+
benchmarkId: string,
102+
language: string,
103+
) {
104+
const { token } = useToken();
105+
106+
return useQuery<{ code: string }, Error>(`last-submission`, async () => {
107+
if (benchmarkId && language) {
108+
const { data } = await axios.put(
109+
`${process.env.REACT_APP_API_ENDPOINT}/submissions`,
110+
{
111+
benchmarkId,
112+
language,
113+
},
114+
{
115+
headers: {
116+
Authorization: `Bearer ${token}`,
117+
},
118+
},
119+
);
120+
// console.log(data);
121+
return data;
122+
}
123+
});
124+
}

0 commit comments

Comments
 (0)