Skip to content

Commit

Permalink
refactor: 내 초대코드 조회 query useGetMyInviteCode function으로 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeseon-han committed Feb 23, 2024
1 parent b9f9fb7 commit 15203f3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/components/templates/AddFriendTemplate.tsx
Original file line number Diff line number Diff line change
@@ -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 = '친구 추가가 완료되었습니다.';
Expand All @@ -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');
})
Expand All @@ -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 (
<div className="pt-[72px] px-[20px]">
Expand All @@ -50,7 +48,7 @@ const AddFriendTemplate: React.FC = () => {
content={
<div className="flex">
<span className="flex-1 outline-none mr-[10px] border-none p-[10px] bg-gray1 rounded-[6px] text-gray8 body1-medium">
{myInviteCode?.data?.inviteCode}
{myInviteCode ?? ''}
</span>
<MiniButton label="복사" onClick={onCopy} variant="active" />
</div>
Expand Down
1 change: 1 addition & 0 deletions src/hooks/queries/friendship/index.ts
Original file line number Diff line number Diff line change
@@ -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';
17 changes: 17 additions & 0 deletions src/hooks/queries/friendship/useGetMyInviteCode.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 15203f3

Please sign in to comment.