diff --git a/src/components/molecules/LabelRoundBox.tsx b/src/components/molecules/LabelRoundBox.tsx index 87aaf4e..f47406a 100644 --- a/src/components/molecules/LabelRoundBox.tsx +++ b/src/components/molecules/LabelRoundBox.tsx @@ -7,7 +7,7 @@ const LabelRoundBox: React.FC<{ return (

{label}

-
{content}
+ {content}
); }; diff --git a/src/components/templates/AddFriendTemplate.tsx b/src/components/templates/AddFriendTemplate.tsx index 1bc095f..4da82e3 100644 --- a/src/components/templates/AddFriendTemplate.tsx +++ b/src/components/templates/AddFriendTemplate.tsx @@ -1,18 +1,40 @@ -import { BulletNoticeBox } from '../organisms'; -import { LabelRoundBox } from '../molecules'; +import { LabelRoundBox, WarningLine } from '@/components/molecules'; +import React, { useState } from 'react'; + +import { BulletNoticeBox } from '@/components/organisms'; import { MiniButton } from '@/components/atoms'; -import React from 'react'; import { queryKeys } from '@/hooks/queries/queryKeys'; +import { useAddFriendship } from '@/hooks/queries/friendship'; import { useBaseQuery } from '@/hooks/queries/useBaseQuery'; +import useToast from '@/hooks/useToast'; const AddFriendTemplate: React.FC = () => { + const [friendInviteCode, setFriendInviteCode] = useState(''); + const [warningVisible, setWarningVisible] = useState(false); + const { showToast } = useToast(); + const addFriendship = useAddFriendship({ + onSuccess: () => { + showToast('친구 추가가 완료되었습니다.', 'success'); + }, + }); + const onCopy: () => void = () => { navigator.clipboard .writeText(myInviteCode?.data?.inviteCode ?? '') - .then(() => null) + .then(() => { + showToast('초대 코드가 복사되었습니다.', 'success'); + }) .catch(() => null); }; + const onAddFriend = () => { + if (friendInviteCode.length < 9) { + setWarningVisible(true); + } else { + addFriendship.mutate({ inviteCode: friendInviteCode }); + } + }; + const { data: myInviteCode } = useBaseQuery<{ inviteCode: string }>( queryKeys.MY_INVITE_CODE(), '/users/me/invite-code', @@ -23,23 +45,37 @@ const AddFriendTemplate: React.FC = () => { +
{myInviteCode?.data?.inviteCode} - +
} /> - - +
+ { + setFriendInviteCode(e.target.value); + }} + maxLength={10} + /> + +
+ {warningVisible ? ( + + ) : null} } /> diff --git a/src/hooks/queries/friendship/index.ts b/src/hooks/queries/friendship/index.ts index 2c70a67..a69da17 100644 --- a/src/hooks/queries/friendship/index.ts +++ b/src/hooks/queries/friendship/index.ts @@ -1,2 +1,3 @@ export { default as useGetFriendships } from './useGetFriendships'; export { default as useDeleteFriendship } from './useDeleteFriendship'; +export { default as useAddFriendship } from './useAddFriendship'; diff --git a/src/hooks/queries/friendship/useAddFriendship.ts b/src/hooks/queries/friendship/useAddFriendship.ts new file mode 100644 index 0000000..8a497ca --- /dev/null +++ b/src/hooks/queries/friendship/useAddFriendship.ts @@ -0,0 +1,11 @@ +import { queryKeys } from '../queryKeys'; +import { useBaseMutation } from '../useBaseMutation'; + +const useAddFriendship = ({ onSuccess }: { onSuccess: () => void }) => { + return useBaseMutation<{ inviteCode: string }>( + queryKeys.ADD_FRIENDSHIP(), + `/friendship`, + onSuccess, + ); +}; +export default useAddFriendship; diff --git a/src/hooks/queries/queryKeys.ts b/src/hooks/queries/queryKeys.ts index ede5a80..0dbad20 100644 --- a/src/hooks/queries/queryKeys.ts +++ b/src/hooks/queries/queryKeys.ts @@ -7,6 +7,7 @@ export const queryKeys = { FRIENDSHIPS: (sort: FriendshipSortType) => ['friendship', sort], DELETE_FRIENDSHIP: () => ['deleteFriendship'], MY_INVITE_CODE: () => ['myInviteCode'], + ADD_FRIENDSHIP: () => ['addFriendship'], } as const; export type QueryKeys = (typeof queryKeys)[keyof typeof queryKeys];