Skip to content

Commit

Permalink
๐ŸšจFix : profile/follow button ์—๋Ÿฌ์ˆ˜์ •
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeon-hub authored Sep 23, 2023
2 parents 7174a7c + ea81301 commit d2095db
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/apis/follow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const postFollowUser = async (userId: string, token: string) => {
{ userId },
{
headers: {
Authorization: token
Authorization: `bearer ${token}`
}
}
);
Expand All @@ -19,7 +19,7 @@ const deleteFollowUser = async (userId: string, token: string) => {
const response = await axios.delete<Follow>(`${API_BASE_URL}/follow/delete`, {
data: { id: userId },
headers: {
Authorization: token
Authorization: `bearer ${token}`
}
});
return response.data;
Expand Down
46 changes: 28 additions & 18 deletions src/pages/profile/components/FollowButton.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
import { useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { useMutation } from '@tanstack/react-query';
import useSessionStorage from '@hooks/useSessionStorage';
import { deleteFollowUser, postFollowUser } from '@apis/follow';
import { Button } from '@components/Button';
import { User } from '@/types';

interface FollowButtonProps {
followDataId: string;
followingDataId: string; // ์‚ญ์ œ์šฉ - following data id
followingUserId: string; // ํŒ”๋กœ์šฐ์šฉ - ํŒ”๋กœ์šฐํ•  userId
following?: boolean;
}

const FollowButton = ({ followDataId }: FollowButtonProps) => {
const [followed, setFollowed] = useState(true);
const FollowButton = ({
followingDataId,
followingUserId,
following = true
}: FollowButtonProps) => {
const [followed, setFollowed] = useState(following);
const [dataId, setDataId] = useState(followingDataId);
const [userSessionData] = useSessionStorage<Pick<User, '_id' | 'token'>>(
'userData',
{
_id: '',
token: ''
}
);
const { token, _id } = userSessionData;
const { token } = userSessionData;

const { refetch } = useQuery({
queryKey: ['follow'],
queryFn: async () => {
const data = followed
? await deleteFollowUser(followDataId, token)
: await postFollowUser(_id, token);
const { mutate } = useMutation(
() =>
followed
? deleteFollowUser(dataId, token)
: postFollowUser(followingUserId, token),
{
onSuccess: (data) => {
if (!followed) {
setDataId(data._id);
}

return data;
},
enabled: false
});
setFollowed((prev) => !prev);
}
}
);

const handleClickFollow = async () => {
await refetch();
setFollowed((prev) => !prev);
const handleClickFollow = () => {
mutate();
};

return (
Expand Down
11 changes: 9 additions & 2 deletions src/pages/profile/components/FollowUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const FollowUser = ({
followUser,
following
}: FollowUserProps) => {
const { fullName, image, isOnline, email } = followUser;
const { _id, fullName, image, isOnline, email } = followUser;

return (
<FollowUserContainer>
Expand All @@ -22,8 +22,15 @@ const FollowUser = ({
image={image}
isOnline={isOnline}
email={email}
id={_id}
/>
{following && <FollowButton followDataId={followDataId} />}
{following && (
<FollowButton
followingDataId={followDataId}
followingUserId={_id}
following={true}
/>
)}
</FollowUserContainer>
);
};
Expand Down
11 changes: 9 additions & 2 deletions src/pages/profile/components/FollowUserInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { FollowUserInfoContainer } from './FollowUserInfo.style';
import { UserId, UserName } from '@components/UserText';
import { BadgeAvatar } from '@components/Avatar';
import { useNavigate } from 'react-router-dom';

interface FollowUserInfoProps {
fullName: string;
image?: string;
email: string;
isOnline: boolean;
id: string;
}

const FollowUserInfo = ({
fullName,
isOnline,
email,
image
image,
id
}: FollowUserInfoProps) => {
const navigate = useNavigate();

const handleClickInfo = () => navigate(`/profile/${id}`);

return (
<FollowUserInfoContainer>
<FollowUserInfoContainer onClick={handleClickInfo}>
<BadgeAvatar
alt={fullName}
src={image}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/profile/constants/profileTabs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const PROFILE_TABS = {
MEDITATION: '๋ช…์ƒ',
FOLLOWING: 'ํŒ”๋กœ์›Œ',
FOLLOWER: 'ํŒ”๋กœ์ž‰',
FOLLOWING: 'ํŒ”๋กœ์ž‰',
FOLLOWER: 'ํŒ”๋กœ์›Œ',
INFO: '๋ช…์ƒ์ •๋ณด'
};

0 comments on commit d2095db

Please sign in to comment.