-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseMyTodo.ts
More file actions
132 lines (113 loc) · 4.07 KB
/
useMyTodo.ts
File metadata and controls
132 lines (113 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { useState, useEffect } from 'react';
import type { CycleType } from '../constant/mock';
import type { TodoItemTypes } from '@/page/todo/myTodo/component/TodoBox/TodoBox.types';
import { createDate, formatDateDot } from '@/common/util/format';
import { useGetRecommendation } from '@/api/domain/myTodo/hook/useGetRecommendation';
import { usePostRecommendation } from '@/api/domain/myTodo/hook/usePostRecommendation';
import { useDeleteRecommendation } from '@/api/domain/myTodo/hook/useDeleteRecommendation';
import { useMandalartId } from '@/common/hook/useMandalartId';
const mockSubGoals = Array.from({ length: 8 * 8 }, (_, i) => {
const parentId = Math.floor(i / 8) + 1;
const order = (i % 8) + 1;
const cycles: CycleType[] = ['DAILY', 'WEEKLY', 'ONCE'];
const cycle = cycles[i % 3];
return {
id: parentId * 100 + order,
title: `${parentId}-${order} 하위 목표`,
content: `${parentId}-${order} 하위 목표`,
cycle,
parentId,
order,
isCompleted: false,
};
});
interface UseMyTodoProps {
initialDate?: Date;
initialRecommendTodos?: TodoItemTypes[];
initialMyTodos?: TodoItemTypes[];
}
const MIN_DATE = createDate(2025, 1, 1);
const MAX_DATE = createDate(2025, 1, 31);
export const useMyTodo = ({ initialDate = createDate(2025, 7, 18) }: UseMyTodoProps = {}) => {
const MANDALART_ID = useMandalartId();
const [currentDate, setCurrentDate] = useState(initialDate);
const [selectedCycle, setSelectedCycle] = useState<CycleType>();
const [selectedParentId, setSelectedParentId] = useState<number>();
const [todos, setTodos] = useState<TodoItemTypes[]>([]);
const [recommendTodos, setRecommendTodos] = useState<TodoItemTypes[]>([]);
const formattedDate = formatDateDot(currentDate);
const { data: recommendationData, refetch } = useGetRecommendation(MANDALART_ID, formattedDate);
const { mutate: completeTodo } = usePostRecommendation();
const { mutate: deleteTodo } = useDeleteRecommendation();
useEffect(() => {
if (recommendationData?.subGoals) {
const formatted = recommendationData.subGoals.map((goal, index) => ({
id: goal.id.toString(),
content: goal.title,
isCompleted: goal.isCompleted,
cycle: goal.cycle as CycleType,
parentId: 0,
order: index,
}));
setRecommendTodos(formatted);
}
}, [recommendationData]);
useEffect(() => {
setTodos(mockSubGoals);
}, []);
const hasPreviousDate = currentDate > MIN_DATE;
const hasNextDate = currentDate < MAX_DATE;
const handleDateChange = (newDate: Date) => {
setCurrentDate(newDate);
};
const handleCycleClick = (cycle: CycleType) => {
setSelectedCycle(selectedCycle === cycle ? undefined : cycle);
};
const handleRecommendTodoClick = (item: TodoItemTypes) => {
const isCompleted = item.completed;
setRecommendTodos((prev) =>
prev.map((todo) => (todo.id === item.id ? { ...todo, completed: !todo.completed } : todo)),
);
if (isCompleted) {
deleteTodo(Number(item.id), {
onSuccess: () => {
refetch();
},
});
} else {
completeTodo(Number(item.id), {
onSuccess: () => {
refetch();
},
});
}
};
const handleMyTodoClick = (item: TodoItemTypes) => {
setTodos((prev) =>
prev.map((todo) => (todo.id === item.id ? { ...todo, completed: !todo.completed } : todo)),
);
// TODO: 할 일 완료 상태 API 호출 필요 시 추가
};
const handleMandalartClick = () => {
// TODO: 만다라트 클릭 로직
};
const filteredTodos = todos
.filter((todo) => !selectedCycle || todo.cycle === selectedCycle)
.filter((todo) => !selectedParentId || todo.parentId === selectedParentId)
.sort((a, b) => (a.parentId ?? 0) - (b.parentId ?? 0) || (a.order ?? 0) - (b.order ?? 0));
return {
currentDate,
selectedCycle,
selectedParentId,
setSelectedParentId,
todos: filteredTodos,
recommendTodos,
hasPreviousDate,
hasNextDate,
handleDateChange,
handleCycleClick,
handleRecommendTodoClick,
handleMyTodoClick,
handleMandalartClick,
};
};