Skip to content

Commit 9ebfaea

Browse files
refactor: optimize task position calculation in CreateTask component
1 parent 0240643 commit 9ebfaea

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

client/src/features/tasks/components/CreateTask.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import { UserAuth } from '@/app/context/AuthContext';
1010
import { CreateInput } from '@/shared/components/CreateInput';
1111
import { useUpdateNotifications } from '@/shared/hooks/useNotification';
1212
import { createNotification } from '@/shared/utils/createNotification';
13-
import { useCreateTask as useCreateTaskMutation } from '@/features/tasks/hooks/useTasks';
13+
import { useCreateTask as useCreateTaskMutation, useTasks } from '@/features/tasks/hooks/useTasks';
1414

1515
const CreateTask = () => {
1616
const [taskName, setTaskName] = useState('');
1717
const { listId } = useParams();
1818
const [checked, setChecked] = useState(false);
19+
const { tasks } = useTasks(listId);
1920
const inputRef = useRef(null!) as React.RefObject<HTMLInputElement>;
2021
const keydown = useShortcut(['ctrl+e']);
2122
const [date, setDate] = useState<string | undefined>(undefined);
@@ -25,20 +26,21 @@ const CreateTask = () => {
2526

2627
const createTask = () => {
2728
if (taskName && listId) {
29+
const minPosition = tasks.length > 0 ? Math.min(...tasks.map((t) => t.position)) : 1;
2830
const newTask = {
2931
id: nanoid(SIZE_ID),
3032
list_id: listId,
3133
name: replaceEmojis(taskName),
3234
description: '',
3335
checked,
3436
date: date ? format(date, 'MM-dd-yyyy') : '',
35-
position: 0,
37+
position: minPosition - 1,
3638
};
3739

3840
setDate(undefined);
3941
setChecked(false);
4042
setTaskName('');
41-
createTaskMutation.mutate({ ...newTask, position: 0 });
43+
createTaskMutation.mutate(newTask);
4244

4345
const body = createNotification({
4446
type: 'task',

0 commit comments

Comments
 (0)