@@ -7,35 +7,41 @@ import Sort from "@mui/icons-material/Sort";
7
7
import { Box , Container } from "@mui/material" ;
8
8
import Image from "next/image" ;
9
9
import Link from "next/link" ;
10
- import { useState } from "react" ;
10
+ import { useEffect , useState } from "react" ;
11
11
import $button from "../../components/design_system/button/Button.module.css" ;
12
12
import noBenchmarks from "../../public/img/no-benchmarks.png" ;
13
13
import SearchIcon from "@mui/icons-material/Search" ;
14
- import { SortDirection , SortProperty } from "@/types/global" ;
14
+ import { SortDirection , SortProperty , TaskData } from "@/types/global" ;
15
15
16
16
function Benchmarks ( ) {
17
17
const [ isPara , setIsPara ] = useState ( false ) ;
18
18
19
19
const [ sortProperty , setSortProperty ] = useState < SortProperty > ( "first_name" ) ;
20
20
const [ sortDirection , setSortDirection ] = useState < SortDirection > ( "asc" ) ;
21
21
22
+ const [ displayedTasks , setDisplayedTasks ] = useState < TaskData [ ] > ( [ ] ) ;
23
+
22
24
const { data : tasksData , isLoading } = trpc . para . getMyTasks . useQuery ( ) ;
23
25
24
26
const handleTogglePara = ( ) => {
25
27
setIsPara ( ! isPara ) ;
26
28
} ;
27
29
28
- const getSortedTasks = ( ) => {
29
- if ( ! tasksData ) return [ ] ;
30
-
31
- return [ ...tasksData ] . sort ( ( a , b ) => {
32
- if ( a [ sortProperty ] < b [ sortProperty ] )
33
- return sortDirection === "asc" ? - 1 : 1 ;
34
- if ( a [ sortProperty ] > b [ sortProperty ] )
35
- return sortDirection === "asc" ? 1 : - 1 ;
36
- return 0 ;
37
- } ) ;
38
- } ;
30
+ useEffect ( ( ) => {
31
+ if ( ! tasksData ) {
32
+ setDisplayedTasks ( [ ] ) ;
33
+ } else {
34
+ setDisplayedTasks (
35
+ [ ...tasksData ] . sort ( ( a , b ) => {
36
+ if ( a [ sortProperty ] < b [ sortProperty ] )
37
+ return sortDirection === "asc" ? - 1 : 1 ;
38
+ if ( a [ sortProperty ] > b [ sortProperty ] )
39
+ return sortDirection === "asc" ? 1 : - 1 ;
40
+ return 0 ;
41
+ } )
42
+ ) ;
43
+ }
44
+ } , [ sortDirection , sortProperty , tasksData ] ) ;
39
45
40
46
const handleSort = ( property : SortProperty ) => {
41
47
if ( property === sortProperty ) {
@@ -46,8 +52,6 @@ function Benchmarks() {
46
52
}
47
53
} ;
48
54
49
- const displayedTasks = getSortedTasks ( ) ;
50
-
51
55
if ( isLoading ) {
52
56
return < div > Loading...</ div > ;
53
57
}
0 commit comments