Skip to content

Commit 876c9c8

Browse files
committed
refactor(benchmarks): use mutation for submission
1 parent 6bb5cb7 commit 876c9c8

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/components/Benchmarks/CreateBenchmark.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import React, { useState } from 'react';
2+
import { useMutation } from 'react-query';
23
import { Link } from 'react-router-dom';
3-
import Label from '../utils/Label';
4+
import { createBenchmark } from '../../hooks/benchmark';
45
import Header from '../Page/Header';
56
import Page from '../Page/Page';
6-
import { createBenchmark } from '../../hooks/benchmark';
7+
import Label from '../utils/Label';
8+
import benchmarkModel from './BenchmarkModel';
79

810
const CreateBenchmark: React.FC = () => {
911
const [status, setStatus] = useState('');
1012
const [message, setMessage] = useState('');
1113
const [id, setId] = useState('');
1214

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+
1327
const submitBenchmarkCreation = async (event: any) => {
1428
event.preventDefault();
1529
const title = event.target.title.value;
@@ -19,15 +33,10 @@ const CreateBenchmark: React.FC = () => {
1933
if (title === '' || subject === '') {
2034
setMessage('At least one field is blank');
2135
setStatus('Error');
22-
setId('');
2336
return;
2437
}
2538

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 });
3140
};
3241

3342
return (

src/hooks/benchmark.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useQuery } from 'react-query';
21
import axios, { AxiosResponse } from 'axios';
2+
import { useQuery } from 'react-query';
33
import benchmarkModel from '../components/Benchmarks/BenchmarkModel';
44
import useToken from './token';
55

@@ -37,22 +37,18 @@ export function useBenchmarkSList() {
3737
});
3838
}
3939

40-
export async function createBenchmark(
41-
title: string,
42-
subject: string,
43-
difficulty: string,
44-
): Promise<benchmarkModel> {
40+
export async function createBenchmark(bench: {
41+
title: string;
42+
subject: string;
43+
difficulty: string;
44+
}): Promise<benchmarkModel> {
4545
const res: AxiosResponse<benchmarkModel> = await axios.post(
4646
`${process.env.REACT_APP_API_ENDPOINT}/benchmarks`,
47+
bench,
4748
{
48-
title,
49-
subject,
50-
difficulty,
51-
},
52-
{
49+
timeout: 5000,
5350
headers: {
54-
Authorization: 'Bearer ' + localStorage.getItem('access_token'),
55-
// 'Authorization': 'Bearer ' + useToken() //
51+
Authorization: 'Bearer ' + localStorage.getItem('access_token'), // FIX ME
5652
},
5753
},
5854
);

0 commit comments

Comments
 (0)