-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLowerTodo.tsx
More file actions
545 lines (501 loc) · 18.4 KB
/
LowerTodo.tsx
File metadata and controls
545 lines (501 loc) · 18.4 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import * as styles from './LowerTodo.css';
import TodoFields from './component/TodoFields';
import { EMPTY_BOOL_ARR } from './mock';
import { isValidSubGoal, truncateText, getFirstValidGoalIndex } from './util';
import { PATH } from '@/route';
import { IcSmallNext } from '@/assets/svg';
import GradientBackground from '@/common/component/Background/GradientBackground';
import Tooltip from '@/common/component/Tooltip/Tooltip';
import { useOverlayModal } from '@/common/hook/useOverlayModal';
import AiRecommendModal from '@/common/component/AiRecommendModal/AiRecommendModal';
import AiFailModal from '@/common/component/AiFailModal/AiFailModal';
import Mandalart from '@/common/component/Mandalart/Mandalart';
import { useCoreGoals } from '@/api/domain/lowerTodo/hook/useCoreGoals';
import { useOverallGoal } from '@/api/domain/lowerTodo/hook/useOverallGoal';
import { useSubGoals } from '@/api/domain/lowerTodo/hook/useSubGoals';
import { useCreateSubGoal } from '@/api/domain/lowerTodo/hook/useCreateSubGoal';
import { useUpdateSubGoal } from '@/api/domain/lowerTodo/hook/useUpdateSubGoal';
import { useDeleteSubGoal } from '@/api/domain/lowerTodo/hook/useDeleteSubGoal';
import { useAiRecommendSubGoal } from '@/api/domain/lowerTodo/hook/useAiRecommendSubGoal';
import { completeMandalart } from '@/api/domain/lowerTodo';
import { postAiRecommendSubGoals } from '@/api/domain/lowerTodo';
import { useMandalartId } from '@/common/hook/useMandalartId';
interface LowerTodoProps {
userName?: string;
mainGoal?: string;
}
interface TodoItem {
title: string;
cycle: 'DAILY' | 'WEEKLY' | 'ONCE';
}
const LowerTodo = ({ userName = '김도트' }: LowerTodoProps) => {
const navigate = useNavigate();
const { openModal, closeModal } = useOverlayModal();
const [selectedGoalIndex, setSelectedGoalIndex] = useState(0);
const [allTodos, setAllTodos] = useState<TodoItem[][]>(
Array(8)
.fill(null)
.map(() => Array(8).fill({ title: '', cycle: 'DAILY' })),
);
const [aiUsedByGoal] = useState([...EMPTY_BOOL_ARR]);
const [tooltipOpenArr, setTooltipOpenArr] = useState(Array(8).fill(true));
const [subGoalIdsByPosition, setSubGoalIdsByPosition] = useState<{
[position: number]: number | null;
}>({});
const mandalartId = useMandalartId();
const { data: coreGoalsData } = useCoreGoals(mandalartId);
const { data: overallGoalData } = useOverallGoal(mandalartId);
const selectedCoreGoalId =
selectedGoalIndex !== -1 && coreGoalsData
? coreGoalsData.data.coreGoals[selectedGoalIndex]?.id
: undefined;
const { data: subGoalsData } = useSubGoals({
mandalartId,
coreGoalId: selectedCoreGoalId,
});
const createSubGoalMutation = useCreateSubGoal(selectedCoreGoalId ?? 0);
const updateSubGoalMutation = useUpdateSubGoal(selectedCoreGoalId ?? 0);
const deleteSubGoalMutation = useDeleteSubGoal();
const aiRecommendMutation = useAiRecommendSubGoal(selectedCoreGoalId ?? 0);
const recommendAiSubGoal = aiRecommendMutation.mutate;
const handleSaveSubGoalSync = (todo: TodoItem, position: number, done?: () => void) => {
console.log('handleSaveSubGoalSync called:', {
todo,
position,
subGoalIdsByPosition,
selectedGoalIndex,
});
const subGoalId = subGoalIdsByPosition[position];
const trimmedTitle = todo.title.trim();
// 1. subGoalId가 있고 title이 빈 문자열이면 DELETE 호출
if (subGoalId && !trimmedTitle) {
deleteSubGoalMutation.mutate(subGoalId, {
onSuccess: () => {
setAllTodos((prev) => {
const newTodos = [...prev];
newTodos[selectedGoalIndex][position] = { title: '', cycle: 'DAILY' };
return newTodos;
});
setSubGoalIdsByPosition((prev) => ({ ...prev, [position]: null }));
if (done) {
done();
}
},
onError: () => {
alert('하위 목표 삭제 중 오류가 발생했습니다.');
if (done) {
done();
}
},
});
return;
}
// 2. subGoalId가 있고 title이 있으면 PATCH 호출 (주기만 바꿔도 포함)
if (subGoalId && trimmedTitle) {
updateSubGoalMutation.mutate(
{
title: trimmedTitle,
cycle: todo.cycle,
},
{
onSuccess: () => {
setAllTodos((prev) => {
const newTodos = [...prev];
newTodos[selectedGoalIndex][position] = { ...todo };
return newTodos;
});
if (done) {
done();
}
},
onError: () => {
alert('하위 목표 수정 중 오류가 발생했습니다.');
if (done) {
done();
}
},
},
);
return;
}
// 3. subGoalId가 없고 title이 있으면 POST 호출
if (!subGoalId && trimmedTitle) {
createSubGoalMutation.mutate(
{
title: trimmedTitle,
position: position + 1,
cycle: todo.cycle,
},
{
onSuccess: (res) => {
setSubGoalIdsByPosition((prev) => ({ ...prev, [position]: res.id }));
setAllTodos((prev) => {
const newTodos = [...prev];
newTodos[selectedGoalIndex][position] = { ...todo };
return newTodos;
});
if (done) {
done();
}
},
onError: (err) => {
const error = err as any;
if (error.response?.data?.message) {
alert(error.response.data.message);
} else {
alert('하위 목표 저장 중 오류가 발생했습니다.');
}
if (done) {
done();
}
},
},
);
return;
}
// 4. subGoalId가 없고 title도 없으면 아무것도 안 함
if (done) {
done();
}
};
const handleTodoChange = (newTodos: TodoItem[]) => {
console.log('handleTodoChange called:', { newTodos, selectedGoalIndex });
setAllTodos((prev) => {
const newState = [...prev];
newState[selectedGoalIndex] = [...newTodos];
console.log('setAllTodos in handleTodoChange, newState:', newState);
return newState;
});
};
useEffect(() => {
console.log('useEffect - coreGoalsData 변경됨');
console.log('coreGoalsData:', coreGoalsData);
if (coreGoalsData && coreGoalsData.data.coreGoals.length > 0) {
const subGoals = coreGoalsData.data.coreGoals.map((goal) => goal.title);
setSelectedGoalIndex(getFirstValidGoalIndex(subGoals));
}
}, [coreGoalsData]);
useEffect(() => {
console.log('useEffect - subGoalsData 변경됨');
console.log('subGoalsData:', subGoalsData);
if (
subGoalsData &&
selectedGoalIndex !== -1 &&
allTodos[selectedGoalIndex].every((todo) => !todo.title?.trim())
) {
setAllTodos((prev) => {
// 이미 값이 있으면 덮어쓰지 않음
if (prev[selectedGoalIndex].some((todo) => todo.title.trim() !== '')) {
console.log('useEffect: 값이 이미 있으므로 덮어쓰지 않음', prev);
return prev;
}
const newTodos = [...prev];
const apiTodos = subGoalsData.data.subGoals.map((subGoal) => ({
title: subGoal.title,
cycle: subGoal.cycle,
}));
const filledTodos = Array(8)
.fill({ title: '', cycle: 'DAILY' })
.map((_, idx) => apiTodos[idx] || { title: '', cycle: 'DAILY' });
const prevFilled = prev[selectedGoalIndex];
const isSame =
filledTodos.length === prevFilled.length &&
filledTodos.every(
(t, i) => t.title === prevFilled[i].title && t.cycle === prevFilled[i].cycle,
);
if (isSame) {
console.log('useEffect: filledTodos와 prevFilled가 같으므로 덮어쓰지 않음', prev);
return prev;
}
newTodos[selectedGoalIndex] = filledTodos;
console.log('useEffect: setAllTodos로 덮어씀', newTodos);
return newTodos;
});
}
}, [subGoalsData, selectedGoalIndex, allTodos]);
useEffect(() => {
if (coreGoalsData && selectedGoalIndex !== -1) {
const todos = allTodos[selectedGoalIndex];
if (todos && todos.every((todo) => todo.title.trim() !== '')) {
setTooltipOpenArr((arr) => arr.map((v, i) => (i === selectedGoalIndex ? false : v)));
}
}
}, [coreGoalsData, allTodos, selectedGoalIndex]);
if (!coreGoalsData) {
console.warn('coreGoalsData가 비어 있습니다!');
return null;
}
const mandalartOrder = [0, 1, 2, 5, 8, 7, 6, 3];
const coreGoalsGrid = Array(9)
.fill(null)
.map(() => ({ title: '', id: 0, position: 0, aiGeneratable: false }));
if (coreGoalsData.data.coreGoals && coreGoalsData.data.coreGoals.length > 0) {
coreGoalsData.data.coreGoals
.filter((goal) => {
const gridIdx = mandalartOrder[goal.position - 1];
return goal.title !== (overallGoalData?.title || '') && gridIdx !== 4;
})
.forEach((goal) => {
const gridIdx = mandalartOrder[goal.position - 1];
coreGoalsGrid[gridIdx] = goal;
});
}
coreGoalsGrid[4] = {
title: overallGoalData?.title || '',
id: 0,
position: 0,
aiGeneratable: false,
};
// 디버깅: coreGoalsGrid/subGoals 생성 직후 값 점검
console.log(
'coreGoalsGrid 생성 후:',
coreGoalsGrid.map((g) => g.title),
);
console.log(
'subGoals 생성 후:',
coreGoalsGrid.map((g) => g.title),
);
const subGoals = coreGoalsGrid.map((goal) => goal.title);
const getGridIndex = (selectedGoalIndex: number) => {
return selectedGoalIndex < 4 ? selectedGoalIndex : selectedGoalIndex + 1;
};
const selectedGridIndex = getGridIndex(selectedGoalIndex);
const todos = Array(8)
.fill(null)
.map(() => ({ title: '', cycle: 'DAILY' as 'DAILY' | 'WEEKLY' | 'ONCE', id: 0, position: 0 }));
if (subGoalsData && subGoalsData.data && subGoalsData.data.subGoals) {
subGoalsData.data.subGoals.forEach((goal, idx) => {
todos[idx] = { ...goal, cycle: goal.cycle || 'DAILY', id: goal.id ?? 0, position: idx + 1 };
});
}
const selectedGoal = coreGoalsGrid[selectedGridIndex];
const selectedGoalTitle = selectedGoal?.title || '';
const mandalartSubGoals = Array(9)
.fill(null)
.map(() => ({ title: '', cycle: 'DAILY' as 'DAILY' | 'WEEKLY' | 'ONCE', id: 0, position: 0 }));
mandalartSubGoals[4] = { title: selectedGoalTitle, cycle: 'DAILY', id: 0, position: 0 };
const uniqueTodos = todos.filter((todo) => todo.title !== selectedGoalTitle);
let todoIdx = 0;
mandalartOrder.forEach((gridIdx) => {
if (gridIdx !== 4 && todoIdx < uniqueTodos.length) {
mandalartSubGoals[gridIdx] = uniqueTodos[todoIdx];
todoIdx++;
}
});
const updateTooltipState = (index: number, value: boolean) => {
setTooltipOpenArr((arr) => arr.map((v, i) => (i === index ? value : v)));
};
const isTooltipOpen = selectedGoalIndex !== -1 ? tooltipOpenArr[selectedGoalIndex] : false;
const handleTooltipClose = () => {
updateTooltipState(selectedGoalIndex, false);
};
const hasAnyTodos = allTodos.some((goalTodos) =>
goalTodos.some((todo) => todo.title.trim() !== ''),
);
const isCurrentGoalAiUsed = selectedGoalIndex === -1 ? false : aiUsedByGoal[selectedGoalIndex];
// 렌더링 직전 값 점검
console.log('selectedGoalIndex:', selectedGoalIndex);
console.log('subGoals:', subGoals);
console.log('subGoals[selectedGoalIndex]:', subGoals[selectedGoalIndex]);
console.log('coreGoalsData:', coreGoalsData);
console.log('isValidSubGoal:', isValidSubGoal(subGoals[selectedGoalIndex]));
const isAllCurrentTodosFilled = todos.every((todo) => todo.title.trim() !== '');
const shouldShowTooltip = isTooltipOpen && !isAllCurrentTodosFilled;
const handleSubGoalClick = (position: number) => {
if (position < 1 || position > 8) {
return;
}
const newIndex = position - 1;
setSelectedGoalIndex(newIndex);
// 클릭 시점에 값 점검
console.log('클릭한 position:', position);
console.log('선택될 selectedGoalIndex:', newIndex);
console.log('subGoals[newIndex]:', subGoals[newIndex]);
};
const handleApplyAiRecommendedGoals = async (selected: { title: string }[]) => {
const goals = selected.map((item) => ({
title: item.title,
cycle: 'DAILY',
}));
try {
if (!selectedCoreGoalId) {
openModal(
<AiFailModal onClose={closeModal} message="coreGoalSnapshotId가 올바르지 않습니다." />,
);
return;
}
await postAiRecommendSubGoals(selectedCoreGoalId, goals);
window.location.reload();
} catch (error: any) {
openModal(
<AiFailModal
onClose={closeModal}
message={error?.response?.data?.message || 'AI 추천값 저장에 실패했습니다.'}
/>,
);
}
};
const handleAiRecommendSuccess = (aiList: { title: string; cycle: string }[]) => {
openModal(
<AiRecommendModal
onClose={closeModal}
onSubmit={handleApplyAiRecommendedGoals}
values={todos.map((todo) => todo.title)}
options={aiList.map((item) => item.title)}
/>,
);
};
const handleAiRecommendFail = (err: any) => {
const message = err?.response?.data?.message || '다시 한 번 시도해주세요.';
openModal(<AiFailModal onClose={closeModal} message={message} />);
};
const handleOpenAiModal = () => {
if (!selectedCoreGoalId) {
handleAiRecommendFail({ response: { data: { message: 'coreGoalId가 올바르지 않습니다.' } } });
return;
}
recommendAiSubGoal(
{
coreGoal: subGoals[selectedGoalIndex],
subGoal: todos.filter((todo) => todo.title.trim()).map((todo) => ({ title: todo.title })),
},
{
onSuccess: (res) => {
const aiList = (res as any)?.data?.aiRecommendedList ?? (res as any)?.aiRecommendedList;
handleAiRecommendSuccess(aiList);
},
onError: handleAiRecommendFail,
},
);
};
const handleNavigateComplete = async () => {
try {
await completeMandalart(mandalartId);
navigate(PATH.TODO_MY);
} catch {
alert('만다라트 완성 처리 중 오류가 발생했습니다.');
}
};
return (
<main className={styles.lowerTodoContainer}>
<GradientBackground />
<section className={styles.lowerTodoBoxWrapper}>
<header className={styles.lowerTodoHeader}>
<div className={styles.lowerTodoHeaderLeft}>
<h1 className={styles.lowerTodoHeaderTitle}>
{userName}님,
<br />
<span className={styles.lowerTodoHeaderGoal}>
'
{isValidSubGoal(subGoals[selectedGoalIndex])
? subGoals[selectedGoalIndex]
: overallGoalData?.title || '전체 목표 없음'}
'
</span>
에<br />
도움이 될 8가지 할 일을 작성해보세요.
</h1>
</div>
<div className={styles.aiAssistWrapper}>
{shouldShowTooltip && (
<Tooltip
className={styles.aiAssistTooltip}
isOpen={isTooltipOpen}
onClose={handleTooltipClose}
/>
)}
{!isAllCurrentTodosFilled && (
<button
className={
isCurrentGoalAiUsed
? styles.aiAssistButton.inactive
: styles.aiAssistButton.active
}
type="button"
aria-label="AI로 빈칸 채우기"
onClick={handleOpenAiModal}
disabled={isCurrentGoalAiUsed || !selectedCoreGoalId}
>
AI로 빈칸 채우기
</button>
)}
</div>
</header>
<div className={styles.lowerTodoBox}>
<div className={styles.mainGoalSection}>
<Mandalart
type="TODO_MAIN"
data={{
id: 0,
position: 0,
title: truncateText(overallGoalData?.title || '전체 목표 없음', 23),
subGoals: mandalartOrder.map((gridIdx) => {
const goal = coreGoalsGrid[gridIdx];
return {
id: goal.id,
title: truncateText(goal.title, 23),
position: goal.position,
};
}),
}}
onGoalClick={handleSubGoalClick}
/>
</div>
<div className={styles.subGoalAndTodoSection}>
<div className={styles.subGoalSection}>
<Mandalart
type="TODO_SUB"
data={{
id: selectedGoalIndex,
position: selectedGoalIndex,
title: truncateText(selectedGoalTitle, 23),
subGoals: mandalartOrder.map((gridIdx) => {
const goal = mandalartSubGoals[gridIdx];
return {
id: goal.id,
title: truncateText(goal.title, 23),
position: goal.position,
cycle: goal.cycle,
};
}),
}}
onGoalClick={() => {}}
/>
<TodoFields
values={allTodos[selectedGoalIndex]}
onChange={handleTodoChange}
onSave={handleSaveSubGoalSync}
lastSavedTodos={[]}
selectedGoalIndex={selectedGoalIndex}
/>
</div>
<div className={styles.scrollerSection} />
</div>
</div>
<button
className={styles.mandalCompleteBox}
type="button"
aria-label="만다라트 완성하기"
onClick={handleNavigateComplete}
disabled={!hasAnyTodos}
>
<span
className={
hasAnyTodos ? styles.mandalCompleteText.active : styles.mandalCompleteText.inactive
}
>
만다라트를 완성했어요
</span>
<IcSmallNext
className={
hasAnyTodos ? styles.mandalCompleteIcon.active : styles.mandalCompleteIcon.inactive
}
/>
</button>
</section>
</main>
);
};
export default LowerTodo;