Skip to content

Commit 142875f

Browse files
committed
Explicitly type getMyTasks and use useEffect instead of getSortedTasks
1 parent 06fe422 commit 142875f

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

src/backend/routers/para.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from "zod";
22
import { hasCaseManager, hasPara, router } from "../trpc";
33
import { createPara } from "../lib/db_helpers/case_manager";
4+
import { TaskData } from "@/types/global";
45

56
export const para = router({
67
getParaById: hasCaseManager
@@ -60,7 +61,7 @@ export const para = router({
6061
// TODO elsewhere: add "email_verified_at" timestamp when para first signs in with their email address (entered into db by cm)
6162
}),
6263

63-
getMyTasks: hasPara.query(async (req) => {
64+
getMyTasks: hasPara.query(async (req): Promise<TaskData[]> => {
6465
const { userId } = req.ctx.auth;
6566

6667
const result = await req.ctx.db

src/pages/benchmarks/index.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,41 @@ import Sort from "@mui/icons-material/Sort";
77
import { Box, Container } from "@mui/material";
88
import Image from "next/image";
99
import Link from "next/link";
10-
import { useState } from "react";
10+
import { useEffect, useState } from "react";
1111
import $button from "../../components/design_system/button/Button.module.css";
1212
import noBenchmarks from "../../public/img/no-benchmarks.png";
1313
import SearchIcon from "@mui/icons-material/Search";
14-
import { SortDirection, SortProperty } from "@/types/global";
14+
import { SortDirection, SortProperty, TaskData } from "@/types/global";
1515

1616
function Benchmarks() {
1717
const [isPara, setIsPara] = useState(false);
1818

1919
const [sortProperty, setSortProperty] = useState<SortProperty>("first_name");
2020
const [sortDirection, setSortDirection] = useState<SortDirection>("asc");
2121

22+
const [displayedTasks, setDisplayedTasks] = useState<TaskData[]>([]);
23+
2224
const { data: tasksData, isLoading } = trpc.para.getMyTasks.useQuery();
2325

2426
const handleTogglePara = () => {
2527
setIsPara(!isPara);
2628
};
2729

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]);
3945

4046
const handleSort = (property: SortProperty) => {
4147
if (property === sortProperty) {
@@ -46,8 +52,6 @@ function Benchmarks() {
4652
}
4753
};
4854

49-
const displayedTasks = getSortedTasks();
50-
5155
if (isLoading) {
5256
return <div>Loading...</div>;
5357
}

src/types/global.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,18 @@ export type FormEvent = React.FormEvent<HTMLFormElement>;
77

88
export type SortProperty = "first_name";
99
export type SortDirection = "asc" | "desc";
10+
11+
export interface TaskData {
12+
task_id: string;
13+
first_name: string;
14+
last_name: string;
15+
category: string;
16+
description: string;
17+
instructions: string | null;
18+
attempts_per_trial: number | null;
19+
number_of_trials: number | null;
20+
due_date: Date | null;
21+
trial_count: number | null;
22+
seen: boolean;
23+
completed_trials: string | number | bigint | null;
24+
}

0 commit comments

Comments
 (0)