diff --git a/src/components/templates/AddFriendTemplate.tsx b/src/components/templates/AddFriendTemplate.tsx index 4d34801..d4466fc 100644 --- a/src/components/templates/AddFriendTemplate.tsx +++ b/src/components/templates/AddFriendTemplate.tsx @@ -1,11 +1,12 @@ import { LabelRoundBox, WarningLine } from '@/components/molecules'; import React, { useState } from 'react'; +import { + useAddFriendship, + useGetMyInviteCode, +} from '@/hooks/queries/friendship'; import { BulletNoticeBox } from '@/components/organisms'; import { MiniButton } from '@/components/atoms'; -import { queryKeys } from '@/hooks/queries/queryKeys'; -import { useAddFriendship } from '@/hooks/queries/friendship'; -import { useBaseQuery } from '@/hooks/queries/useBaseQuery'; import useToast from '@/hooks/useToast'; const FRIEND_ADD_SUCCESS_MESSAGE = '친구 추가가 완료되었습니다.'; @@ -23,7 +24,7 @@ const AddFriendTemplate: React.FC = () => { const onCopy: () => void = () => { navigator.clipboard - .writeText(myInviteCode?.data?.inviteCode ?? '') + .writeText(myInviteCode ?? '') .then(() => { showToast(CODE_COPY_SUCCESS_MESSAGE, 'success'); }) @@ -38,10 +39,7 @@ const AddFriendTemplate: React.FC = () => { } }; - const { data: myInviteCode } = useBaseQuery<{ inviteCode: string }>( - queryKeys.MY_INVITE_CODE(), - '/users/me/invite-code', - ); + const { inviteCode: myInviteCode } = useGetMyInviteCode(); return (
@@ -50,7 +48,7 @@ const AddFriendTemplate: React.FC = () => { content={
- {myInviteCode?.data?.inviteCode} + {myInviteCode ?? ''}
diff --git a/src/hooks/queries/friendship/index.ts b/src/hooks/queries/friendship/index.ts index a69da17..baf19b4 100644 --- a/src/hooks/queries/friendship/index.ts +++ b/src/hooks/queries/friendship/index.ts @@ -1,3 +1,4 @@ export { default as useGetFriendships } from './useGetFriendships'; export { default as useDeleteFriendship } from './useDeleteFriendship'; export { default as useAddFriendship } from './useAddFriendship'; +export { default as useGetMyInviteCode } from './useGetMyInviteCode'; diff --git a/src/hooks/queries/friendship/useGetMyInviteCode.ts b/src/hooks/queries/friendship/useGetMyInviteCode.ts new file mode 100644 index 0000000..bf8c6ce --- /dev/null +++ b/src/hooks/queries/friendship/useGetMyInviteCode.ts @@ -0,0 +1,17 @@ +import { queryKeys } from '../queryKeys'; +import { useBaseQuery } from '../useBaseQuery'; + +const useGetMyInviteCode = () => { + const { data } = useBaseQuery<{ inviteCode: string }>( + queryKeys.MY_INVITE_CODE(), + '/users/me/invite-code', + ); + + if (!data?.data) { + return { inviteCode: '-' }; + } + + return data?.data; +}; + +export default useGetMyInviteCode;