Skip to content

Commit 01fa318

Browse files
authored
Merge pull request #156 from imaginer-dev/30-그룹-일정-관련-유효성-검증
30 그룹 일정 관련 유효성 검증을 할 수 있어야 한다.
2 parents 7b24368 + 0cef349 commit 01fa318

File tree

7 files changed

+79
-12
lines changed

7 files changed

+79
-12
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/tests/EditGroupSchedulePage.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ describe('EditGroupPage', () => {
3333

3434
expect(groupNameInput.value).toBe(groupScheduleFixture[0].title);
3535
expect(groupDescriptionInput.value).toBe(groupScheduleFixture[0].description);
36-
expect(groupDateStartInput.value).toBe(dateToYYMMDD(new Date(groupScheduleFixture[0].start_date)));
37-
expect(groupDateEndInput.value).toBe(dateToYYMMDD(new Date(groupScheduleFixture[0].end_date)));
36+
expect(groupDateStartInput.value).toBe(dateToYYMMDD(groupScheduleFixture[0].start_date));
37+
expect(groupDateEndInput.value).toBe(dateToYYMMDD(groupScheduleFixture[0].end_date));
3838
expect(groupMemoInput.value).toBe(groupScheduleFixture[0].memo);
3939
});
4040
});

src/tests/GroupForm.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ describe('EditGroupForm', () => {
2727

2828
expect(groupNameInput.value).toBe('name');
2929
expect(groupDescriptionInput.value).toBe('description');
30-
expect(groupDateStartInput.value).toBe(dateToYYMMDD(new Date(startDate)));
31-
expect(groupDateEndInput.value).toBe(dateToYYMMDD(new Date(endDate)));
30+
expect(groupDateStartInput.value).toBe(dateToYYMMDD(startDate));
31+
expect(groupDateEndInput.value).toBe(dateToYYMMDD(endDate));
3232
expect(groupMemoInput.value).toBe('memo');
3333
});
3434
it('모임명, 설명, 메모를 수정할 수 있어야 한다.', async () => {

src/utils/__test__/dateUtils.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { dateToYYMMDD } from '../dateUtils';
33

44
describe('dateUtils', () => {
55
it('should return a date string', () => {
6-
const date = new Date('2021-09-01');
7-
const result = dateToYYMMDD(date);
6+
const result = dateToYYMMDD(new Date('2021-09-01').toString());
87
expect(result).toBe('2021-09-01');
98
});
109
});

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)