Skip to content

Commit 08d3405

Browse files
refactor: 코드 리팩토링
1 parent 2231e64 commit 08d3405

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

src/page/todo/myTodo/MyTodo.css.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ export const emptyTodoBox = style({
208208
flexShrink: 0,
209209
borderRadius: '0.8px',
210210
background: colors.grey4,
211+
boxSizing: 'border-box',
211212
});
212213

213214
export const emptyTodoText = style({

src/page/todo/myTodo/component/TodoCheckSection/TodoCheckSection.tsx

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const TodoCheckSection = ({
3535
selectedCycle,
3636
mandalartData,
3737
onCycleClick,
38+
onTodoClick,
3839
onMandalartClick,
3940
selectedParentId,
4041
}: TodoCheckSectionProps) => {
@@ -49,37 +50,37 @@ const TodoCheckSection = ({
4950
const [localSubGoals, setLocalSubGoals] = useState<TodoItemTypes[]>([]);
5051

5152
useEffect(() => {
52-
const subGoals: TodoItemTypes[] = (subGoalResponse?.data?.subGoals ?? []).map((goal) => ({
53+
const apiSubGoals = (subGoalResponse?.data?.subGoals ?? []).map((goal) => ({
5354
id: goal.id,
54-
title: goal.title,
55+
content: goal.title,
5556
cycle: goal.cycle,
5657
isCompleted: goal.isCompleted,
57-
content: goal.title,
5858
}));
59-
setLocalSubGoals(subGoals);
59+
60+
setLocalSubGoals(apiSubGoals);
6061
}, [subGoalResponse]);
6162

63+
const updateLocalSubGoalCompletion = (id: TodoItemTypes['id'], nextValue: boolean) => {
64+
setLocalSubGoals((prev) =>
65+
prev.map((goal) => (goal.id === id ? { ...goal, isCompleted: nextValue } : goal)),
66+
);
67+
};
68+
6269
const checkSubGoalMutation = useCheckSubGoal();
6370
const uncheckSubGoalMutation = useUncheckSubGoal();
6471

6572
const handleTodoClick = (item: TodoItemTypes) => {
6673
const today = new Date().toISOString().split('T')[0];
67-
const originalCompleted = item.isCompleted;
74+
const originalCompleted = Boolean(item.isCompleted);
75+
const nextCompleted = !originalCompleted;
6876

69-
setLocalSubGoals((prev) =>
70-
prev.map((goal) =>
71-
goal.id === item.id ? { ...goal, isCompleted: !goal.isCompleted } : goal,
72-
),
73-
);
77+
updateLocalSubGoalCompletion(item.id, nextCompleted);
78+
onTodoClick({ ...item, isCompleted: nextCompleted });
7479

75-
if (originalCompleted === true) {
80+
if (originalCompleted) {
7681
uncheckSubGoalMutation.mutate(Number(item.id), {
7782
onError: () => {
78-
setLocalSubGoals((prev) =>
79-
prev.map((goal) =>
80-
goal.id === item.id ? { ...goal, isCompleted: originalCompleted } : goal,
81-
),
82-
);
83+
updateLocalSubGoalCompletion(item.id, originalCompleted);
8384
},
8485
});
8586
} else {
@@ -90,17 +91,32 @@ const TodoCheckSection = ({
9091
},
9192
{
9293
onError: () => {
93-
setLocalSubGoals((prev) =>
94-
prev.map((goal) =>
95-
goal.id === item.id ? { ...goal, isCompleted: originalCompleted } : goal,
96-
),
97-
);
94+
updateLocalSubGoalCompletion(item.id, originalCompleted);
9895
},
9996
},
10097
);
10198
}
10299
};
103100

101+
const hasTodos = localSubGoals.length > 0;
102+
const todoContainerClass = hasTodos
103+
? styles.todoCheckContainer
104+
: styles.noScrollTodoCheckContainer;
105+
106+
const renderEmptyState = () => (
107+
<div className={styles.emptyTodoBox}>
108+
<span className={styles.emptyTodoText}>해당하는 할 일이 없어요</span>
109+
</div>
110+
);
111+
112+
const renderTodoItems = () =>
113+
localSubGoals.map((todo) => (
114+
<div key={todo.id} className={styles.todoCheckLine}>
115+
<CycleChip type="display" value={todo.cycle as CycleType} />
116+
<TodoBox type="todo" items={[todo]} onItemClick={handleTodoClick} />
117+
</div>
118+
));
119+
104120
return (
105121
<section className={styles.checkSection}>
106122
<header className={styles.checkTextWrapper}>
@@ -151,25 +167,8 @@ const TodoCheckSection = ({
151167
))}
152168
</div>
153169

154-
<div
155-
className={
156-
localSubGoals.length === 0
157-
? styles.noScrollTodoCheckContainer
158-
: styles.todoCheckContainer
159-
}
160-
>
161-
{localSubGoals.length === 0 ? (
162-
<div className={styles.emptyTodoBox}>
163-
<span className={styles.emptyTodoText}>해당하는 할 일이 없어요</span>
164-
</div>
165-
) : (
166-
localSubGoals.map((todo) => (
167-
<div key={todo.id} className={styles.todoCheckLine}>
168-
<CycleChip type="display" value={todo.cycle as CycleType} />
169-
<TodoBox type="todo" items={[todo]} onItemClick={handleTodoClick} />
170-
</div>
171-
))
172-
)}
170+
<div className={todoContainerClass}>
171+
{hasTodos ? renderTodoItems() : renderEmptyState()}
173172
</div>
174173
</div>
175174
</div>

0 commit comments

Comments
 (0)