Skip to content

Commit

Permalink
fix: friends search and prevent sending request to urself
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaqiWang18 committed May 16, 2024
1 parent 80f41ba commit 91d06b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 9 additions & 0 deletions friends/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ def send_friend_request(request, userId):
"""
from_student = get_object_or_404(Student, user=request.user)
to_student = get_object_or_404(Student, user__id=userId)

# check if there's already a request from to_student to from_student
if FriendRequest.objects.filter(
from_friend=to_student, to_friend=from_student
).exists():
return JsonResponse(
{"message": "The friend already has a request to you"}, status=400
)

friend_request, created = FriendRequest.objects.get_or_create(
from_friend=from_student, to_friend=to_student
)
Expand Down
12 changes: 10 additions & 2 deletions static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const FindNewFriends = () => {
const endpoint = requestSent[userId]
? getRejectFriendRequestEndpoint(userId)
: getSendFriendRequestEndpoint(userId);
await fetch(endpoint, {
const res = await fetch(endpoint, {
headers: {
"X-CSRFToken": Cookie.get("csrftoken"),
Accept: "application/json",
Expand All @@ -74,7 +74,15 @@ const FindNewFriends = () => {
method: "POST",
credentials: "include",
});
setRequestSent((prevStatus) => ({ ...prevStatus, [userId]: !prevStatus[userId] }));
const json = await res.json();
if (res.status === 200) {
setRequestSent((prevStatus) => ({
...prevStatus,
[userId]: !prevStatus[userId],
}));
} else {

Check warning on line 83 in static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx

View workflow job for this annotation

GitHub Actions / build (3.8, 14.x)

Unexpected console statement

Check warning on line 83 in static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
console.error(json);
}
};
return (
<Box width="100%" display="flex" flexDirection="column" alignItems="center">
Expand Down

0 comments on commit 91d06b8

Please sign in to comment.