-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This reverts commit f889a79.
- Loading branch information
Showing
11 changed files
with
66 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,43 @@ | ||
'use client'; | ||
|
||
import { notFound, useRouter } from 'next/navigation'; | ||
import { FormProvider, SubmitHandler, useForm } from 'react-hook-form'; | ||
|
||
import { | ||
useBookGroupEditCurrentInfo, | ||
useBookGroupInfoMutation, | ||
} from '@/queries/group/useBookGroupQuery'; | ||
import type { APIGroupDetail, APIEditBookGroup } from '@/types/group'; | ||
|
||
import { SERVICE_ERROR_MESSAGE } from '@/constants'; | ||
import { | ||
checkAuthentication, | ||
isAxiosErrorWithCustomCode, | ||
} from '@/utils/helpers'; | ||
|
||
import useToast from '@/v1/base/Toast/useToast'; | ||
import BookGroupEditDateForm from '@/v1/bookGroup/edit/BookGroupEditDateForm'; | ||
import BookGroupEditIntroduceForm from '@/v1/bookGroup/edit/BookGroupEditIntroduceForm'; | ||
import BookGroupEditTitleForm from '@/v1/bookGroup/edit/BookGroupEditTitleForm'; | ||
import BookGroupEditTopNavigation from '@/v1/bookGroup/edit/BookGroupEditTopNavigation'; | ||
|
||
const BookGroupEditPage = ({ | ||
import GroupAPI from '@/apis/group'; | ||
import { APIGroupDetail } from '@/types/group'; | ||
import AuthRequired from '@/ui/AuthRequired'; | ||
import TopNavigation from '@/ui/common/TopNavigation'; | ||
import EditGroupForm from '@/ui/Group/EditGroupForm'; | ||
import { VStack } from '@chakra-ui/react'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
const GroupEditPage = ({ | ||
params: { groupId }, | ||
}: { | ||
params: { groupId: APIGroupDetail['bookGroupId'] }; | ||
params: { groupId: number }; | ||
}) => { | ||
const router = useRouter(); | ||
|
||
const isAuthenticated = checkAuthentication(); | ||
|
||
const { data: bookGroupData } = useBookGroupEditCurrentInfo(groupId); | ||
const { isOwner, title, description, maxMemberCount, startDate, endDate } = | ||
bookGroupData; | ||
|
||
if (!isAuthenticated || !isOwner) { | ||
notFound(); | ||
} | ||
|
||
const bookGroupEdit = useBookGroupInfoMutation(groupId); | ||
|
||
const { show: showToast } = useToast(); | ||
|
||
const methods = useForm<Omit<APIEditBookGroup, 'isOwner'>>({ | ||
mode: 'all', | ||
defaultValues: { | ||
title: title, | ||
introduce: description, | ||
maxMemberCount: maxMemberCount ? maxMemberCount : 9999, | ||
startDate: startDate, | ||
endDate: endDate, | ||
}, | ||
}); | ||
|
||
const handleFormSubmit: SubmitHandler< | ||
Omit<APIEditBookGroup, 'isOwner' | 'startDate'> | ||
> = async ({ title, introduce, maxMemberCount, endDate }) => { | ||
bookGroupEdit.mutate( | ||
{ title, introduce, maxMemberCount, endDate }, | ||
{ | ||
onSuccess: () => { | ||
router.push(`/group/${groupId}`); | ||
|
||
showToast({ type: 'success', message: '모임 정보를 수정했어요! 🎉' }); | ||
return; | ||
}, | ||
onError: error => { | ||
if (isAxiosErrorWithCustomCode(error)) { | ||
const { code } = error.response.data; | ||
const message = SERVICE_ERROR_MESSAGE[code]; | ||
|
||
showToast({ type: 'error', message }); | ||
return; | ||
} | ||
|
||
showToast({ | ||
type: 'error', | ||
message: '모임 정보 수정을 실패했어요 🥲', | ||
}); | ||
}, | ||
} | ||
); | ||
}; | ||
const [group, setGroup] = useState<APIGroupDetail>(); | ||
|
||
const getGroup = useCallback(async () => { | ||
try { | ||
const { data } = await GroupAPI.getGroupDetailInfo({ | ||
bookGroupId: groupId, | ||
}); | ||
setGroup(data); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}, [groupId]); | ||
|
||
useEffect(() => { | ||
getGroup(); | ||
}, [getGroup]); | ||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<BookGroupEditTopNavigation onSubmit={handleFormSubmit} /> | ||
|
||
<form | ||
className="mt-[2.5rem] flex flex-col gap-[3.2rem]" | ||
onSubmit={methods.handleSubmit(handleFormSubmit)} | ||
> | ||
<BookGroupEditTitleForm /> | ||
<BookGroupEditIntroduceForm /> | ||
<BookGroupEditDateForm /> | ||
</form> | ||
</FormProvider> | ||
<AuthRequired> | ||
<VStack justify="center" align="center"> | ||
<TopNavigation pageTitle="모임 수정" /> | ||
{group && <EditGroupForm group={group} />} | ||
</VStack> | ||
</AuthRequired> | ||
); | ||
}; | ||
|
||
export default BookGroupEditPage; | ||
export default GroupEditPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.