diff --git a/friends/views.py b/friends/views.py index 13826779f..096fc393b 100644 --- a/friends/views.py +++ b/friends/views.py @@ -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 ) diff --git a/static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx b/static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx index cfe2e5404..5d7e55495 100644 --- a/static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx +++ b/static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx @@ -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", @@ -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 { + console.error(json); + } }; return (