Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions client/src/components/draggable-tasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useAppStore } from '@/store/useStore';
import { Task, TaskOrder } from '@/lib/api/interfaces';
import { TaskItem } from '@/pages/Tasks/componenets/TaskItem';
import { useMemo } from 'react';
import { SortOption } from '@/pages/Tasks/componenets/TaskFilters';

interface DraggableTaskItemProps {
task: Task;
Expand Down Expand Up @@ -57,9 +58,10 @@ function DraggableTaskItem({ task }: DraggableTaskItemProps) {

interface DraggableTasksProps {
tasks: Task[];
sortOption?: SortOption;
}

export function DraggableTasks({ tasks }: DraggableTasksProps) {
export function DraggableTasks({ tasks, sortOption = SortOption.CustomOrder }: DraggableTasksProps) {
const { selectedListId, taskOrders, reorderTasks } = useAppStore();

const sensors = useSensors(
Expand All @@ -76,16 +78,39 @@ export function DraggableTasks({ tasks }: DraggableTasksProps) {
const sortedTasks = useMemo(() => {
if (!selectedListId) return tasks;

let orderedTasks = [...tasks];
const currentOrders = taskOrders[selectedListId] || [];
const tasksWithOrder = tasks.map((task, index) => {
const orderItem = currentOrders.find(order => order.taskId === task.id);
return {
...task,
order: orderItem?.order ?? index
};
});
return tasksWithOrder.sort((a, b) => a.order - b.order);
}, [tasks, selectedListId, taskOrders]);

// First apply custom order from drag-and-drop if no sort option is active
if (sortOption === SortOption.CustomOrder) {
const tasksWithOrder = orderedTasks.map((task, index) => {
const orderItem = currentOrders.find(order => order.taskId === task.id);
return {
...task,
order: orderItem?.order ?? index
};
});
orderedTasks = tasksWithOrder.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
} else {
// Apply sorting based on the selected option
switch (sortOption) {
case SortOption.NameAsc:
orderedTasks.sort((a, b) => a.name.localeCompare(b.name));
break;
case SortOption.NameDesc:
orderedTasks.sort((a, b) => b.name.localeCompare(a.name));
break;
case SortOption.PriorityHighToLow:
orderedTasks.sort((a, b) => a.priority - b.priority);
break;
case SortOption.PriorityLowToHigh:
orderedTasks.sort((a, b) => b.priority - a.priority);
break;
}
}

return orderedTasks;
}, [tasks, selectedListId, taskOrders, sortOption]);

const handleDragEnd = (event: DragEndEvent) => {
const { active, over } = event;
Expand Down
4 changes: 3 additions & 1 deletion client/src/pages/Tasks/componenets/TaskFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
SelectValue,
} from "@/components/ui/select"

export enum SortOption {

Check warning on line 11 in client/src/pages/Tasks/componenets/TaskFilters.tsx

View workflow job for this annotation

GitHub Actions / lint

Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components

Check warning on line 11 in client/src/pages/Tasks/componenets/TaskFilters.tsx

View workflow job for this annotation

GitHub Actions / lint

Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components
CustomOrder = "custom_order",
NameAsc = "name_asc",
NameDesc = "name_desc",
PriorityLowToHigh = "priority_low_to_high",
Expand Down Expand Up @@ -68,13 +69,14 @@
</Select>

<Select
defaultValue={SortOption.NameAsc}
defaultValue={SortOption.CustomOrder}
onValueChange={(value) => onSortChange(value as SortOption)}
>
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Sort by" />
</SelectTrigger>
<SelectContent>
<SelectItem value={SortOption.CustomOrder}>Custom Order</SelectItem>
<SelectItem value={SortOption.NameAsc}>Name (A-Z)</SelectItem>
<SelectItem value={SortOption.NameDesc}>Name (Z-A)</SelectItem>
<SelectItem value={SortOption.PriorityLowToHigh}>
Expand Down
7 changes: 5 additions & 2 deletions client/src/pages/Tasks/componenets/TaskList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import TaskCompletedViewList from "./TaskCompletedViewList"
import AllPendingTasksList from "./AllPendingTasksList"
import NoPendingTasks from "@/components/no-pending-tasks"
import { DraggableTasks } from "@/components/draggable-tasks"
import { SortOption } from "./TaskFilters"

interface TaskListProps {
tasks: Task[]
isCompletedView?: boolean
isAllPendingView?: boolean
sortOption?: SortOption
}

export function TaskList({ tasks, isCompletedView, isAllPendingView }: TaskListProps) {
export function TaskList({ tasks, isCompletedView,
isAllPendingView, sortOption }: TaskListProps) {
const pendingTasks = tasks.filter(task => !task.isCompleted)
const completedTasks = tasks.filter(task => task.isCompleted)

Expand All @@ -28,7 +31,7 @@ export function TaskList({ tasks, isCompletedView, isAllPendingView }: TaskListP
return (
<div className="container space-y-4">
<div className="container space-y-2">
<DraggableTasks tasks={pendingTasks} />
<DraggableTasks tasks={pendingTasks} sortOption={sortOption} />
</div>
<Accordion type="multiple" className="space-y-1 border-0">
{completedTasks.length > 0 && (
Expand Down
1 change: 1 addition & 0 deletions client/src/pages/Tasks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export function Tasks() {
tasks={filteredAndSortedTasks}
isCompletedView={listId === 'completed'}
isAllPendingView={listId === 'all'}
sortOption={sort}
/>
</div>

Expand Down
Loading