-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTodoModal.tsx
More file actions
225 lines (198 loc) · 5.87 KB
/
TodoModal.tsx
File metadata and controls
225 lines (198 loc) · 5.87 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
import { useState } from 'react';
import axiosInstance from '@/src/apis/axiosInstance';
import TextButton from '@/src/components/ui/Button/TextButton';
import ModalDropdown from '@/src/components/ui/ModalInput/ModalDropdown';
import ModalImage from '@/src/components/ui/ModalInput/ModalImage';
import ModalInput from '@/src/components/ui/ModalInput/ModalInput';
import ModalTextarea from '@/src/components/ui/ModalInput/ModalTextarea';
import { useGetColumnList } from '@/src/hooks/dashboard/useGetColumnList';
import { useGetMembers } from '@/src/hooks/table/useGetMembers';
interface TodoModalProps {
onClose: () => void;
mode: string;
postData: PostData;
getData?: GetData;
onCreated?: (message: string) => void;
}
interface PostData {
assigneeUserId: number;
dashboardId: number;
columnId: number;
title: string;
description: string;
dueDate: string;
tags: string[];
imageUrl: string | File;
}
interface GetData {
assigneeUserId: number;
dashboardId: number;
columnId: number;
title: string;
description: string;
dueDate: string;
tags: string[];
imageUrl: string | File;
}
const TodoModal: React.FC<TodoModalProps> = ({
onClose,
mode,
postData,
getData,
onCreated,
}) => {
const [formData, setFormData] = useState<PostData>({
assigneeUserId: postData.assigneeUserId,
dashboardId: postData.dashboardId,
columnId: postData.columnId,
title: postData.title || '',
description: postData.description || '',
dueDate: postData.dueDate || '',
tags: postData.tags || [],
imageUrl: postData.imageUrl || '',
});
const memberOptions = useGetMembers(1, 99).membersInfo?.members || [];
const columnsOptions = useGetColumnList().data || [];
const handleChangeTitle = (value: string[]) => {
setFormData((prevFormData) => ({ ...prevFormData, title: value[0] }));
};
const handleChangeDescription = (value: string) => {
setFormData((prevFormData) => ({ ...prevFormData, description: value }));
};
const handleChangeDueDate = (value: string[]) => {
setFormData((prevFormData) => ({
...prevFormData,
dueDate: value[0].replaceAll('.', '-'),
}));
};
const handleChangeTags = (value: string[]) => {
setFormData((prevFormData) => ({ ...prevFormData, tags: value }));
};
const handleChangeImageUrl = (value: File) => {
setFormData((prevFormData) => ({ ...prevFormData, imageUrl: value }));
};
const handleChangeColumn = (value: number) => {
setFormData((prevFormData) => ({ ...prevFormData, columnId: value }));
};
const handleChangeAssignee = (value: number) => {
setFormData((prevFormData) => ({ ...prevFormData, assigneeUserId: value }));
};
const uploadImageAndGetUrl = async (imageFile: string | File) => {
const imageFormData = new FormData();
imageFormData.append('image', imageFile);
const response = await axiosInstance.post<{ imageUrl: File }>(
`columns/${postData.columnId}/card-image`,
imageFormData,
{
headers: {
'Content-Type': 'multipart/form-data',
},
},
);
return response.data.imageUrl;
};
const handleSubmit = () => {
void (async () => {
try {
let sendFormData;
if (formData.imageUrl) {
const imageUrl = await uploadImageAndGetUrl(formData.imageUrl);
sendFormData = { ...formData, imageUrl: imageUrl };
} else {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
sendFormData = (({ imageUrl, ...rest }) => rest)(formData);
}
await axiosInstance.post('cards', sendFormData);
onClose();
onCreated('할 일을 생성했습니다.');
} catch (error) {
console.error('Error occurred:', error); // handle error
}
})();
};
return (
<div className='flex w-[506px] flex-col gap-8 rounded-lg bg-white p-7'>
<div className='text-xl font-medium'>
{mode === '수정' ? '할 일 수정' : '할 일 생성'}
</div>
<div className='flex flex-col gap-8'>
{mode === '생성' && (
<>
<ModalDropdown
label='담당자'
data={memberOptions}
onDropdownSelect={handleChangeAssignee}
/>
<ModalInput
label='제목'
required={true}
onValueChange={handleChangeTitle}
/>
<ModalTextarea
label='설명'
required={true}
isButton={false}
onTextChange={handleChangeDescription}
/>
<ModalInput label='마감일' onValueChange={handleChangeDueDate} />
<ModalInput label='태그' onValueChange={handleChangeTags} />
<ModalImage label='이미지' onImageSelect={handleChangeImageUrl} />
</>
)}
{/* mode가 '수정'일 때도 보이도록 */}
{mode === '수정' && (
<>
<div className='flex flex-row items-center gap-2.5'>
<ModalDropdown
label='상태'
data={columnsOptions}
currentId={getData?.columnId}
onDropdownSelect={handleChangeColumn}
/>
<ModalDropdown
label='담당자'
data={memberOptions}
currentId={getData?.assigneeUserId}
onDropdownSelect={handleChangeAssignee}
/>
</div>
<ModalInput
label='제목'
required={true}
onValueChange={handleChangeTitle}
/>
<ModalTextarea
label='설명'
required={true}
isButton={false}
onTextChange={handleChangeDescription}
/>
<ModalInput label='마감일' onValueChange={handleChangeDueDate} />
<ModalInput label='태그' onValueChange={handleChangeTags} />
<ModalImage label='이미지' onImageSelect={handleChangeImageUrl} />
</>
)}
</div>
<div className='mt-7 flex justify-end gap-3 sm:mt-6 sm:justify-center'>
<TextButton
buttonSize='md'
textSize='md'
color='secondary'
onClick={onClose}
>
취소
</TextButton>
<TextButton
buttonSize='md'
type='submit'
textSize='md'
color='primary'
onClick={handleSubmit}
>
{mode === '생성' ? '생성' : '수정'}
</TextButton>
</div>
</div>
);
};
export default TodoModal;