Skip to content

Commit 263e893

Browse files
authored
Merge pull request #267 from YAPP-Github/fix/YS-569
[YS-569] 교내 실험 여부에 따른 조건부 유효성 검증 에러 해결
2 parents ba4306c + c8c3eef commit 263e893

3 files changed

Lines changed: 45 additions & 26 deletions

File tree

src/app/upload/components/OutlineSection/OutlineSection.tsx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const OutlineSection = ({
5252
useUserResearcherInfo();
5353

5454
// 진행 방식 선택 로직
55-
const { selectedMatchType, handleMatchTypeChange } = useMatchType();
55+
const { selectedMatchType, handleMatchTypeChange } = useMatchType({ setIsOnCampus });
5656

5757
// 지역 선택 로직
5858
const {
@@ -212,25 +212,27 @@ const OutlineSection = ({
212212
<label className={label} htmlFor="place">
213213
실험 장소
214214
</label>
215-
<div>
216-
<Controller
217-
name="isOnCampus"
218-
control={control}
219-
render={({ field }) => (
220-
<CheckboxWithIcon
221-
checked={field.value}
222-
onChange={() => {
223-
const newValue = !field.value;
224-
field.onChange(newValue);
225-
setIsOnCampus?.(newValue);
226-
}}
227-
label="교내 실험"
228-
align="left"
229-
size="large"
230-
/>
231-
)}
232-
/>
233-
</div>
215+
{selectedMatchType !== MATCH_TYPE.ONLINE && (
216+
<div>
217+
<Controller
218+
name="isOnCampus"
219+
control={control}
220+
render={({ field }) => (
221+
<CheckboxWithIcon
222+
checked={field.value}
223+
onChange={() => {
224+
const newValue = !field.value;
225+
field.onChange(newValue);
226+
setIsOnCampus?.(newValue);
227+
}}
228+
label="교내 실험"
229+
align="left"
230+
size="large"
231+
/>
232+
)}
233+
/>
234+
</div>
235+
)}
234236
</div>
235237

236238
{selectedMatchType === MATCH_TYPE.ONLINE ? (

src/app/upload/hooks/useMatchType.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { usePathname } from 'next/navigation';
2+
import { Dispatch, SetStateAction } from 'react';
23
import { useFormContext, useWatch } from 'react-hook-form';
34

45
import useUserInfo from '@/app/home/hooks/useUserInfo';
56
import { MATCH_TYPE, MatchType } from '@/app/post/[postId]/ExperimentPostPage.types';
67
import { isResearcherInfo } from '@/utils/typeGuard';
78

8-
const useMatchType = () => {
9+
interface UseMatchTypeProps {
10+
setIsOnCampus?: Dispatch<SetStateAction<boolean>>;
11+
}
12+
13+
const useMatchType = ({ setIsOnCampus }: UseMatchTypeProps) => {
914
const pathname = usePathname();
1015
const isEdit = pathname.startsWith('/edit');
1116
const { control, setValue } = useFormContext();
@@ -21,7 +26,9 @@ const useMatchType = () => {
2126
setValue('region', null);
2227
setValue('area', null);
2328
setValue('place', null);
24-
setValue('detailedAddress', '');
29+
setValue('detailedAddress', null);
30+
setValue('isOnCampus', false);
31+
setIsOnCampus?.(false);
2532
} else {
2633
setValue('region', '');
2734
setValue('area', '');

src/schema/upload/uploadExperimentPostSchema.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,20 @@ const UploadExperimentPostSchema = ({
5656
region: z.string().min(1, '').nullable(),
5757
// 지역구
5858
area: z.string().min(1, '').nullable(),
59-
// 상세 주소
60-
detailedAddress: isOnCampus // 교내실험이면 옵셔널, 교내실험이 아니라면 필수값
61-
? z.string().max(70, { message: '최대 70자 이하로 입력해 주세요' }).optional()
62-
: z.string().min(1, '').max(70, { message: '최대 70자 이하로 입력해 주세요' }),
59+
// 상세 주소. 비대면 또는 교내실험이면 옵셔널, 교내실험이 아니라면 필수값.
60+
detailedAddress: z
61+
.string()
62+
.nullable()
63+
.refine(
64+
(val) => {
65+
if (val === null) return true; // 비대면
66+
if (val.length > 70) return false; // 최대 70자 이하
67+
if (!isOnCampus && val.trim().length === 0) return false; // 교내실험이 아니면 필수값
68+
69+
return true;
70+
},
71+
{ message: '최대 70자 이하로 입력해 주세요' },
72+
),
6373

6474
// 보상
6575
reward: z

0 commit comments

Comments
 (0)