Skip to content

Commit 8ecf2f8

Browse files
committed
Feat: 팔로우 커스텀 훅 초안 작성
1 parent d80a0e5 commit 8ecf2f8

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

src/components/UserProfile/UserProfileHeader/index.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { useRouter } from 'next/navigation';
55
import Image from 'next/image';
66
import { Button } from '@/components/common/Button/Button';
77
import { useUserProfileQuery } from '@/hooks/apis/Auth/useUserProfileQuery';
8+
// import { useAssignFollowMutation } from '@/hooks/apis/Follow/useAssignFollowMutation';
9+
// import { useDeleteFollowMutation } from '@/hooks/apis/Follow/useDeleteFollowMutation';
810

911
interface UserProfileHeader {
1012
userId: string;
@@ -14,11 +16,22 @@ export const UserProfileHeader = ({ userId }: UserProfileHeader) => {
1416
const router = useRouter();
1517

1618
const { name, profilePic, isFollow } = useUserProfileQuery(Number(userId));
19+
// const { mutate: assignFollow, isPending } = useAssignFollowMutation();
20+
// const { mutate: deleteFollow } = useDeleteFollowMutation();
1721

1822
const handleBack = () => {
1923
router.back();
2024
};
2125

26+
const handleClickFollow = () => {
27+
if (isFollow) {
28+
console.log('dddd');
29+
} else {
30+
//assignFollow(Number(userId));
31+
//deleteFollow(Number(userId));
32+
}
33+
};
34+
2235
return (
2336
<>
2437
<IoMdClose className="size-24 cursor-pointer" onClick={handleBack} />
@@ -36,7 +49,12 @@ export const UserProfileHeader = ({ userId }: UserProfileHeader) => {
3649
) : null}
3750
<span className="text-base-medium">{name}</span>
3851
</div>
39-
<Button size="small" primary={isFollow ? false : true}>
52+
<Button
53+
size="small"
54+
primary={isFollow ? false : true}
55+
onClick={handleClickFollow}
56+
// pending={isPending}
57+
>
4058
{isFollow ? '팔로우' : '팔로잉'}
4159
</Button>
4260
</div>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useMutation, useQueryClient } from '@tanstack/react-query';
2+
import { POST } from '@/apis/services/httpMethod';
3+
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
4+
import { AssignFollowResponse } from '@/types/response';
5+
import { notify } from '@/store/useToastStore';
6+
import { QUERY_KEYS } from '@/constants/QueryKeys';
7+
8+
interface FollowId {
9+
userId: number;
10+
}
11+
12+
export const useAssignFollowMutation = () => {
13+
const queryClient = useQueryClient();
14+
15+
return useMutation({
16+
mutationFn: (userId: number) =>
17+
POST<AssignFollowResponse, FollowId>(
18+
API_ENDPOINTS.FOLLOW.ASSIGN_FOLLOW(userId),
19+
),
20+
onSuccess: (data) => {
21+
notify('success', '팔로우 등록', 3000);
22+
queryClient.invalidateQueries({
23+
queryKey: [QUERY_KEYS.USER_PROFILE, data.data.followId],
24+
});
25+
},
26+
onError: (error) => {
27+
console.error(error.message);
28+
notify('error', '팔로우 등록 실패', 3000);
29+
},
30+
});
31+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useMutation, useQueryClient } from '@tanstack/react-query';
2+
import { POST } from '@/apis/services/httpMethod';
3+
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
4+
import { DeleteFollowResponse } from '@/types/response';
5+
import { notify } from '@/store/useToastStore';
6+
import { QUERY_KEYS } from '@/constants/QueryKeys';
7+
8+
interface FollowId {
9+
userId: number;
10+
}
11+
12+
export const useDeleteFollowMutation = () => {
13+
const queryClient = useQueryClient();
14+
15+
return useMutation({
16+
mutationFn: (userId: number) =>
17+
POST<DeleteFollowResponse, FollowId>(
18+
API_ENDPOINTS.FOLLOW.DELETE_FOLLOW(userId),
19+
),
20+
onSuccess: (data) => {
21+
notify('success', '팔로우 취소', 3000);
22+
queryClient.invalidateQueries({
23+
queryKey: [QUERY_KEYS.USER_PROFILE, data.data.followerId],
24+
});
25+
},
26+
onError: (error) => {
27+
console.error(error.message);
28+
notify('error', '팔로우 취소 실패', 3000);
29+
},
30+
});
31+
};

0 commit comments

Comments
 (0)