Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚨Fix : profile/follow button 에러수정 #80

Merged
merged 4 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 'bearer ' 부분을 api단이 아닌 UI레이어에서 붙여서 보내주는데 통일해야겠네요?! 어느쪽이 더 좋을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api단이 더 좋을 것 같아요! 아니면 세션에 저장할 때 bearer을 붙이고 저장하는건 어떨까요?

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
Comment on lines +9 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postFollowUser -> followUser
deleteFollowUser -> unfollowUser
followingUserId -> 현상유지
followingDataId -> unfollowingUserId

개인적으로는 이게 더 직관적으로 보여서 제안드렸는데 어떻게 생각하시나용

Copy link
Collaborator Author

@nayeon-hub nayeon-hub Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네트워크 요청 함수는 앞에 메소드를 붙이는 컨벤션이 있어서 deleteFollowUser, postFollowUser는 유지해야 할 것 같아요! ㅠ

followingDataId는 userId가 아닌 진짜 following 데이터 아이디랍니다.. ㅠㅠ

following?: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default 값을 부여하고 있어서 옵셔널로 처리하지 않으셔도 괜찮지 않을까요?

}

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;
Comment on lines 21 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const [userSessionData] = useSessionStorage<Pick<User, 'token'>>(
    'userData',
    {
      token: ''
    }
  );

세션에서 토큰만 가져온다면 이렇게 작성해주시면 됩니다!


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: '명상정보'
};