-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommunity-write.tsx
More file actions
176 lines (157 loc) · 5.67 KB
/
community-write.tsx
File metadata and controls
176 lines (157 loc) · 5.67 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
import { ChangeEvent, useState } from 'react';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import { Input, Navigation, TextButton, Title } from '@bds/ui';
import { Icon } from '@bds/ui/icons';
import CommunityImageUploader from '@widgets/community/components/community-image-uploader/community-image-uploader';
import CommunityLine from '@widgets/community/components/community-line/community-line';
import FilterDropDown from '@widgets/community/components/filter-dropdown/filter-dropdown';
import { categoryOptions } from '@widgets/community/configs/category-config';
import { PLACEHOLDER } from '@widgets/community/constant/input-placeholder';
import { CategoryType } from '@widgets/community/types/category-type';
import { COMMUNITY_MUTATION_OPTIONS } from '@shared/api/domain/community/queries';
import { COMMUNITY_QUERY_KEY } from '@shared/api/keys/query-key';
import {
LIMIT_LONG_TEXT,
LIMIT_SHORT_TEXT,
} from '@shared/constants/text-limits';
import { useImageUpload } from '@shared/hooks/use-image-upload';
import { useLimitedInput } from '@shared/hooks/use-limited-input';
import { routePath } from '@shared/router/path';
import * as styles from './community-write.css';
const CommunityWrite = () => {
const navigate = useNavigate();
const queryClient = useQueryClient();
const [title, setTitle] = useState('');
const [content, setContent] = useState('');
const [category, setCategory] = useState<CategoryType | null>(null);
const [uploadedImages, setUploadedImages] = useState<
{ file: File; previewUrl: string }[]
>([]);
const { isErrorState } = useLimitedInput(LIMIT_SHORT_TEXT, title.length);
const { mutate: postFeedMutate, isPending } = useMutation({
...COMMUNITY_MUTATION_OPTIONS.POST_FEED(),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: COMMUNITY_QUERY_KEY.FEED_PREVIEW(),
});
navigate(routePath.COMMUNITY);
},
});
const { uploadImageFiles } = useImageUpload();
const isDisabled =
!(title.trim() && content.trim() && category?.value) || isPending;
const handleGoBack = () => {
navigate(-1);
};
const handleTitleChange = (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.value.length <= LIMIT_SHORT_TEXT) {
setTitle(e.target.value);
}
};
const handleContentChange = (e: ChangeEvent<HTMLTextAreaElement>) => {
if (e.target.value.length <= LIMIT_LONG_TEXT) {
setContent(e.target.value);
}
};
const handleCategory = (option: CategoryType) => {
setCategory(option);
};
const handleImageChange = (files: FileList) => {
const newImages = Array.from(files).map((file) => ({
file,
previewUrl: URL.createObjectURL(file),
}));
setUploadedImages((prev) => [...prev, ...newImages]);
};
const handleRemoveImage = (urlToRemove: string) => {
setUploadedImages((prev) =>
prev.filter((item) => item.previewUrl !== urlToRemove),
);
};
const submitFeed = async (imageUrls: string[]) => {
postFeedMutate({
title,
content,
category: category!.value,
imageUrls,
});
};
const handlePostFeed = async () => {
if (isDisabled) {
return null;
}
const uploadedImageUrls = await uploadImageFiles(
uploadedImages.map((image) => image.file),
);
return submitFeed(uploadedImageUrls);
};
return (
<div className={styles.container}>
<Navigation
title="글쓰기"
leftIcon={<Icon name="caret_left_lg" width="2.4rem" height="2.4rem" />}
onClickLeft={handleGoBack}
rightIcon={
<TextButton color="primary" disabled={isDisabled} size="sm">
업로드
</TextButton>
}
onClickRight={handlePostFeed}
isTextButton
/>
<div className={styles.postContainer}>
<div className={styles.postHeader}>
<div className={styles.postTitle}>
<Title fontStyle="eb_md">제목</Title>
<FilterDropDown
optionTitle={category ? category.label : '카테고리 선택'}
rightIcon={<Icon name="caret_up_sm" />}
isIconRotate
>
{categoryOptions.map((option) => (
<TextButton
key={option.value}
size="sm"
color={category?.value === option.value ? 'primary' : 'black'}
onClick={() => handleCategory(option)}
>
{option.label}
</TextButton>
))}
</FilterDropDown>
</div>
<Input
value={title}
onChange={handleTitleChange}
bgColor="background"
errorState={isErrorState}
placeholder={PLACEHOLDER.TITLE}
/>
</div>
<div className={styles.postContent}>
<Title fontStyle="eb_md">내용</Title>
<CommunityLine value={content} onChange={handleContentChange} />
{uploadedImages.length > 0 && (
<div className={styles.imageContainer}>
{uploadedImages.map((image) => (
<div key={image.previewUrl} className={styles.imageItem}>
<img className={styles.postImage} src={image.previewUrl} />
<TextButton
color="black"
size="sm"
onClick={() => handleRemoveImage(image.previewUrl)}
>
삭제
</TextButton>
</div>
))}
</div>
)}
</div>
</div>
<CommunityImageUploader onChange={handleImageChange} />
</div>
);
};
export default CommunityWrite;