In this episode, we jump back into the profile page and work on making the friend logic much smarter. Up to now, the "Add as a friend" button didn't always reflect the actual friendship status between users, so we start cleaning that up.
First, we add additional checks to determine whether two users are already friends, if there's a pending friend request, or if a friend request has been received by one user from the other. This way, the profile page can correctly show the appropriate action: whether to add as a friend, accept a friend request, or indicate that you are already friends (with the option to unfriend them).
To make this work, we implement an isFriendsWith
method, which lets us check if two users are friends. We also prevent duplicate friend requests and ensure users can't add each other multiple times. Now, if a user has a pending friend request from someone, they’ll see the option to accept or reject it directly from the profile page.
We walk through updating all of the conditional logic and forms to support accepting, rejecting, unfriending, and sending friend requests—all from the profile page, making everything feel much more seamless and intuitive.