Skip to content

Commit bcbc85d

Browse files
committed
feat: link submissions to benchmarks
1 parent 998e7f0 commit bcbc85d

File tree

4 files changed

+76
-49
lines changed

4 files changed

+76
-49
lines changed

src/components/Benchmarks/BenchmarkDetail.tsx

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import React, { Fragment, useEffect, useRef, useState } from 'react';
2-
import { Redirect } from 'react-router-dom';
3-
import { BenchmarkServices } from '../../api/BenchmarkServices';
1+
import React, { Fragment, useRef, useState } from 'react';
2+
import { RouteComponentProps } from 'react-router-dom';
43
import Header from '../Page/Header';
54
import Page from '../Page/Page';
65
import Editor from '@monaco-editor/react';
76
import useProcessInterval from '../../hooks/submissions';
87
import Result from '../Dashboard/Result';
98
import { Listbox, Transition } from '@headlessui/react';
109
import { CheckIcon, SelectorIcon } from '@heroicons/react/solid';
10+
import useBenchmarkDetail from '../../hooks/benchmark';
1111

1212
const languages = [
1313
{
@@ -30,9 +30,14 @@ const languages = [
3030
},
3131
];
3232

33-
// @ts-ignore
34-
const BenchmarkDetail = (props) => {
35-
const [benchmark, setBenchmark] = useState();
33+
type BenchmarkDetailParams = {
34+
id: string;
35+
};
36+
37+
const BenchmarkDetail = ({
38+
match,
39+
}: RouteComponentProps<BenchmarkDetailParams>) => {
40+
// const [benchmark, setBenchmark] = useState<BenchmarkModel>();
3641
const [selected, setSelected] = useState(languages[0]);
3742

3843
//Get monaco instance to access code later
@@ -60,36 +65,29 @@ const BenchmarkDetail = (props) => {
6065
result = <Result status={jobData.status} output={jobData.output} />;
6166
}
6267

63-
useEffect(() => {
64-
let isMounted = true;
65-
BenchmarkServices.getBenchmarkById(props.match.params.id)
66-
.then((response) => {
67-
if (isMounted) {
68-
// @ts-ignore
69-
setBenchmark(response);
70-
}
71-
return () => {
72-
isMounted = false;
73-
};
74-
})
75-
.catch((e) => {
76-
// @ts-ignore
77-
setBenchmark('');
78-
console.log('Error : ' + e);
79-
});
80-
}, [setBenchmark, props.match.params.id]);
68+
const {
69+
isLoading: isBenchmarkLoading,
70+
isError: isBenchmarkError,
71+
data: benchmarkData,
72+
error,
73+
} = useBenchmarkDetail(match.params.id);
74+
75+
if (isBenchmarkLoading) {
76+
return <span>Loading....</span>;
77+
}
8178

82-
if (benchmark === '' || undefined) {
83-
return <Redirect to="/404" />;
79+
if (isBenchmarkError) {
80+
if (error) {
81+
return <span>Error: {error.message}</span>;
82+
}
8483
}
8584

85+
console.log(benchmarkData);
86+
8687
return (
8788
<Page>
8889
<Header
89-
title={
90-
// @ts-ignore
91-
benchmark === undefined ? '' : benchmark.title
92-
}
90+
title={benchmarkData?.title || 'eee'}
9391
button="Back"
9492
navTo="/benchmarks"
9593
/>
@@ -194,12 +192,7 @@ const BenchmarkDetail = (props) => {
194192
</Listbox>
195193
</div>
196194
</div>
197-
<p>
198-
{
199-
// @ts-ignore
200-
benchmark === undefined ? '' : benchmark.subject
201-
}
202-
</p>
195+
<p>{benchmarkData?.subject || ''}</p>
203196
</div>
204197
</div>
205198
<div className="grid flex-1">
@@ -216,7 +209,11 @@ const BenchmarkDetail = (props) => {
216209
<button
217210
className="bg-blue-500 hover:bg-blue-700 text-white font-bold mt-2 py-2 px-4 rounded"
218211
onClick={() => {
219-
mutate(editorRef.current.getValue());
212+
mutate({
213+
code: editorRef.current.getValue(),
214+
benchmarkId:
215+
benchmarkData?.id !== undefined ? benchmarkData.id : '',
216+
});
220217
}}
221218
>
222219
Run code
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
export default class benchmarkModel {
2-
id: String | undefined;
3-
title: String | undefined;
4-
subject: String | undefined;
2+
id: string | undefined;
3+
title: string | undefined;
4+
subject: string | undefined;
55
gitUrl: null | undefined;
6-
createdAt: String | undefined;
7-
difficulty: String | undefined;
6+
createdAt: string | undefined;
7+
difficulty: string | undefined;
88
creator:
99
| {
10-
id: String | undefined;
11-
name: String | undefined;
12-
username: String | undefined;
13-
email: String | undefined;
14-
createdAt: String | undefined;
15-
updatedAt: String | undefined;
10+
id: string | undefined;
11+
name: string | undefined;
12+
username: string | undefined;
13+
email: string | undefined;
14+
createdAt: string | undefined;
15+
updatedAt: string | undefined;
1616
}
1717
| undefined;
1818
}

src/hooks/benchmark.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useQuery } from 'react-query';
2+
import axios from 'axios';
3+
import benchmarkModel from '../components/Benchmarks/BenchmarkModel';
4+
import useToken from './token';
5+
6+
export default function useBenchmarkDetail(id: string) {
7+
const { token } = useToken();
8+
9+
return useQuery<benchmarkModel, Error>(`benchmark-${id}`, async () => {
10+
if (id) {
11+
const { data } = await axios.get(
12+
`${process.env.REACT_APP_API_ENDPOINT}/benchmarks/${id}`,
13+
{
14+
headers: {
15+
Authorization: `Bearer ${token}`,
16+
},
17+
},
18+
);
19+
console.log(data);
20+
return data;
21+
}
22+
});
23+
}

src/hooks/submissions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ function useProcessInterval({
1515
const { token } = useToken();
1616

1717
// 1: Handle code submission
18-
async function createJob(code: string) {
18+
async function createJob({
19+
code,
20+
benchmarkId,
21+
}: {
22+
code: string;
23+
benchmarkId: string;
24+
}) {
1925
const response = await fetch(
2026
`${process.env.REACT_APP_API_ENDPOINT}/submissions`,
2127
{
@@ -27,6 +33,7 @@ function useProcessInterval({
2733
body: JSON.stringify({
2834
language: 'cpython3',
2935
code: code,
36+
benchmarkId: benchmarkId,
3037
}),
3138
},
3239
);

0 commit comments

Comments
 (0)