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 : 로그인 직후 친구 목록 반영 안 되는 문제 + 즐겨찾기 시 즐겨찾기 목록, 친구 목록 중복 현상 #40

Merged
merged 1 commit into from
Jun 2, 2024
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
9 changes: 2 additions & 7 deletions src/apis/getFriendList.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { customedAxios } from './customedAxios';
import api from '../service/TokenService';

const accessToken = api.getAccessToken();

export const getFriendList = async (data: FriendListReq): Promise<FriendListRes> => {
export const getFriendList = async (): Promise<FriendListRes> => {
const res = await customedAxios.get('/members/friends', {
params: {
page: data.page,
},
headers: {
Authorization: `Bearer ${accessToken}`,
Authorization: `Bearer ${api.getAccessToken()}`,
},
});
return res.data;
Expand Down
6 changes: 2 additions & 4 deletions src/apis/postLike.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { customedAxios } from './customedAxios';
import api from '../service/TokenService';

const accessToken = api.getAccessToken();

export const postLike = async (data: PostLikeReq): Promise<PostLikeRes> => {
const res = await customedAxios.post(
`/members/friends/${data.friendId}/likes`,
Expand All @@ -12,9 +10,9 @@ export const postLike = async (data: PostLikeReq): Promise<PostLikeRes> => {
isLiked: data.isLiked,
},
headers: {
Authorization: `Bearer ${accessToken}`,
Authorization: `Bearer ${api.getAccessToken()}`,
},
}
);
return res.data;
return { status: res.status };
};
1 change: 1 addition & 0 deletions src/components/Friend/UserFriend/UserFriend.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const FriendContainer = styled.div`
display: flex;
flex-direction: column;
overflow-y: scroll;
cursor: pointer;

// 스크롤 바 안 생기게
-ms-overflow-style: none;
Expand Down
24 changes: 15 additions & 9 deletions src/components/Friend/UserFriend/UserFriend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { postLike } from '../../../apis/postLike';
const UserFriend = () => {
const [friendList, setFriendList] = useState<Friend[]>([]);
const [favoriteFriendList, setFavoriteFriendList] = useState<Friend[]>([]);
const currentPage = 0; // <------------------ 페이징 처리 위한 임시 변수
const [trigger, setTrigger] = useState(false); // 트리거 상태 추가

const onLikeToggle = async (friendId: number, isLiked: boolean) => {
Expand All @@ -20,28 +19,31 @@ const UserFriend = () => {
};

try {
await postLike(params);
setTrigger((prevTrigger) => !prevTrigger);
const response = await postLike(params);
if (response.status === 200) {
setTrigger((prevTrigger) => !prevTrigger);
} else {
console.error('postLike 요청이 실패했습니다.');
}
} catch (error) {
console.error('즐겨찾기 실패:', error);
console.error('postLike 요청이 실패했습니다:', error);
}
};

useEffect(() => {
const fetchFriendList = async () => {
try {
const params = { page: currentPage };
const data = await getFriendList(params);
const data = await getFriendList();
setFriendList(data.friends);
const favoriteFriends = data.friends.filter((friend: Friend) => friend.isStarred);
setFavoriteFriendList(favoriteFriends); // 기존 목록에 새로운 목록 추가
setFavoriteFriendList(favoriteFriends);
} catch (error) {
console.error('친구 목록 가져오기 실패:', error);
}
};

fetchFriendList();
}, [currentPage, trigger]);
}, [trigger]);

return (
<FriendContainer>
Expand All @@ -50,7 +52,11 @@ const UserFriend = () => {
friends={favoriteFriendList}
onLikeToggle={onLikeToggle}
/>
<FriendList sum={friendList.length} friends={friendList} onLikeToggle={onLikeToggle} />
<FriendList
sum={friendList.length - favoriteFriendList.length}
friends={friendList.filter((friend) => !favoriteFriendList.includes(friend))}
onLikeToggle={onLikeToggle}
/>
</FriendContainer>
);
};
Expand Down
5 changes: 0 additions & 5 deletions src/types/Friend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ interface Friend {
onLikeToggle: (friendId: number, isLiked: boolean) => void;
}

interface FriendListReq {
page: number;
}

interface PostLikeReq {
friendId: number;
isLiked: boolean;
}
interface PostLikeRes {
status: number;
message: string;
}

interface FriendDetail {
Expand Down