|
1 |
| -import React from 'react'; |
2 |
| -import { BenchmarkServices } from '../../api/BenchmarkServices'; |
3 | 1 | import Header from '../Page/Header';
|
4 | 2 | import Page from '../Page/Page';
|
5 | 3 | import benchmarkModel from './BenchmarkModel';
|
6 | 4 | import BenchmarkRow from './BenchmarkRow';
|
| 5 | +import { useBenchmarkSList } from '../../hooks/benchmark'; |
7 | 6 |
|
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(); |
18 | 15 |
|
19 |
| - onLoadData() { |
20 |
| - BenchmarkServices.getAllBenchmarks().then((response) => { |
21 |
| - this.setState({ benchmarksJson: response }); |
22 |
| - }); |
| 16 | + if (isBenchmarksLoading) { |
| 17 | + return <span>Loading....</span>; |
23 | 18 | }
|
24 | 19 |
|
25 |
| - componentDidMount() { |
26 |
| - this.onLoadData(); |
| 20 | + if (isBenchmarksError) { |
| 21 | + if (error) { |
| 22 | + return <span>Error: {error.message}</span>; |
| 23 | + } |
27 | 24 | }
|
28 | 25 |
|
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 | + } |
70 | 29 |
|
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> |
81 | 79 | </div>
|
82 | 80 | </div>
|
83 | 81 | </div>
|
84 | 82 | </div>
|
85 |
| - </Page> |
86 |
| - ); |
87 |
| - } |
88 |
| -} |
| 83 | + </div> |
| 84 | + </Page> |
| 85 | + ); |
| 86 | +}; |
| 87 | + |
| 88 | +export default Benchmarks; |
0 commit comments