-
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.
- Loading branch information
Showing
12 changed files
with
399 additions
and
164 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
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use client'; | ||
|
||
import { notFound, useRouter } from 'next/navigation'; | ||
import { SubmitHandler, useForm } from 'react-hook-form'; | ||
|
||
import useJoinBookGroup from '@/hooks/group/useJoinBookGroup'; | ||
|
||
import SSRSafeSuspense from '@/components/SSRSafeSuspense'; | ||
import Loading from '@/v1/base/Loading'; | ||
import Input from '@/v1/base/Input'; | ||
import InputLength from '@/v1/base/InputLength'; | ||
import ErrorMessage from '@/v1/base/ErrorMessage'; | ||
import BottomActionButton from '@/v1/base/BottomActionButton'; | ||
import BookGroupNavigation from '@/v1/bookGroup/BookGroupNavigation'; | ||
|
||
type JoinFormValues = { | ||
answer: string; | ||
}; | ||
|
||
const JoinBookGroupPage = ({ | ||
params: { groupId }, | ||
}: { | ||
params: { groupId: number }; | ||
}) => { | ||
return ( | ||
<SSRSafeSuspense fallback={<Loading fullpage />}> | ||
<BookGroupNavigation groupId={groupId}> | ||
<BookGroupNavigation.BackButton | ||
href={`/group/${groupId}`} | ||
routeOption="replace" | ||
/> | ||
<BookGroupNavigation.Title /> | ||
</BookGroupNavigation> | ||
<BookGroupJoinForm groupId={groupId} /> | ||
</SSRSafeSuspense> | ||
); | ||
}; | ||
|
||
const BookGroupJoinForm = ({ groupId }: { groupId: number }) => { | ||
const router = useRouter(); | ||
const { isMember, hasPassword, question, joinBookGroup } = | ||
useJoinBookGroup(groupId); | ||
|
||
if (isMember || !hasPassword) { | ||
notFound(); | ||
} | ||
|
||
const { | ||
register, | ||
watch, | ||
handleSubmit, | ||
formState: { errors }, | ||
} = useForm<JoinFormValues>({ mode: 'all' }); | ||
|
||
const submitJoinForm: SubmitHandler<JoinFormValues> = ({ answer }) => { | ||
joinBookGroup({ | ||
answer, | ||
onSuccess: () => router.replace(`/group/${groupId}`), | ||
}); | ||
}; | ||
|
||
return ( | ||
<form | ||
className="mt-[2.5rem] flex flex-col gap-[2.5rem]" | ||
onSubmit={handleSubmit(submitJoinForm)} | ||
> | ||
<p className="whitespace-pre-line text-2xl font-bold leading-snug"> | ||
{`문제를 맞추면 | ||
모임에 가입할 수 있어요`} | ||
</p> | ||
<div className="flex flex-col gap-[1.5rem]"> | ||
<p className="text-sm">{question}</p> | ||
<div className="flex flex-col gap-[0.5rem]"> | ||
<Input | ||
{...register('answer', { | ||
required: '정답을 입력해주세요', | ||
pattern: { | ||
value: /^\S*$/g, | ||
message: '띄어쓰기 없이 정답을 입력해주세요.', | ||
}, | ||
minLength: { value: 1, message: '1자 이상 입력해주세요.' }, | ||
maxLength: { value: 10, message: '10자 이하 입력해주세요.' }, | ||
})} | ||
placeholder="띄어쓰기 없이 정답을 입력해주세요" | ||
error={!!errors.answer} | ||
/> | ||
<div className="flex flex-row-reverse justify-between gap-[0.4rem]"> | ||
<InputLength | ||
isError={!!errors.answer} | ||
currentLength={watch('answer')?.length} | ||
maxLength={10} | ||
/> | ||
{errors.answer && ( | ||
<ErrorMessage>{errors.answer.message}</ErrorMessage> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
<BottomActionButton type="submit">제출하기</BottomActionButton> | ||
</form> | ||
); | ||
}; | ||
|
||
export default JoinBookGroupPage; |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { isAxiosErrorWithCustomCode } from '@/utils/helpers'; | ||
import { SERVICE_ERROR_MESSAGE } from '@/constants'; | ||
import groupAPI from '@/apis/group'; | ||
import useToast from '@/v1/base/Toast/useToast'; | ||
import { useBookGroupJoinInfo } from '@/queries/group/useBookGroupQuery'; | ||
|
||
const useJoinBookGroup = (groupId: number) => { | ||
const { data: bookGroupJoinData, refetch } = useBookGroupJoinInfo(groupId); | ||
const { isExpired, isMember, hasPassword, question } = bookGroupJoinData; | ||
|
||
const toast = useToast(); | ||
|
||
const joinBookGroup = async ({ | ||
answer, | ||
onSuccess, | ||
}: { | ||
answer?: string; | ||
onSuccess?: () => void; | ||
}) => { | ||
try { | ||
await groupAPI.joinGroup({ bookGroupId: groupId, password: answer }); | ||
toast.show({ message: '🎉 모임에 가입되었어요! 🎉', type: 'success' }); | ||
onSuccess && onSuccess(); | ||
} catch (error) { | ||
if (!isAxiosErrorWithCustomCode(error)) { | ||
toast.show({ message: '잠시 후 다시 시도해주세요', type: 'error' }); | ||
return; | ||
} | ||
|
||
const { code } = error.response.data; | ||
const message = SERVICE_ERROR_MESSAGE[code]; | ||
const isWrongAnswerErrorCode = code === 'BG3'; | ||
|
||
if (isWrongAnswerErrorCode) { | ||
toast.show({ | ||
message: '정답이 아니에요. 다시 시도해주세요!', | ||
type: 'error', | ||
}); | ||
return; | ||
} | ||
|
||
toast.show({ message, type: 'error' }); | ||
} | ||
}; | ||
|
||
return { | ||
isExpired, | ||
isMember, | ||
hasPassword, | ||
question, | ||
refetch, | ||
joinBookGroup, | ||
}; | ||
}; | ||
|
||
export default useJoinBookGroup; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import type { APIBookshelfInfo } from '@/types/bookshelf'; | ||
import bookshelfAPI from '@/apis/bookshelf'; | ||
import bookShelfKeys from './key'; | ||
|
||
const useMutateBookshelfLikeQuery = ( | ||
bookshelfId: APIBookshelfInfo['bookshelfId'] | ||
) => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation({ | ||
mutationFn: async (isLiked: APIBookshelfInfo['isLiked']) => | ||
!isLiked | ||
? bookshelfAPI.likeBookshelf(bookshelfId) | ||
: bookshelfAPI.unlikeBookshelf(bookshelfId), | ||
onMutate: async () => { | ||
await queryClient.cancelQueries(bookShelfKeys.info(bookshelfId)); | ||
|
||
const prevData = queryClient.getQueryData<APIBookshelfInfo>( | ||
bookShelfKeys.info(bookshelfId) | ||
); | ||
|
||
if (prevData) { | ||
const newData: APIBookshelfInfo = { | ||
...prevData, | ||
isLiked: !prevData.isLiked, | ||
likeCount: prevData.isLiked | ||
? prevData.likeCount - 1 | ||
: prevData.likeCount + 1, | ||
}; | ||
|
||
queryClient.setQueryData<APIBookshelfInfo>( | ||
bookShelfKeys.info(bookshelfId), | ||
newData | ||
); | ||
} | ||
|
||
return { prevData }; | ||
}, | ||
onError: (_error, _value, context) => { | ||
queryClient.setQueryData( | ||
bookShelfKeys.info(bookshelfId), | ||
context?.prevData | ||
); | ||
}, | ||
onSettled: () => { | ||
queryClient.invalidateQueries(bookShelfKeys.info(bookshelfId)); | ||
}, | ||
}); | ||
}; | ||
|
||
export default useMutateBookshelfLikeQuery; |
Oops, something went wrong.