From 15203f32cb48496d079f73ab8e68e2ba6eee27e8 Mon Sep 17 00:00:00 2001 From: hyeseon han Date: Fri, 23 Feb 2024 23:03:52 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=82=B4=20=EC=B4=88=EB=8C=80?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=A1=B0=ED=9A=8C=20query=20useGetMyInvit?= =?UTF-8?q?eCode=20function=EC=9C=BC=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/templates/AddFriendTemplate.tsx | 16 +++++++--------- src/hooks/queries/friendship/index.ts | 1 + .../queries/friendship/useGetMyInviteCode.ts | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 src/hooks/queries/friendship/useGetMyInviteCode.ts 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;