Skip to content

Commit 122282a

Browse files
Mikadowsangristan
authored andcommitted
refactor(benchmarks): convert Benchmark class component to function
1 parent 06a868b commit 122282a

File tree

4 files changed

+90
-82
lines changed

4 files changed

+90
-82
lines changed

src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { ReactQueryDevtools } from 'react-query/devtools';
44
import { BrowserRouter, Route, Switch } from 'react-router-dom';
55
import './App.css';
66
import BenchmarkDetail from './components/Benchmarks/BenchmarkDetail';
7-
import { Benchmarks } from './components/Benchmarks/Benchmarks';
87
import { CreateBenchmark } from './components/Benchmarks/CreateBenchmark';
98
import Dashboard from './components/Dashboard/Dashboard';
109
import Landing from './components/Landing';
1110
import Login from './components/Login';
1211
import Register from './components/Register';
1312
import PrivateRoute from './components/Routing/PrivateRoute';
13+
import Benchmarks from './components/Benchmarks/Benchmarks';
1414

1515
function App() {
1616
const queryClient = new QueryClient();

src/api/BenchmarkServices.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,4 @@ export class BenchmarkServices {
2424
);
2525
return res.data;
2626
}
27-
28-
static async getAllBenchmarks(): Promise<benchmarkModel[]> {
29-
return axios
30-
.get(`${process.env.REACT_APP_API_ENDPOINT}/benchmarks`)
31-
.then((response) => {
32-
return response.data;
33-
});
34-
}
3527
}
Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,88 @@
1-
import React from 'react';
2-
import { BenchmarkServices } from '../../api/BenchmarkServices';
31
import Header from '../Page/Header';
42
import Page from '../Page/Page';
53
import benchmarkModel from './BenchmarkModel';
64
import BenchmarkRow from './BenchmarkRow';
5+
import { useBenchmarkSList } from '../../hooks/benchmark';
76

8-
export class Benchmarks extends React.Component<
9-
{},
10-
{ benchmarksJson: benchmarkModel[] }
11-
> {
12-
constructor(props: any) {
13-
super(props);
14-
this.state = {
15-
benchmarksJson: [],
16-
};
17-
}
7+
const Benchmarks: React.FC = () => {
8+
let benchmarks: benchmarkModel[] = [];
9+
const {
10+
isLoading: isBenchmarksLoading,
11+
isError: isBenchmarksError,
12+
data: benchmarksData,
13+
error,
14+
} = useBenchmarkSList();
1815

19-
onLoadData() {
20-
BenchmarkServices.getAllBenchmarks().then((response) => {
21-
this.setState({ benchmarksJson: response });
22-
});
16+
if (isBenchmarksLoading) {
17+
return <span>Loading....</span>;
2318
}
2419

25-
componentDidMount() {
26-
this.onLoadData();
20+
if (isBenchmarksError) {
21+
if (error) {
22+
return <span>Error: {error.message}</span>;
23+
}
2724
}
2825

29-
render() {
30-
return (
31-
<Page>
32-
<Header title="Benchmarks" button="Create" navTo="/benchmarks/create" />
33-
<div className="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
34-
<div className="flex flex-col">
35-
<div className="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
36-
<div className="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
37-
<div className="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
38-
<table className="min-w-full divide-y divide-gray-200">
39-
<thead className="bg-gray-50">
40-
<tr>
41-
<th
42-
scope="col"
43-
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
44-
>
45-
Title
46-
</th>
47-
<th
48-
scope="col"
49-
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
50-
>
51-
Subject
52-
</th>
53-
<th
54-
scope="col"
55-
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
56-
>
57-
Difficulty
58-
</th>
59-
<th
60-
scope="col"
61-
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
62-
>
63-
Creator
64-
</th>
65-
<th scope="col" className="relative px-6 py-3">
66-
<span className="sr-only">View</span>
67-
</th>
68-
</tr>
69-
</thead>
26+
if (benchmarksData) {
27+
benchmarks = benchmarksData;
28+
}
7029

71-
<tbody className="bg-white divide-y divide-gray-200">
72-
{this.state.benchmarksJson.map((benchmark) => (
73-
<BenchmarkRow
74-
key={benchmark.id?.toString()}
75-
benchmark={benchmark}
76-
/>
77-
))}
78-
</tbody>
79-
</table>
80-
</div>
30+
return (
31+
<Page>
32+
<Header title="Benchmarks" button="Create" navTo="/benchmarks/create" />
33+
<div className="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
34+
<div className="flex flex-col">
35+
<div className="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
36+
<div className="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
37+
<div className="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
38+
<table className="min-w-full divide-y divide-gray-200">
39+
<thead className="bg-gray-50">
40+
<tr>
41+
<th
42+
scope="col"
43+
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
44+
>
45+
Title
46+
</th>
47+
<th
48+
scope="col"
49+
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
50+
>
51+
Subject
52+
</th>
53+
<th
54+
scope="col"
55+
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
56+
>
57+
Difficulty
58+
</th>
59+
<th
60+
scope="col"
61+
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
62+
>
63+
Creator
64+
</th>
65+
<th scope="col" className="relative px-6 py-3">
66+
<span className="sr-only">View</span>
67+
</th>
68+
</tr>
69+
</thead>
70+
<tbody className="bg-white divide-y divide-gray-200">
71+
{benchmarks.map((benchmark: benchmarkModel) => (
72+
<BenchmarkRow
73+
key={benchmark.id?.toString()}
74+
benchmark={benchmark}
75+
/>
76+
))}
77+
</tbody>
78+
</table>
8179
</div>
8280
</div>
8381
</div>
8482
</div>
85-
</Page>
86-
);
87-
}
88-
}
83+
</div>
84+
</Page>
85+
);
86+
};
87+
88+
export default Benchmarks;

src/hooks/benchmark.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,19 @@ export default function useBenchmarkDetail(id: string) {
2020
}
2121
});
2222
}
23+
24+
export function useBenchmarkSList() {
25+
const { token } = useToken();
26+
27+
return useQuery<benchmarkModel[], Error>(`benchmark`, async () => {
28+
const { data } = await axios.get(
29+
`${process.env.REACT_APP_API_ENDPOINT}/benchmarks`,
30+
{
31+
headers: {
32+
Authorization: `Bearer ${token}`,
33+
},
34+
},
35+
);
36+
return data;
37+
});
38+
}

0 commit comments

Comments
 (0)