Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

fix(tasks): refetch tasks with group-aware ordering on group-by change

Khuram Niazf4497084ba9b
diff --git a/__tests__/TasksContainer.spec.tsx b/__tests__/TasksContainer.spec.tsx
index 4fcf25d..b04e0cc 100644
--- a/__tests__/TasksContainer.spec.tsx
+++ b/__tests__/TasksContainer.spec.tsx
@@ -203,7 +203,7 @@ describe("TasksContainer Component", () => {
expect(getTasksList).toHaveBeenCalledWith(1, 25, undefined, [
"in-progress",
"needs-attention",
- ], undefined);
+ ], undefined, "none");
});
await waitFor(() => {
@@ -704,7 +704,7 @@ describe("TasksContainer Component", () => {
expect(getTasksList).toHaveBeenCalledWith(1, 25, "type-1", [
"in-progress",
"needs-attention",
- ], undefined);
+ ], undefined, "none");
});
});
@@ -722,7 +722,7 @@ describe("TasksContainer Component", () => {
await waitFor(() => {
expect(getTasksList).toHaveBeenCalledWith(1, 25, "type-1", [
"needs-attention",
- ], undefined);
+ ], undefined, "none");
});
});
@@ -736,6 +736,13 @@ describe("TasksContainer Component", () => {
await waitFor(() => {
expect(groupBySelect).toHaveTextContent("Due Date");
});
+
+ await waitFor(() => {
+ expect(getTasksList).toHaveBeenLastCalledWith(1, 25, "type-1", [
+ "in-progress",
+ "needs-attention",
+ ], undefined, "dueDate");
+ });
});
});
@@ -792,7 +799,7 @@ describe("TasksContainer Component", () => {
expect(getTasksList).toHaveBeenCalledWith(2, 25, undefined, [
"in-progress",
"needs-attention",
- ], undefined);
+ ], undefined, "none");
});
});
@@ -909,7 +916,8 @@ describe("TasksContainer Component", () => {
25,
undefined,
["in-progress", "needs-attention"],
- undefined
+ undefined,
+ "none"
);
});
@@ -935,7 +943,8 @@ describe("TasksContainer Component", () => {
25,
undefined,
["in-progress", "needs-attention"],
- "Task 1"
+ "Task 1",
+ "none"
);
});
});
@@ -1044,7 +1053,8 @@ describe("TasksContainer Component", () => {
25,
undefined,
["needs-attention"],
- undefined
+ undefined,
+ "none"
);
});
@@ -1067,7 +1077,8 @@ describe("TasksContainer Component", () => {
25,
undefined,
["needs-attention"],
- "Task"
+ "Task",
+ "none"
);
});
});
@@ -1089,7 +1100,8 @@ describe("TasksContainer Component", () => {
25,
"type-1",
["in-progress", "needs-attention"],
- undefined
+ undefined,
+ "none"
);
});
@@ -1111,7 +1123,8 @@ describe("TasksContainer Component", () => {
25,
"type-1",
["in-progress", "needs-attention"],
- "Development"
+ "Development",
+ "none"
);
});
});
@@ -1157,7 +1170,8 @@ describe("TasksContainer Component", () => {
25,
undefined,
["in-progress", "needs-attention"],
- "Task"
+ "Task",
+ "none"
);
});
@@ -1177,7 +1191,8 @@ describe("TasksContainer Component", () => {
25,
undefined,
["in-progress", "needs-attention"],
- "Task"
+ "Task",
+ "none"
);
});
});
@@ -1213,7 +1228,8 @@ describe("TasksContainer Component", () => {
25,
undefined,
["in-progress", "needs-attention"],
- "first search"
+ "first search",
+ "none"
);
});
@@ -1238,7 +1254,8 @@ describe("TasksContainer Component", () => {
25,
undefined,
["in-progress", "needs-attention"],
- "second search"
+ "second search",
+ "none"
);
});
});
@@ -1278,7 +1295,8 @@ describe("TasksContainer Component", () => {
25,
undefined,
["in-progress", "needs-attention"],
- "Task"
+ "Task",
+ "none"
);
});
});
@@ -1320,7 +1338,8 @@ describe("TasksContainer Component", () => {
25,
undefined,
["in-progress", "needs-attention"],
- "Task"
+ "Task",
+ "none"
);
});
diff --git a/__tests__/TasksPageClient.spec.tsx b/__tests__/TasksPageClient.spec.tsx
index 3d285ab..382cefa 100644
--- a/__tests__/TasksPageClient.spec.tsx
+++ b/__tests__/TasksPageClient.spec.tsx
@@ -386,7 +386,8 @@ describe("TasksPageClient Component", () => {
25,
"type-1",
expect.any(Array),
- undefined
+ undefined,
+ "none"
);
});
});
@@ -418,7 +419,8 @@ describe("TasksPageClient Component", () => {
25,
"type-1",
expect.any(Array),
- undefined
+ undefined,
+ "none"
);
});
@@ -435,7 +437,8 @@ describe("TasksPageClient Component", () => {
25,
undefined,
expect.any(Array),
- undefined
+ undefined,
+ "none"
);
});
});
diff --git a/__tests__/task.actions.spec.ts b/__tests__/task.actions.spec.ts
index 148b4be..5b8216e 100644
--- a/__tests__/task.actions.spec.ts
+++ b/__tests__/task.actions.spec.ts
@@ -190,6 +190,67 @@ describe("taskActions", () => {
});
});
+ it("should order by dueDate when grouping by due date", async () => {
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+
+ const mockTasks = [mockTask];
+ (prisma.task.findMany as any).mockResolvedValue(mockTasks);
+ (prisma.task.count as any).mockResolvedValue(1);
+
+ await getTasksList(1, 25, undefined, undefined, undefined, "dueDate");
+
+ expect(prisma.task.findMany).toHaveBeenCalledWith({
+ where: { userId: mockUser.id },
+ include: {
+ activityType: true,
+ activities: {
+ select: { id: true },
+ },
+ },
+ orderBy: [
+ { dueDate: "asc" },
+ { priority: "desc" },
+ { createdAt: "desc" },
+ ],
+ skip: 0,
+ take: 25,
+ });
+ });
+
+ it("should order by activityType label when grouping by activity type", async () => {
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+
+ const mockTasks = [mockTask];
+ (prisma.task.findMany as any).mockResolvedValue(mockTasks);
+ (prisma.task.count as any).mockResolvedValue(1);
+
+ await getTasksList(
+ 1,
+ 25,
+ undefined,
+ undefined,
+ undefined,
+ "activityType",
+ );
+
+ expect(prisma.task.findMany).toHaveBeenCalledWith({
+ where: { userId: mockUser.id },
+ include: {
+ activityType: true,
+ activities: {
+ select: { id: true },
+ },
+ },
+ orderBy: [
+ { activityType: { label: "asc" } },
+ { priority: "desc" },
+ { createdAt: "desc" },
+ ],
+ skip: 0,
+ take: 25,
+ });
+ });
+
it("should return error when user is not authenticated", async () => {
(getCurrentUser as any).mockResolvedValue(null);
diff --git a/src/actions/task.actions.ts b/src/actions/task.actions.ts
index 00aa1a9..eb30591 100644
--- a/src/actions/task.actions.ts
+++ b/src/actions/task.actions.ts
@@ -1,18 +1,50 @@
"use server";
import prisma from "@/lib/db";
import { handleError } from "@/lib/utils";
-import { Task, TaskStatus } from "@/models/task.model";
+import { Task, TaskGroupBy, TaskStatus } from "@/models/task.model";
import { AddTaskFormSchema } from "@/models/addTaskForm.schema";
import { getCurrentUser } from "@/utils/user.utils";
import { APP_CONSTANTS } from "@/lib/constants";
import { z } from "zod";
+function getTasksOrderBy(groupBy?: TaskGroupBy) {
+ switch (groupBy) {
+ case "dueDate":
+ return [
+ { dueDate: "asc" as const },
+ { priority: "desc" as const },
+ { createdAt: "desc" as const },
+ ];
+ case "createdDate":
+ return [{ createdAt: "desc" as const }, { priority: "desc" as const }];
+ case "updatedDate":
+ return [
+ { updatedAt: "desc" as const },
+ { priority: "desc" as const },
+ { createdAt: "desc" as const },
+ ];
+ case "activityType":
+ return [
+ { activityType: { label: "asc" as const } },
+ { priority: "desc" as const },
+ { createdAt: "desc" as const },
+ ];
+ default:
+ return [
+ { priority: "desc" as const },
+ { createdAt: "desc" as const },
+ { updatedAt: "desc" as const },
+ ];
+ }
+}
+
export const getTasksList = async (
page: number = 1,
limit: number = APP_CONSTANTS.RECORDS_PER_PAGE,
filter?: string,
statusFilter?: TaskStatus[],
- search?: string
+ search?: string,
+ groupBy?: TaskGroupBy
): Promise<any | undefined> => {
try {
const user = await getCurrentUser();
@@ -52,7 +84,7 @@ export const getTasksList = async (
select: { id: true },
},
},
- orderBy: [{ priority: "desc" }, { createdAt: "desc" }, { updatedAt: "desc" }],
+ orderBy: getTasksOrderBy(groupBy),
skip: offset,
take: limit,
}),
diff --git a/src/components/tasks/TasksContainer.tsx b/src/components/tasks/TasksContainer.tsx
index d7bb4fb..4c70750 100644
--- a/src/components/tasks/TasksContainer.tsx
+++ b/src/components/tasks/TasksContainer.tsx
@@ -19,7 +19,12 @@ import {
startActivityFromTask,
} from "@/actions/task.actions";
import { toastActionResult, toastError } from "@/lib/toast";
-import { Task, TaskStatus, TASK_STATUSES } from "@/models/task.model";
+import {
+ Task,
+ TaskGroupBy,
+ TaskStatus,
+ TASK_STATUSES,
+} from "@/models/task.model";
import {
Select,
SelectContent,
@@ -72,9 +77,7 @@ function TasksContainer({
const [editTask, setEditTask] = useState<Task | null>(null);
const [initialLoading, setInitialLoading] = useState(false);
const [loadingMore, setLoadingMore] = useState(false);
- const [groupBy, setGroupBy] = useState<
- "none" | "createdDate" | "dueDate" | "updatedDate" | "activityType"
- >("none");
+ const [groupBy, setGroupBy] = useState<TaskGroupBy>("none");
const [dialogOpen, setDialogOpen] = useState(false);
const [statusFilter, setStatusFilter] = useState<TaskStatus[]>(
DEFAULT_STATUS_FILTER,
@@ -98,6 +101,7 @@ function TasksContainer({
filter?: string,
statuses?: TaskStatus[],
search?: string,
+ group?: TaskGroupBy,
) => {
if (pageNum === 1) setInitialLoading(true);
else setLoadingMore(true);
@@ -107,6 +111,7 @@ function TasksContainer({
filter,
statuses,
search,
+ group,
);
if (success && data) {
setTasks((prev) => (pageNum === 1 ? data : [...prev, ...data]));
@@ -122,9 +127,15 @@ function TasksContainer({
);
const reloadTasks = useCallback(async () => {
- await loadTasks(1, filterKey, statusFilter, searchTerm || undefined);
+ await loadTasks(
+ 1,
+ filterKey,
+ statusFilter,
+ searchTerm || undefined,
+ groupBy,
+ );
onTasksChanged?.();
- }, [loadTasks, filterKey, statusFilter, searchTerm, onTasksChanged]);
+ }, [loadTasks, filterKey, statusFilter, searchTerm, groupBy, onTasksChanged]);
const onDeleteTask = async (taskId: string) => {
const result = await deleteTaskById(taskId);
@@ -194,9 +205,15 @@ function TasksContainer({
useEffect(() => {
(async () =>
- await loadTasks(1, filterKey, statusFilter, searchTerm || undefined))();
+ await loadTasks(
+ 1,
+ filterKey,
+ statusFilter,
+ searchTerm || undefined,
+ groupBy,
+ ))();
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [loadTasks, filterKey, statusFilter]);
+ }, [loadTasks, filterKey, statusFilter, groupBy]);
// Debounced search effect
useEffect(() => {
@@ -206,7 +223,7 @@ function TasksContainer({
if (searchTerm === "" && !hasSearched.current) return;
const timer = setTimeout(() => {
- loadTasks(1, filterKey, statusFilter, searchTerm || undefined);
+ loadTasks(1, filterKey, statusFilter, searchTerm || undefined, groupBy);
}, 300);
return () => clearTimeout(timer);
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -225,7 +242,13 @@ function TasksContainer({
!loadingMore &&
tasks.length < totalTasks
) {
- loadTasks(page + 1, filterKey, statusFilter, searchTerm || undefined);
+ loadTasks(
+ page + 1,
+ filterKey,
+ statusFilter,
+ searchTerm || undefined,
+ groupBy,
+ );
}
},
{ threshold: APP_CONSTANTS.INTERSECTION_OBSERVER_THRESHOLD },
@@ -240,20 +263,14 @@ function TasksContainer({
filterKey,
statusFilter,
searchTerm,
+ groupBy,
initialLoading,
loadingMore,
loadTasks,
]);
const onGroupByChange = (value: string) => {
- setGroupBy(
- value as
- | "none"
- | "createdDate"
- | "dueDate"
- | "updatedDate"
- | "activityType",
- );
+ setGroupBy(value as TaskGroupBy);
};
const toggleStatusFilter = (status: TaskStatus) => {
diff --git a/src/components/tasks/TasksTable.tsx b/src/components/tasks/TasksTable.tsx
index e0d4f88..b9cd351 100644
--- a/src/components/tasks/TasksTable.tsx
+++ b/src/components/tasks/TasksTable.tsx
@@ -45,7 +45,12 @@ import {
} from "../ui/dropdown-menu";
import { Button } from "../ui/button";
import { useState } from "react";
-import { Task, TASK_STATUSES, TaskStatus } from "@/models/task.model";
+import {
+ Task,
+ TASK_STATUSES,
+ TaskGroupBy,
+ TaskStatus,
+} from "@/models/task.model";
import { DeleteAlertDialog } from "../DeleteAlertDialog";
type TasksTableProps = {
@@ -54,7 +59,7 @@ type TasksTableProps = {
editTask: (id: string) => void;
onChangeTaskStatus: (id: string, status: TaskStatus) => void;
onStartActivity: (id: string) => void;
- groupBy?: "none" | "createdDate" | "dueDate" | "updatedDate" | "activityType";
+ groupBy?: TaskGroupBy;
};
type PriorityLevel = "low" | "medium" | "high" | "critical";
diff --git a/src/models/task.model.ts b/src/models/task.model.ts
index f31306f..1d5b148 100644
--- a/src/models/task.model.ts
+++ b/src/models/task.model.ts
@@ -9,6 +9,13 @@ export const TASK_STATUSES = {
export type TaskStatus = keyof typeof TASK_STATUSES;
+export type TaskGroupBy =
+ | "none"
+ | "createdDate"
+ | "dueDate"
+ | "updatedDate"
+ | "activityType";
+
export interface Task {
id: string;
userId: string;