1
1
import React , { useState } from 'react' ;
2
+ import { useMutation } from 'react-query' ;
2
3
import { Link } from 'react-router-dom' ;
3
- import Label from '../utils/Label ' ;
4
+ import { createBenchmark } from '../../hooks/benchmark ' ;
4
5
import Header from '../Page/Header' ;
5
6
import Page from '../Page/Page' ;
6
- import { createBenchmark } from '../../hooks/benchmark' ;
7
+ import Label from '../utils/Label' ;
8
+ import benchmarkModel from './BenchmarkModel' ;
7
9
8
10
const CreateBenchmark : React . FC = ( ) => {
9
11
const [ status , setStatus ] = useState ( '' ) ;
10
12
const [ message , setMessage ] = useState ( '' ) ;
11
13
const [ id , setId ] = useState ( '' ) ;
12
14
15
+ const { mutate } = useMutation ( createBenchmark , {
16
+ onSuccess : ( data : benchmarkModel ) => {
17
+ setMessage ( `Your benchmark ${ data . title } has been saved` ) ;
18
+ setStatus ( 'Success' ) ;
19
+ setId ( data . id ! ) ;
20
+ } ,
21
+ onError : ( err : any ) => {
22
+ setMessage ( `Failed to create benchmark: ${ err } ` ) ;
23
+ setStatus ( 'Error' ) ;
24
+ } ,
25
+ } ) ;
26
+
13
27
const submitBenchmarkCreation = async ( event : any ) => {
14
28
event . preventDefault ( ) ;
15
29
const title = event . target . title . value ;
@@ -19,15 +33,10 @@ const CreateBenchmark: React.FC = () => {
19
33
if ( title === '' || subject === '' ) {
20
34
setMessage ( 'At least one field is blank' ) ;
21
35
setStatus ( 'Error' ) ;
22
- setId ( '' ) ;
23
36
return ;
24
37
}
25
38
26
- await createBenchmark ( title , subject , difficulty ) . then ( ( data ) => {
27
- setMessage ( 'Your benchmark' + data . title + ' have been saved' ) ;
28
- setStatus ( 'Success' ) ;
29
- setId ( data . id ! ) ;
30
- } ) ;
39
+ mutate ( { title, subject, difficulty } ) ;
31
40
} ;
32
41
33
42
return (
0 commit comments