Skip to content

Commit 8aeaaa3

Browse files
authored
Merge pull request #343 from Team-TenTen/bug/#342/follow-button
팔로우 유무에 따라 버튼이 변경되지 않는 문제 수정
2 parents b227678 + f8096b9 commit 8aeaaa3

File tree

8 files changed

+140
-155
lines changed

8 files changed

+140
-155
lines changed
Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,75 @@
1+
'use client'
2+
13
import {
24
Avatar,
35
CategoryListItem,
46
FollowListButton,
57
ProfileEditButton,
8+
Spinner,
69
} from '@/components'
710
import { CATEGORIES_RENDER, PROFILE_MSG } from '@/constants'
8-
import { fetchGetUserProfile } from '@/services/users/useUsers'
11+
import { useGetUserProfile } from '@/services/users/useUsers'
912
import { UserLayoutProps } from './layout'
1013

11-
export default async function UserPage({
12-
params: { userId },
13-
}: UserLayoutProps) {
14-
const user = await fetchGetUserProfile({ memberId: userId })
14+
export default function UserPage({ params: { userId } }: UserLayoutProps) {
15+
const { data: user, isFetching: isUserLoading } = useGetUserProfile(
16+
Number(userId),
17+
)
1518

1619
return (
1720
<>
18-
<div className="flex flex-col gap-4 px-4 py-6">
19-
<div className="flex gap-3">
20-
{user?.profileImagePath && (
21-
<div className="relative h-20 w-20">
22-
<Avatar
23-
src={user.profileImagePath}
24-
alt="profile"
25-
/>
21+
{isUserLoading ? (
22+
<Spinner />
23+
) : (
24+
<div className="flex flex-col gap-4 px-4 py-6">
25+
<div className="flex gap-3">
26+
{user?.profileImagePath && (
27+
<div className="relative h-20 w-20">
28+
<Avatar
29+
src={user.profileImagePath}
30+
alt="profile"
31+
/>
32+
</div>
33+
)}
34+
<div className="flex flex-col gap-1 py-0.5">
35+
<div className="text-xl font-semibold text-gray9">
36+
{user?.nickname}
37+
</div>
38+
<div className="text-xs font-medium text-gray6">
39+
{user?.newsEmail}
40+
</div>
41+
<div className="flex gap-1 text-xs font-medium text-gray6">
42+
<FollowListButton user={user} />
43+
</div>
2644
</div>
27-
)}
28-
<div className="flex flex-col gap-1 py-0.5">
29-
<div className="text-xl font-semibold text-gray9">
30-
{user?.nickname}
31-
</div>
32-
<div className="text-xs font-medium text-gray6">
33-
{user?.newsEmail}
45+
</div>
46+
<ProfileEditButton user={user} />
47+
<div className="flex flex-col ">
48+
<div className="py-3 text-sm font-semibold text-gray9">
49+
{PROFILE_MSG.FAVORITE_CATEGORY}
3450
</div>
35-
<div className="flex gap-1 text-xs font-medium text-gray6">
36-
<FollowListButton user={user} />
51+
<div>
52+
<CategoryListItem
53+
label={
54+
user?.favoriteCategory
55+
? CATEGORIES_RENDER[user.favoriteCategory]
56+
: '없음'
57+
}
58+
active={true}
59+
disabled={true}
60+
/>
3761
</div>
3862
</div>
39-
</div>
40-
<ProfileEditButton user={user} />
41-
<div className="flex flex-col ">
42-
<div className="py-3 text-sm font-semibold text-gray9">
43-
{PROFILE_MSG.FAVORITE_CATEGORY}
44-
</div>
4563
<div>
46-
<CategoryListItem
47-
label={
48-
user?.favoriteCategory
49-
? CATEGORIES_RENDER[user.favoriteCategory]
50-
: '없음'
51-
}
52-
active={true}
53-
disabled={true}
54-
/>
55-
</div>
56-
</div>
57-
<div>
58-
<div className="py-3 text-sm font-semibold text-gray9">
59-
{PROFILE_MSG.DESCRIPTION}
64+
<div className="py-3 text-sm font-semibold text-gray9">
65+
{PROFILE_MSG.DESCRIPTION}
66+
</div>
67+
<div className="text-sm font-normal text-gray9">
68+
{user?.aboutMe}
69+
</div>
6070
</div>
61-
<div className="text-sm font-normal text-gray9">{user?.aboutMe}</div>
6271
</div>
63-
</div>
72+
)}
6473
</>
6574
)
6675
}

src/components/FollowListButton/FollowListButton.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client'
22

33
import { PROFILE_MSG } from '@/constants'
4-
import { useFollowUser, useModal } from '@/hooks'
4+
import { useModal } from '@/hooks'
55
import { useCurrentUser } from '@/hooks/useCurrentUser'
66
import { fetchGetFollowers, fetchGetFollowing } from '@/services/users/useUsers'
77
import { UserProfileResBody } from '@/types'
@@ -13,13 +13,6 @@ const FollowListButton = ({ user }: { user: UserProfileResBody }) => {
1313
const myId = currentUser?.memberId
1414
const { Modal, isOpen, modalClose, currentModal, handleOpenCurrentModal } =
1515
useModal()
16-
const { followingCount, setFollowingCount, followerCount } = useFollowUser({
17-
memberId: user?.memberId || 0,
18-
isInitFollowing: !!user?.isFollowing,
19-
followingInitCount: user?.followingCount || 0,
20-
followerInitCount: user?.followerCount || 0,
21-
handleOpenCurrentModal,
22-
})
2316

2417
return (
2518
<>
@@ -28,15 +21,15 @@ const FollowListButton = ({ user }: { user: UserProfileResBody }) => {
2821
onClick={() => {
2922
handleOpenCurrentModal('following')
3023
}}>
31-
{PROFILE_MSG.FOLLOWING} {followingCount}
24+
{PROFILE_MSG.FOLLOWING} {user?.followingCount}
3225
</div>
3326
{PROFILE_MSG.LIST_DIVIDER}
3427
<div
3528
className="cursor-pointer hover:font-semibold"
3629
onClick={() => {
3730
handleOpenCurrentModal('follower')
3831
}}>
39-
{PROFILE_MSG.FOLLOWER} {followerCount}
32+
{PROFILE_MSG.FOLLOWER} {user?.followerCount}
4033
</div>
4134
{currentModal !== 'login' && isOpen && (
4235
<Modal
@@ -54,8 +47,7 @@ const FollowListButton = ({ user }: { user: UserProfileResBody }) => {
5447
fetchFn={fetchGetFollowing}
5548
myId={myId}
5649
type="following"
57-
followingCount={followingCount}
58-
setFollowingCount={setFollowingCount}
50+
followingCount={user?.followingCount}
5951
/>
6052
)}
6153
{currentModal === 'follower' && (
@@ -64,8 +56,7 @@ const FollowListButton = ({ user }: { user: UserProfileResBody }) => {
6456
fetchFn={fetchGetFollowers}
6557
myId={myId}
6658
type="follower"
67-
followingCount={followingCount}
68-
setFollowingCount={setFollowingCount}
59+
followingCount={user?.followingCount}
6960
/>
7061
)}
7162
</div>

src/components/ProfileEditButton/ProfileEditButton.tsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,39 @@ const ProfileEditButton = ({ user }: { user: UserProfileResBody }) => {
1414
const { currentUser } = useCurrentUser()
1515
const myId = currentUser?.memberId
1616
const { handleOpenCurrentModal } = useModal()
17-
const { isFollowing, handleClickFollow } = useFollowUser({
18-
memberId: user?.memberId || 0,
19-
isInitFollowing: !!user?.isFollowing,
20-
followingInitCount: user?.followingCount || 0,
21-
followerInitCount: user?.followerCount || 0,
17+
const { handleClickFollow } = useFollowUser({
18+
profileId: user?.memberId || 0,
19+
memberId: currentUser?.memberId || 0,
20+
myId: myId || 0,
2221
handleOpenCurrentModal,
2322
})
2423

25-
return myId ? (
24+
return (
2625
<Button
2726
type="button"
2827
onClick={() => {
2928
if (user?.memberId === myId) {
3029
router.push('/user/setting')
31-
} else if (isFollowing) {
32-
handleClickFollow(isFollowing)
30+
} else if (user?.isFollowing) {
31+
handleClickFollow(user?.isFollowing)
3332
} else {
34-
handleClickFollow(isFollowing)
33+
handleClickFollow(user?.isFollowing)
3534
}
3635
}}
3736
className={cls(
3837
'button button-md button-lg',
3938
getProfileButtonColor({
40-
isFollowing,
39+
isFollowing: user?.isFollowing,
4140
memberId: user?.memberId,
4241
myId,
4342
}),
4443
)}>
4544
{getProfileButtonText({
46-
isFollowing,
45+
isFollowing: user?.isFollowing,
4746
memberId: user?.memberId,
4847
myId,
4948
})}
5049
</Button>
51-
) : (
52-
<DeferredComponent>
53-
<Spinner />
54-
</DeferredComponent>
5550
)
5651
}
5752

src/components/common/FollowList/FollowList.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Dispatch, Fragment, SetStateAction } from 'react'
1+
import { Fragment, useEffect } from 'react'
22
import { Spinner } from '@/components'
33
import useFollowQuery from '@/components/common/FollowList/hooks/useFollowQuery'
44
import useInfiniteScroll from '@/hooks/useInfiniteScroll'
@@ -12,7 +12,6 @@ export interface FollowListProps {
1212
myId?: number
1313
type?: string
1414
followingCount?: number
15-
setFollowingCount?: Dispatch<SetStateAction<number | undefined>>
1615
}
1716

1817
export interface FollowUserProps {
@@ -29,7 +28,6 @@ const FollowList = ({
2928
myId,
3029
type,
3130
followingCount,
32-
setFollowingCount,
3331
}: FollowListProps) => {
3432
const { followList, fetchNextPage, hasNextPage, isFollowLoading } =
3533
useFollowQuery({
@@ -38,11 +36,11 @@ const FollowList = ({
3836
type,
3937
})
4038
const { target } = useInfiniteScroll({ hasNextPage, fetchNextPage })
41-
39+
useEffect(() => {
40+
console.log(isFollowLoading)
41+
}, [isFollowLoading])
4242
return isFollowLoading ? (
43-
<DeferredComponent>
44-
<Spinner />
45-
</DeferredComponent>
43+
<Spinner />
4644
) : (
4745
<ul className="flex flex-col gap-y-2">
4846
{followList &&
@@ -60,7 +58,6 @@ const FollowList = ({
6058
followingCount={followingCount}
6159
myId={myId}
6260
profileId={memberId}
63-
setFollowingCount={setFollowingCount}
6461
/>
6562
</li>
6663
))}

src/components/common/FollowList/hooks/useFollowQuery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useInfiniteQuery } from '@tanstack/react-query'
55
const useFollowQuery = ({ memberId, fetchFn, type }: FollowListProps) => {
66
const queryKey =
77
type === 'following' ? QUERY_KEYS.FOLLOWING : QUERY_KEYS.FOLLOWERS
8-
const { data, fetchNextPage, hasNextPage, isLoading } = useInfiniteQuery({
8+
const { data, fetchNextPage, hasNextPage, isFetching } = useInfiniteQuery({
99
queryKey: [queryKey, memberId],
1010
queryFn: ({ pageParam }) =>
1111
fetchFn({
@@ -22,7 +22,7 @@ const useFollowQuery = ({ memberId, fetchFn, type }: FollowListProps) => {
2222
followList: data,
2323
fetchNextPage,
2424
hasNextPage,
25-
isFollowLoading: isLoading,
25+
isFollowLoading: isFetching,
2626
}
2727
}
2828

src/components/common/User/User.tsx

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use client'
22

3-
import { Dispatch, SetStateAction } from 'react'
43
import { PROFILE_MSG } from '@/constants'
54
import { useFollowUser, useModal } from '@/hooks'
65
import { cls } from '@/utils'
@@ -14,12 +13,11 @@ interface UserProps {
1413
nickname: string
1514
profileImagePath: string
1615
aboutMe?: string
17-
isFollowing?: boolean
16+
isFollowing: boolean
1817
isAuth?: boolean
1918
followingCount?: number
2019
myId?: number
2120
profileId?: number
22-
setFollowingCount?: Dispatch<SetStateAction<number | undefined>>
2321
}
2422

2523
const User = ({
@@ -29,20 +27,16 @@ const User = ({
2927
aboutMe,
3028
isFollowing,
3129
isAuth,
32-
followingCount,
3330
myId,
3431
profileId,
35-
setFollowingCount,
3632
}: UserProps) => {
3733
const { Modal, isOpen, modalClose, currentModal, handleOpenCurrentModal } =
3834
useModal()
39-
const { isFollowing: isFollowingValue, handleClickListInFollow } =
40-
useFollowUser({
41-
profileId: profileId || 0,
42-
memberId: memberId || 0,
43-
isInitFollowing: !!isFollowing,
44-
followerInitCount: followingCount || 0,
45-
})
35+
const { handleClickListInFollow } = useFollowUser({
36+
profileId: profileId || 0,
37+
memberId: memberId || 0,
38+
myId: myId || 0,
39+
})
4640

4741
return (
4842
<>
@@ -70,23 +64,16 @@ const User = ({
7064
type="button"
7165
className={cls(
7266
'button px-2.5 py-1.5 text-sm',
73-
isFollowingValue ? 'button-white' : 'button-emerald',
67+
isFollowing ? 'button-white' : 'button-emerald',
7468
)}
7569
onClick={() => {
7670
if (myId) {
77-
handleClickListInFollow(isFollowingValue)
78-
if (isFollowingValue) {
79-
profileId === myId &&
80-
setFollowingCount?.((prev) => prev! - 1)
81-
} else {
82-
profileId === myId &&
83-
setFollowingCount?.((prev) => prev! + 1)
84-
}
71+
handleClickListInFollow(isFollowing)
8572
} else {
8673
handleOpenCurrentModal('login')
8774
}
8875
}}>
89-
{isFollowingValue ? PROFILE_MSG.FOLLOWING : PROFILE_MSG.FOLLOW}
76+
{isFollowing ? PROFILE_MSG.FOLLOWING : PROFILE_MSG.FOLLOW}
9077
</Button>
9178
</div>
9279
)}

0 commit comments

Comments
 (0)