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' ;
4
3
import Header from '../Page/Header' ;
5
4
import Page from '../Page/Page' ;
6
5
import Editor from '@monaco-editor/react' ;
7
6
import useProcessInterval from '../../hooks/submissions' ;
8
7
import Result from '../Dashboard/Result' ;
9
8
import { Listbox , Transition } from '@headlessui/react' ;
10
9
import { CheckIcon , SelectorIcon } from '@heroicons/react/solid' ;
10
+ import useBenchmarkDetail from '../../hooks/benchmark' ;
11
11
12
12
const languages = [
13
13
{
@@ -30,9 +30,14 @@ const languages = [
30
30
} ,
31
31
] ;
32
32
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>();
36
41
const [ selected , setSelected ] = useState ( languages [ 0 ] ) ;
37
42
38
43
//Get monaco instance to access code later
@@ -60,36 +65,29 @@ const BenchmarkDetail = (props) => {
60
65
result = < Result status = { jobData . status } output = { jobData . output } /> ;
61
66
}
62
67
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
+ }
81
78
82
- if ( benchmark === '' || undefined ) {
83
- return < Redirect to = "/404" /> ;
79
+ if ( isBenchmarkError ) {
80
+ if ( error ) {
81
+ return < span > Error: { error . message } </ span > ;
82
+ }
84
83
}
85
84
85
+ console . log ( benchmarkData ) ;
86
+
86
87
return (
87
88
< Page >
88
89
< Header
89
- title = {
90
- // @ts -ignore
91
- benchmark === undefined ? '' : benchmark . title
92
- }
90
+ title = { benchmarkData ?. title || 'eee' }
93
91
button = "Back"
94
92
navTo = "/benchmarks"
95
93
/>
@@ -194,12 +192,7 @@ const BenchmarkDetail = (props) => {
194
192
</ Listbox >
195
193
</ div >
196
194
</ div >
197
- < p >
198
- {
199
- // @ts -ignore
200
- benchmark === undefined ? '' : benchmark . subject
201
- }
202
- </ p >
195
+ < p > { benchmarkData ?. subject || '' } </ p >
203
196
</ div >
204
197
</ div >
205
198
< div className = "grid flex-1" >
@@ -216,7 +209,11 @@ const BenchmarkDetail = (props) => {
216
209
< button
217
210
className = "bg-blue-500 hover:bg-blue-700 text-white font-bold mt-2 py-2 px-4 rounded"
218
211
onClick = { ( ) => {
219
- mutate ( editorRef . current . getValue ( ) ) ;
212
+ mutate ( {
213
+ code : editorRef . current . getValue ( ) ,
214
+ benchmarkId :
215
+ benchmarkData ?. id !== undefined ? benchmarkData . id : '' ,
216
+ } ) ;
220
217
} }
221
218
>
222
219
Run code
0 commit comments