Skip to content

Commit d3c2867

Browse files
Merge pull request #196 from SOPT-36-NINEDOT/refactor/#192/myTodoBottom
[Refactor]: 나의 할 일 뷰 하단 리팩토링
2 parents f2d95f3 + f8f3221 commit d3c2867

File tree

6 files changed

+381
-293
lines changed

6 files changed

+381
-293
lines changed

src/page/edit/component/Content/Content.tsx

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import { useMandalartHover } from '../../hook/useMandalartHover';
88
import Mandalart from '@/common/component/Mandalart/Mandalart';
99
import { useMandalAll } from '@/api/domain/mandalAll/hook';
1010
import { useSubGoals, useUpdateSubGoal } from '@/api/domain/edit/hook';
11+
import { useMandalartId } from '@/common/hook/useMandalartId';
1112
import type { CoreGoal, SubGoal } from '@/page/mandal/types/mandal';
1213

1314
interface ContentProps {
1415
isEditing: boolean;
1516
setIsEditing: (value: boolean) => void;
1617
}
1718

18-
const MANDAL_ID = 1;
19-
2019
const Content = ({ isEditing, setIsEditing }: ContentProps) => {
21-
const { data: mandalartData, isLoading: isMandalLoading } = useMandalAll(MANDAL_ID);
20+
const mandalartId = useMandalartId();
21+
const { data: mandalartData, isLoading: isMandalLoading } = useMandalAll(mandalartId);
2222
const {
2323
isHovered,
2424
hoveredGoal,
@@ -31,21 +31,22 @@ const Content = ({ isEditing, setIsEditing }: ContentProps) => {
3131
mandalartData,
3232
});
3333

34-
const { isLoading: isSubGoalsLoading } = useSubGoals(MANDAL_ID, hoveredGoal?.id, undefined, {
35-
enabled: !!hoveredGoal,
34+
const activeCoreGoalId = hoveredGoal?.id && hoveredGoal.id > 0 ? hoveredGoal.id : undefined;
35+
const { isLoading: isSubGoalsLoading } = useSubGoals(mandalartId, activeCoreGoalId, undefined, {
36+
enabled: !!activeCoreGoalId,
3637
});
37-
const { mutate: updateGoal } = useUpdateSubGoal(MANDAL_ID);
38+
const { mutate: updateGoal } = useUpdateSubGoal(mandalartId);
3839

39-
const isLoading = isMandalLoading || (hoveredGoal && isSubGoalsLoading);
40+
const isLoading = !mandalartId || isMandalLoading || (hoveredGoal && isSubGoalsLoading);
4041

4142
const handleSave = useCallback(() => {
42-
if (!hoveredGoal) {
43+
if (!hoveredGoal || !mandalartId) {
4344
return;
4445
}
4546

4647
const validSubGoals = hoveredGoal.subGoals.filter((subGoal) => subGoal.title);
4748

48-
if (validSubGoals.length > 0) {
49+
if (validSubGoals.length > 0 && hoveredGoal.id > 0) {
4950
updateGoal({
5051
coreGoal: {
5152
position: hoveredGoal.position,
@@ -61,7 +62,7 @@ const Content = ({ isEditing, setIsEditing }: ContentProps) => {
6162

6263
setIsEditing(false);
6364
setIsHovered(true);
64-
}, [hoveredGoal, updateGoal, setIsEditing, setIsHovered]);
65+
}, [hoveredGoal, updateGoal, setIsEditing, setIsHovered, mandalartId]);
6566

6667
useEffect(() => {
6768
if (!isEditing && hoveredGoal) {
@@ -87,17 +88,19 @@ const Content = ({ isEditing, setIsEditing }: ContentProps) => {
8788
const validSubGoals = hoveredGoal.subGoals.filter((subGoal) => subGoal.title);
8889

8990
if (validSubGoals.length > 0) {
90-
updateGoal({
91-
coreGoal: {
92-
position: hoveredGoal.position,
93-
title: hoveredGoal.title,
94-
},
95-
subGoals: validSubGoals.map((subGoal) => ({
96-
position: subGoal.position,
97-
title: subGoal.title,
98-
cycle: subGoal.cycle || 'DAILY',
99-
})),
100-
});
91+
if (mandalartId && hoveredGoal.id > 0) {
92+
updateGoal({
93+
coreGoal: {
94+
position: hoveredGoal.position,
95+
title: hoveredGoal.title,
96+
},
97+
subGoals: validSubGoals.map((subGoal) => ({
98+
position: subGoal.position,
99+
title: subGoal.title,
100+
cycle: subGoal.cycle || 'DAILY',
101+
})),
102+
});
103+
}
101104
}
102105

103106
setHoveredGoal(goal);
@@ -110,7 +113,16 @@ const Content = ({ isEditing, setIsEditing }: ContentProps) => {
110113
setIsHovered(true);
111114
setIsEditing(true);
112115
},
113-
[mandalartData, isEditing, hoveredGoal, updateGoal, setHoveredGoal, setIsHovered, setIsEditing],
116+
[
117+
mandalartData,
118+
isEditing,
119+
hoveredGoal,
120+
updateGoal,
121+
setHoveredGoal,
122+
setIsHovered,
123+
setIsEditing,
124+
mandalartId,
125+
],
114126
);
115127

116128
const handleTitleChange = useCallback(
@@ -215,10 +227,10 @@ const Content = ({ isEditing, setIsEditing }: ContentProps) => {
215227
position={hoveredGoal.position}
216228
id={hoveredGoal.id}
217229
onSubGoalsChange={handleSubGoalsChange}
218-
mandalartId={MANDAL_ID}
230+
mandalartId={mandalartId}
219231
/>
220232
);
221-
}, [isLoading, hoveredGoal, handleTitleChange, handleSubGoalsChange]);
233+
}, [isLoading, hoveredGoal, handleTitleChange, handleSubGoalsChange, mandalartId]);
222234

223235
const renderSubGoals = useCallback(() => {
224236
if (isLoading) {
@@ -274,7 +286,7 @@ const Content = ({ isEditing, setIsEditing }: ContentProps) => {
274286
onMouseLeave={handleMouseLeave}
275287
onClick={handleMandalartClick}
276288
>
277-
{isMandalLoading ? (
289+
{!mandalartId || isMandalLoading ? (
278290
<div className={styles.loadingContainer}>로딩중...</div>
279291
) : (
280292
<Mandalart type="TODO_EDIT" data={mainGoalData} onGoalClick={handleGoalClick} />

0 commit comments

Comments
 (0)