친구 삭제 수정
저번에 했던 친구 삭제 방식 (친구 요청 하면 uuid로 저장되고 상대방이 수락할때 그 uuid +1로 저장하고 친구 삭제할땐 uuid, uuid+1, uuid.slice모두 삭제하는 방식)은 비효율적인거 같다고 생각되어 수정하게 됐다.
우선 친구 요청과 친구 수락모두 각각의 uuid로 저장되게 했다.
//친구 수락
const friendAddOnClick = async (i: FriendSearchProps) => {
let friendAdd = {
id: uuidv4(),
myId: myId,
friendId: i.id,
myNickName: myNickName,
friendNickName: i.nickname,
};
try {
//상대와 친구가 돼있는지 검사후 이중 저장 방지
const response = await axios.get(
`http://localhost:3001/friend?myId=${myId}&friendId=${i.id}`
);
const existingFriend = response.data[0];
console.log(existingFriend);
if (existingFriend) {
console.log("이미 친구");
return;
}
postMutation.mutate(friendAdd);
} catch (error) {
console.error(error);
}
};
//찾아온 친구 id 를이용해 두개다 삭제
const friendDeleteOnClick = (id: any) => {
const friendDelete = getFriendAuth.filter((i) => {
return id === i.friendId || id === i.myId;
});
DeleteMutation.mutate(friendDelete[0].id);
};
친구 삭제를 할때는 요청했던 정보, 수락했던 두개를 모두 한번에 삭제해야 했다.
그래서 친구 삭제 mutation을 만든 후 삭제할때 친구db를 가져온 후 filter를 이용해 친구 정보 두개의 id를 모두 찾아 삭제해 줬다.
// 친구 삭제
const DeleteMutation = useMutation(
//넘겨받은 id를 삭제
(id) => axios.delete(`http://localhost:3001/friend/${id}`),
{
onSuccess: () => {
// 쿼리 무효화
queryClient.invalidateQueries("friend");
queryClient.invalidateQueries("friendsearch");
},
}
);
//찾아온 친구 id 를이용해 두개다 삭제
const friendDeleteOnClick = (id: any) => {
const friendDelete = getFriendAuth.filter((i) => {
return id === i.friendId || id === i.myId;
});
DeleteMutation.mutate(friendDelete[0].id);
DeleteMutation.mutate(friendDelete[1].id);
};