-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathedit-profile.tsx
More file actions
306 lines (279 loc) · 10.4 KB
/
edit-profile.tsx
File metadata and controls
306 lines (279 loc) · 10.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
import { userMutations } from '@apis/user/user-mutations';
import { userQueries } from '@apis/user/user-queries';
import Button from '@components/button/button/button';
import Divider from '@components/divider/divider';
import Icon from '@components/icon/icon';
import Input from '@components/input/input';
import { zodResolver } from '@hookform/resolvers/zod';
import { cn } from '@libs/cn';
import SelectionGroup from '@pages/edit-profile/components/selection-group';
import {
PROFILE_SYNC_MATE,
PROFILE_VIEWING_STYLE,
} from '@pages/edit-profile/constants/edit-profile';
import {
EditProfileSchema,
type EditProfileValues,
} from '@pages/edit-profile/schema/EditProfileSchema';
import { NO_TEAM_OPTION, TEAMS } from '@pages/onboarding/constants/onboarding';
import {
INTRODUCTION_RULE_MESSAGE,
NICKNAME_DUPLICATED,
NICKNAME_RULE_MESSAGE,
NICKNAME_SUCCESS_MESSAGE,
} from '@pages/sign-up/constants/NOTICE';
import {
INTRODUCTION_PLACEHOLDER,
NICKNAME_PLACEHOLDER,
} from '@pages/sign-up/constants/validation';
import type { NicknameStatus } from '@pages/sign-up/types/nickname-types';
import { useMutation, useQuery } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { Controller, useForm } from 'react-hook-form';
const EditProfile = () => {
const { data } = useQuery(userQueries.MATCH_CONDITION());
const [team, setTeam] = useState<string | undefined>(undefined);
const [mateTeam, setMateTeam] = useState<string | undefined>(undefined);
const [viewStyle, setViewStyle] = useState<string | undefined>(undefined);
const [avgSeason, setAvgSeason] = useState('');
const [isSubmit, setIsSubmit] = useState(false);
const [nicknameStatus, setNicknameStatus] = useState<NicknameStatus>('idle');
const { mutate: editProfile } = useMutation(userMutations.EDIT_PROFILE());
const { mutate: editMatchCondition } = useMutation(userMutations.EDIT_MATCH_CONDITION());
const {
control,
formState: { errors, isSubmitting },
trigger,
getValues,
watch,
} = useForm<EditProfileValues>({
resolver: zodResolver(EditProfileSchema),
mode: 'onChange',
defaultValues: { nickname: '', introduction: '' },
});
const nicknameVal = watch('nickname', '');
const introductionVal = watch('introduction', '');
const { mutateAsync: checkNickname } = useMutation(userMutations.NICKNAME_CHECK());
const submitNickname = async () => {
const ok = await trigger('nickname');
if (!ok) return;
editProfile({ field: '닉네임', value: getValues('nickname').trim() });
};
const submitInformation = async () => {
const ok = await trigger('introduction');
if (!ok) return;
editProfile({ field: '소개', value: getValues('introduction').trim() });
};
const initial = {
team: data?.team ?? '',
mateTeam: data?.teamAllowed ?? '',
viewStyle: data?.style ?? '',
avgSeason: data?.avgSeason ?? 0,
};
const teamValue = team ?? initial.team;
const viewStyleValue = viewStyle ?? initial.viewStyle;
const mateTeamValue = (teamValue === NO_TEAM_OPTION ? '' : (mateTeam ?? initial.mateTeam)) ?? '';
const avgSeasonValue = avgSeason === '' ? initial.avgSeason : Number(avgSeason);
const isMatchDirty =
teamValue !== initial.team ||
mateTeamValue !== initial.mateTeam ||
viewStyleValue !== initial.viewStyle ||
avgSeasonValue !== initial.avgSeason;
const isSubmitDisabled = !isMatchDirty || isSubmit;
const handleSaveClick = () => {
if (!isMatchDirty) return;
setIsSubmit(true);
editMatchCondition({
team: teamValue,
style: viewStyleValue,
teamAllowed: teamValue === NO_TEAM_OPTION ? null : mateTeamValue || null,
avgSeason: avgSeasonValue,
});
};
// biome-ignore lint/correctness/useExhaustiveDependencies: reset nickname status whenever value changes
useEffect(() => {
setNicknameStatus('idle');
}, [nicknameVal]);
const handleCheckNickname = async () => {
if (errors.nickname || nicknameVal.trim().length < 2) return;
setNicknameStatus('checking');
try {
const available = await checkNickname({ nickname: nicknameVal });
setNicknameStatus(available ? 'available' : 'duplicate');
} catch {
setNicknameStatus('idle');
}
};
return (
<div className="h-full bg-gray-white px-[1.6rem] pt-[1.6rem] pb-[4rem]">
<h2 className="subhead_18_sb mb-[1.6rem]">프로필 수정</h2>
{/* 닉네임 */}
<section>
<div className="mb-[2.4rem] flex-col gap-[0.8rem]">
<p className="body_16_m text-gray-black">프로필 이미지</p>
{/* TODO: 프로필 편집 api 연결 */}
<div className="relative w-fit">
<Icon name="profile" size={6.4} />
<Icon name="camera" size={1.6} className="absolute right-0 bottom-0" />
</div>
</div>
<Controller
name="nickname"
control={control}
render={({ field }) => (
<Input
{...field}
placeholder={NICKNAME_PLACEHOLDER}
label="닉네임"
defaultMessage={NICKNAME_RULE_MESSAGE}
validationMessage={
nicknameStatus === 'duplicate'
? NICKNAME_DUPLICATED
: nicknameStatus === 'available'
? NICKNAME_SUCCESS_MESSAGE
: undefined
}
isError={nicknameStatus === 'duplicate'}
isValid={nicknameStatus === 'available'}
/>
)}
/>
<div className="mb-[2.4rem] flex justify-end gap-[0.8rem]">
<Button
type="button"
variant={
!!errors.nickname || nicknameVal.trim().length === 0 || isSubmitting
? 'disabled'
: 'skyblue'
}
label="중복 확인"
onClick={handleCheckNickname}
className={cn(
'cap_14_sb mt-[0.8rem] w-auto px-[1.6rem] py-[0.6rem]',
(!!errors.nickname || nicknameVal.trim().length === 0 || isSubmitting) &&
'cursor-not-allowed',
)}
/>
<Button
type="button"
variant={
nicknameStatus !== 'available' || nicknameVal.trim().length === 0 || isSubmitting
? 'disabled'
: 'blue'
}
label="수정"
onClick={submitNickname}
className={cn(
'cap_14_sb mt-[0.8rem] w-auto px-[1.6rem] py-[0.6rem]',
(nicknameStatus !== 'available' || nicknameVal.trim().length === 0 || isSubmitting) &&
'cursor-not-allowed',
)}
/>
</div>
<Controller
name="introduction"
control={control}
render={({ field, fieldState }) => (
<Input
{...field}
placeholder={INTRODUCTION_PLACEHOLDER}
defaultMessage={INTRODUCTION_RULE_MESSAGE}
isError={!!fieldState.error}
isValid={!fieldState.error && field.value.trim().length > 0}
length={introductionVal.length}
hasLength
className="h-[10.4rem]"
label="한 줄 소개"
multiline
/>
)}
/>
<div className="flex justify-end">
<Button
type="button"
variant={
!!errors.introduction || introductionVal.trim().length === 0 || isSubmitting
? 'disabled'
: 'blue'
}
label="수정"
onClick={submitInformation}
className={cn(
'cap_14_sb mt-[0.8rem] w-auto px-[1.6rem] py-[0.6rem]',
(!!errors.introduction || introductionVal.trim().length === 0 || isSubmitting) &&
'cursor-not-allowed',
)}
/>
</div>
</section>
<div className="-mx-[1.6rem] my-[3.2rem]">
<Divider thickness={0.4} />
</div>
{/* 매칭 조건 */}
<section className="flex-col pb-[5.6rem]">
<h2 className="subhead_18_sb mb-[0.4rem]">매칭 조건 수정</h2>
<p className="cap_12_m mb-[1.6rem] text-gray-500">
수정한 조건을 기반으로 새로운 메이트를 추천해드려요!
</p>
<div className="flex-col gap-[3.2rem]">
<div className="flex-col gap-[1.6rem]">
<p className="body_16_m">응원팀</p>
<div className="flex flex-wrap gap-[0.8rem]">
{TEAMS.map((option) => (
<Button
key={option}
label={option}
variant={teamValue === option ? 'skyblue' : 'gray2'}
className="cap_14_sb w-auto px-[1.6rem] py-[0.6rem]"
onClick={() => setTeam(option)}
/>
))}
<Button
label={NO_TEAM_OPTION}
variant={teamValue === NO_TEAM_OPTION ? 'skyblue' : 'gray2'}
className="cap_14_sb w-fit px-[1.6rem] py-[0.6rem]"
onClick={() => {
setTeam(NO_TEAM_OPTION);
setMateTeam('상관없어요');
}}
/>
</div>
</div>
<SelectionGroup
title="직관 메이트의 응원팀"
options={PROFILE_SYNC_MATE}
selectedValue={mateTeamValue}
onSelect={setMateTeam}
disabled={teamValue === NO_TEAM_OPTION}
/>
<SelectionGroup
title="관람 스타일"
options={PROFILE_VIEWING_STYLE}
selectedValue={viewStyleValue}
onSelect={setViewStyle}
/>
<div className="flex-col gap-[1.6rem]">
<p className="body_16_m text-gray-black">평균 직관 수 </p>
<Input
value={avgSeason}
placeholder={initial.avgSeason.toString()}
inputMode="numeric"
onChange={(e) => {
const numericValue = e.target.value.replace(/\D/g, '').slice(0, 3);
setAvgSeason(numericValue);
}}
/>
</div>
</div>
</section>
<Button
variant={isSubmitDisabled ? 'disabled' : 'blue'}
className={cn(isSubmitDisabled && 'cursor-not-allowed')}
onClick={handleSaveClick}
label="매칭 조건 수정"
ariaLabel="매칭 조건 수정"
/>
</div>
);
};
export default EditProfile;