Skip to content

Commit 989ecf9

Browse files
committed
feat: 그룹 스케줄 수정 폼 유효성 검증 로직 추가
1 parent 2ca3741 commit 989ecf9

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

src/components/Group/GroupFormDateInput.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1-
import { FC } from 'react';
1+
import { FC, useState } from 'react';
22
import { dateToYYMMDD } from '@/utils/dateUtils';
3+
import { getGroupSchedulePeriodErrorDefineObject } from '@/utils/groupScheduleUtils';
34

45
interface Props {
56
startDate: string;
67
endDate: string;
78
}
89

910
const GroupFormDateInput: FC<Props> = ({ startDate, endDate }) => {
11+
const [curStartDate, setCurStartDate] = useState<string>(dateToYYMMDD(startDate));
12+
const [curEndDate, setCurEndDate] = useState<string>(dateToYYMMDD(endDate));
13+
const { errorText, isError } = getGroupSchedulePeriodErrorDefineObject({
14+
startDate: curStartDate,
15+
endDate: curEndDate,
16+
});
17+
18+
const onStartDateChange = (e: React.ChangeEvent<HTMLInputElement>) => {
19+
setCurStartDate(e.target.value);
20+
};
21+
22+
const onEndDateChange = (e: React.ChangeEvent<HTMLInputElement>) => {
23+
setCurEndDate(e.target.value);
24+
};
25+
1026
return (
1127
<div className="w-full">
1228
<label className="label label-text w-full text-start">모임 설정 기간 *</label>
@@ -17,7 +33,8 @@ const GroupFormDateInput: FC<Props> = ({ startDate, endDate }) => {
1733
<input
1834
className="input border-none"
1935
type="date"
20-
defaultValue={dateToYYMMDD(new Date(startDate))}
36+
onChange={onStartDateChange}
37+
value={curStartDate}
2138
id="group-start-date-input"
2239
name="startDate"
2340
/>
@@ -28,11 +45,15 @@ const GroupFormDateInput: FC<Props> = ({ startDate, endDate }) => {
2845
<input
2946
className="input border-none"
3047
type="date"
48+
onChange={onEndDateChange}
3149
id="group-end-date-input"
32-
defaultValue={dateToYYMMDD(new Date(endDate))}
50+
value={curEndDate}
3351
name="endDate"
3452
/>
3553
</div>
54+
<div className="label flex h-8 flex-row items-center">
55+
<span className={`label-text-alt ${isError ? 'text-error' : ''}`}>{isError ? errorText : ''}</span>
56+
</div>
3657
</div>
3758
);
3859
};

src/components/Group/GroupFormNameInput.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
import { FC } from 'react';
1+
import { FC, useState } from 'react';
22
import InputForm from '../common/InputForm';
3+
import { isValidGroupScheduleName } from '@/utils/groupScheduleUtils';
34

45
interface Props {
56
name: string;
67
}
78

89
const GroupFormNameInput: FC<Props> = ({ name = '' }) => {
10+
const [isError, setIsError] = useState(false);
11+
12+
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
13+
setIsError(!isValidGroupScheduleName(e.target.value));
14+
};
15+
916
return (
1017
<InputForm
1118
id="group-name-input"
1219
type="text"
1320
defaultValue={name}
1421
placeholder="모임명을 입력하세요"
15-
onChange={() => {}}
22+
onChange={onChange}
1623
title="모임명 *"
1724
hint=""
25+
error={isError}
26+
errorText="모임명을 입력해주세요."
1827
name="name"
1928
/>
2029
);

src/utils/dateUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export const dateToYYMMDD = (date: Date) => {
1+
export const dateToYYMMDD = (value: string) => {
2+
const date = new Date(value);
23
const year = date.getFullYear();
34
const month = date.getMonth() + 1;
45
const day = date.getDate();

src/utils/groupScheduleUtils.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export const isValidGroupScheduleName = (name: string) => {
2+
return name.length > 0;
3+
};
4+
5+
interface IsValidGroupSchedulePeriodParams {
6+
startDate: string;
7+
endDate: string;
8+
}
9+
10+
export const getGroupSchedulePeriodErrorDefineObject = ({ startDate, endDate }: IsValidGroupSchedulePeriodParams) => {
11+
if (!bothDatesHasValue({ startDate, endDate })) {
12+
return {
13+
isError: true,
14+
errorText: '모임 시작일과 종료일을 입력해주세요.',
15+
};
16+
}
17+
18+
if (!startDateIsEarlierThanEndDate({ startDate, endDate })) {
19+
return {
20+
isError: true,
21+
errorText: '모임 시작일이 종료일보다 빠를 수 없습니다.',
22+
};
23+
}
24+
25+
return {
26+
isError: false,
27+
errorText: '',
28+
};
29+
};
30+
31+
const bothDatesHasValue = ({ startDate, endDate }: IsValidGroupSchedulePeriodParams) => {
32+
return startDate.length > 0 && endDate.length > 0;
33+
};
34+
35+
const startDateIsEarlierThanEndDate = ({ startDate, endDate }: IsValidGroupSchedulePeriodParams) => {
36+
return new Date(startDate) < new Date(endDate);
37+
};

0 commit comments

Comments
 (0)