Skip to content

Commit e71f767

Browse files
authored
Merge pull request #192 from imaginer-dev/30-생성-수정-버튼-클릭시-유효성-검증을-실행해야-한다
30 유효성 검사 및 캘린더 이벤트 스타일 수정
2 parents ca725e1 + 714d2ab commit e71f767

File tree

7 files changed

+64
-11
lines changed

7 files changed

+64
-11
lines changed

src/components/common/Calendar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export default memo(function Calendar({
184184
function renderEventContent(eventInfo: EventInfo) {
185185
return (
186186
<>
187-
<div className="w-full border-0 bg-secondary p-0.5 text-white">
187+
<div className="line-clamp-1 w-full text-wrap border-0 bg-secondary p-0.5 text-white">
188188
<b className="border-0">{eventInfo.timeText}</b>
189189
<i> {eventInfo.event.title}</i>
190190
</div>

src/hooks/useAddGroupSchedule.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useAddNewGroup } from '@/react-queries/useAddNewGroup';
22
import { Member } from '@/types/Member';
3-
import { useRef } from 'react';
3+
import { useRef, useState } from 'react';
4+
import { isValidGroupInput } from '@/utils/groupScheduleUtils.ts';
45

56
interface DialogElement {
67
openModal: () => void;
@@ -12,6 +13,8 @@ export const useAddGroupSchedule = () => {
1213
const successDialogRef = useRef<DialogElement | null>(null);
1314
const failDialogRef = useRef<DialogElement | null>(null);
1415

16+
const [errorText, setErrorText] = useState('모임 일정 등록에 실패했습니다.');
17+
1518
const addGroupSchedule = (e: React.FormEvent<HTMLFormElement>, userList: Member[]) => {
1619
e.preventDefault();
1720
const formData = new FormData(e.currentTarget);
@@ -23,6 +26,15 @@ export const useAddGroupSchedule = () => {
2326
memo: formData.get('memo') as string,
2427
newMemberList: userList,
2528
};
29+
30+
const validationResult = isValidGroupInput(newGroupSchedule);
31+
32+
if (validationResult.error) {
33+
failDialogRef?.current?.openModal();
34+
setErrorText(validationResult.errorText);
35+
return;
36+
}
37+
2638
mutate(newGroupSchedule, {
2739
onError: (error) => {
2840
console.error(error);
@@ -36,6 +48,7 @@ export const useAddGroupSchedule = () => {
3648

3749
return {
3850
addGroupSchedule,
51+
errorText,
3952
isPending,
4053
successDialogRef,
4154
failDialogRef,

src/pages/AddGroupPage.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import { useAddGroupSchedule } from '@/hooks/useAddGroupSchedule';
55
import { Member } from '@/types/Member';
66

77
const AddGroupPage = () => {
8-
const { addGroupSchedule, isPending, failDialogRef, successDialogRef } = useAddGroupSchedule();
8+
const { addGroupSchedule, isPending, failDialogRef, successDialogRef, errorText } = useAddGroupSchedule();
99
const handleSubmit = (e: React.FormEvent<HTMLFormElement>, userList: Member[]) => {
1010
e.preventDefault();
11-
// TODO: 멤버 1명 이상이여야 한다.
12-
// TODO: 타이틀, 시작일, 종료시간 은 필수 값이며 빈 값일 수 없다.
13-
// TODO: 종료일은 시작일보다 크거나 같아야 한다.
11+
1412
addGroupSchedule(e, userList);
1513
};
1614

@@ -19,7 +17,7 @@ const AddGroupPage = () => {
1917
<AppBar backButton title={'모임 등록하기'} />
2018
<GroupForm onSubmit={handleSubmit} isLoading={isPending} />
2119
<Dialog ref={successDialogRef} desc="모임 일정이 등록되었습니다." />
22-
<Dialog ref={failDialogRef} desc="모임 일정 등록에 실패했습니다." />
20+
<Dialog ref={failDialogRef} desc={errorText} />
2321
</div>
2422
);
2523
};

src/pages/EditGroupPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const EditGroupPage = () => {
1212
const groupId = params.groupId!;
1313

1414
const { groupMemberData, groupData, error, isLoading, isError } = useGetGroupScheduleDefaultData(groupId);
15-
const { mutate, successDialog, errorDialogRef, isPending: updateIsPending } = useUpdateGroup();
15+
const { mutate, errorText, successDialog, errorDialogRef, isPending: updateIsPending } = useUpdateGroup();
1616

1717
if (isError) {
1818
return <div>{error!.message}</div>;
@@ -54,7 +54,7 @@ const EditGroupPage = () => {
5454
memberList={groupMemberData}
5555
isLoading={updateIsPending}
5656
/>
57-
<Dialog ref={errorDialogRef} desc="오류가 발생했습니다. 다시시도해 주세요." />
57+
<Dialog ref={errorDialogRef} desc={errorText} />
5858
<Dialog ref={successDialog} desc="성공적으로 수정했습니다." />
5959
</div>
6060
)}

src/react-queries/useUpdateGroup.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { useMutation } from '@tanstack/react-query';
22
import { queries } from './queryKeys';
33
import { Member } from '@/types/Member';
44
import { useNavigate } from 'react-router-dom';
5-
import { useEffect, useRef } from 'react';
5+
import { useEffect, useRef, useState } from 'react';
66
import { updateGroup } from '@/apis/updateGroupApi.ts';
77
import { updateGroupMember } from '@/apis/updateGroupMemberApi.ts';
8+
import { isValidGroupInput } from '@/utils/groupScheduleUtils.ts';
89

910
interface UpdateGroup {
1011
name: string;
@@ -27,6 +28,8 @@ export const useUpdateGroup = () => {
2728
const errorDialogRef = useRef<DialogElement | null>(null);
2829
const successDialog = useRef<DialogElement | null>(null);
2930

31+
const [errorText, setErrorText] = useState('모임 일정 수정에 실패했습니다.');
32+
3033
const {
3134
mutate: updateGroupMutate,
3235
isPending: updateGroupIsPending,
@@ -50,6 +53,14 @@ export const useUpdateGroup = () => {
5053
});
5154

5255
const mutate = (data: UpdateGroup) => {
56+
const validationResult = isValidGroupInput(data);
57+
58+
if (validationResult.error) {
59+
errorDialogRef?.current?.openModal();
60+
setErrorText(validationResult.errorText);
61+
return;
62+
}
63+
5364
updateGroupMutate(data);
5465
updateGroupMemberMutate({
5566
updatedMemberList: data.newMemberList,
@@ -75,6 +86,7 @@ export const useUpdateGroup = () => {
7586

7687
return {
7788
mutate,
89+
errorText,
7890
isPending: updateGroupIsPending || updateGroupMemberIsPending,
7991
errorDialogRef,
8092
successDialog,

src/utils/groupScheduleUtils.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isEmptyString } from '@/utils/stringUtils.ts';
2+
13
export const isValidGroupScheduleName = (name: string) => {
24
return name.length > 0;
35
};
@@ -33,5 +35,32 @@ const bothDatesHasValue = ({ startDate, endDate }: IsValidGroupSchedulePeriodPar
3335
};
3436

3537
const startDateIsEarlierThanEndDate = ({ startDate, endDate }: IsValidGroupSchedulePeriodParams) => {
36-
return new Date(startDate) < new Date(endDate);
38+
return new Date(startDate).toISOString().split('T')[0] <= new Date(endDate).toISOString().split('T')[0];
39+
};
40+
41+
interface IsValidGroupInputParams {
42+
name: string;
43+
startDate: string;
44+
endDate: string;
45+
}
46+
47+
export const isValidGroupInput = ({ name, startDate, endDate }: IsValidGroupInputParams) => {
48+
if (isEmptyString(name) || isEmptyString(startDate) || isEmptyString(endDate)) {
49+
return {
50+
error: true,
51+
errorText: '그룹 이름, 시작일, 종료일은 필수 입력 사항입니다.',
52+
};
53+
}
54+
55+
if (!startDateIsEarlierThanEndDate({ startDate, endDate })) {
56+
return {
57+
error: true,
58+
errorText: '모임 시작일이 종료일보다 빠를 수 없습니다.',
59+
};
60+
}
61+
62+
return {
63+
error: false,
64+
errorText: '',
65+
};
3766
};

src/utils/stringUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const isEmptyString = (str: string) => str.trim() === '';

0 commit comments

Comments
 (0)