forked from prography/10th-Motimo-FE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnboardingSubGoalEdit.tsx
More file actions
159 lines (148 loc) · 6.42 KB
/
OnboardingSubGoalEdit.tsx
File metadata and controls
159 lines (148 loc) · 6.42 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
"use client";
import { motion, Reorder } from "motion/react";
import { useState, useEffect, useRef } from "react";
import useModal from "@/hooks/useModal";
import SubGoalEditItem from "@/components/details/SubGoalEditItem/SubGoalEditItem";
import ModalAddingSubGoal from "@/components/shared/Modal/ModalAddingSubGoal/ModalAddingSubGoal";
import ModalDeletingSubGoal from "@/components/shared/Modal/ModalDeletingSubGoal/ModalDeletingSubGoal";
import ModalUpdatingSubGoal from "@/components/details/Modals/ModalUpdatingSubGoal/ModalUpdatingSubGoal";
import PlusSvg from "@/components/shared/public/Add_Plus.svg";
import useToast from "@/hooks/useToast";
import useOnboardingStore from "@/stores/useOnboardingStore";
const OnboardingSubGoalEdit = () => {
const { openModal, closeModal } = useModal();
const { setToast } = useToast();
const { subGoals, setSubGoals, addSubGoal } = useOnboardingStore();
const hasInitialized = useRef(false);
// Add default "기본함" subGoal when component mounts if it doesn't exist
useEffect(() => {
if (hasInitialized.current) return;
const hasDefaultSubGoal = subGoals.some(subGoal => subGoal.title === "기본함");
if (!hasDefaultSubGoal) {
addSubGoal("기본함");
hasInitialized.current = true;
} else {
// Remove duplicate "기본함" subGoals, keep only the first one
const defaultSubGoals = subGoals.filter(subGoal => subGoal.title === "기본함");
if (defaultSubGoals.length > 1) {
const filteredSubGoals = subGoals.filter(subGoal => subGoal.title !== "기본함");
filteredSubGoals.unshift(defaultSubGoals[0]); // Keep the first "기본함" at the beginning
setSubGoals(filteredSubGoals);
}
hasInitialized.current = true;
}
}, [subGoals, addSubGoal, setSubGoals]);
return (
<>
<main className="flex flex-col gap-2">
<>
<div
data-type="type5"
className="w-full p-3 bg-white rounded-lg shadow-[0px_0px_4px_0px_rgba(0,0,0,0.10)] inline-flex flex-col justify-start items-center gap-2 overflow-hidden"
>
<div className="self-stretch h-8 inline-flex justify-start items-center gap-1">
<div className="flex-1 justify-center text-label-strong text-base font-bold font-['SUIT_Variable'] leading-snug">
세부 목표를 추가해주세요.
</div>
<button
onClick={() => {
openModal(
<ModalAddingSubGoal
onClose={() => closeModal()}
onAddSubGoal={async (subGoal: string) => {
addSubGoal(subGoal);
closeModal();
}}
/>,
);
}}
type="button"
className="w-8 h-8 p-1.5 bg-background-primary rounded-[999px] flex justify-center items-center gap-2"
>
<div className="w-5 h-5 relative overflow-hidden text-white">
<PlusSvg width={20} height={20} />
</div>
</button>
</div>
</div>
</>
<Reorder.Group
onReorder={(newOrderedSubGoals) => {
const orderUpdated = newOrderedSubGoals as typeof subGoals;
setSubGoals(
orderUpdated.map((subGoalInfo, idx) => ({
...subGoalInfo,
order: idx + 1,
}))
);
}}
values={subGoals}
>
<section className="flex flex-col gap-2">
{subGoals.map((currentSubGoalInfo, idx) => (
<Reorder.Item
value={currentSubGoalInfo}
key={
currentSubGoalInfo.id
? `${currentSubGoalInfo.id}-${currentSubGoalInfo.title}`
: currentSubGoalInfo.tmpKey || currentSubGoalInfo.order
}
>
<SubGoalEditItem
subGoalTitle={currentSubGoalInfo.title}
onEdit={
currentSubGoalInfo.title === "기본함"
? undefined
: () => {
openModal(
<ModalUpdatingSubGoal
initSubGoal={currentSubGoalInfo.title}
onClose={closeModal}
onUpdateSubGoal={async (newSubGoalTitle) => {
const newSubGoals = subGoals.map((prevSubGoal) => {
if (
prevSubGoal.tmpKey === currentSubGoalInfo.tmpKey ||
prevSubGoal.id === currentSubGoalInfo.id
) {
return { ...prevSubGoal, title: newSubGoalTitle };
}
return prevSubGoal;
});
setSubGoals(newSubGoals);
setToast("세부 목표 텍스트 변경이 완료되었습니다.");
}}
/>,
);
}
}
onDelete={
currentSubGoalInfo.title === "기본함"
? undefined
: () => {
openModal(
<ModalDeletingSubGoal
onClose={closeModal}
onDeletSubGoale={async () => {
const newSubGoals = subGoals.filter(
(prevSubGoal) =>
prevSubGoal.tmpKey !== currentSubGoalInfo.tmpKey &&
prevSubGoal.id !== currentSubGoalInfo.id
);
setSubGoals(newSubGoals);
closeModal();
setToast("세부 목표 삭제가 완료되었습니다.");
}}
/>,
);
}
}
/>
</Reorder.Item>
))}
</section>
</Reorder.Group>
</main>
</>
);
};
export default OnboardingSubGoalEdit;