Skip to content

Commit

Permalink
Finish friends and friend requests
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-petrov1 committed Apr 21, 2024
1 parent 098f0f5 commit 85a66ca
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 18 deletions.
2 changes: 2 additions & 0 deletions client/src/config/api/friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const base = `${baseApiUrl}/userFriend`;

export const friendsPaths = Object.seal({
getById: (userId: number) => `${base}/list/${userId}`,
hasSentRequest: (userId: number) => `${base}/has-sent-request/${userId}`,
add: (friendId: number) => `${base}/add/${friendId}`,
requestsById: (userId: number) => `${base}/requests/${userId}`,
confirmRequest: (friendId: number) => `${base}/confirm/${friendId}`,
removeRequest: (friendId: number) => `${base}/remove/${friendId}`,
removeFriendship: (friendId: number) => `${base}/${friendId}`,
});
147 changes: 140 additions & 7 deletions client/src/pages/profile/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ function Profile() {

const userId = Number(idParam || -1);

const {
get: getHasSentFriendRequest,
data: hasSentFriendRequest,
loading: loadingHasSentFriendRequest,
} = useFetch<boolean>(friendsPaths.hasSentRequest(userId), [userId]);

const { data: user } = useFetch<IFullUser>(usersPaths.getById(userId), [
userId,
]);
Expand All @@ -30,6 +36,8 @@ function Profile() {
friendsPaths.add(user?.id || -1)
);

const { del } = useFetch<void>(friendsPaths.removeFriendship(userId));

const refreshFriends = async () => {
await getFriends();
};
Expand All @@ -44,14 +52,29 @@ function Profile() {
}

await postFriendReq();
await getHasSentFriendRequest();
};

const handleRemoveFriendRequest = async () => {
await del();
await getHasSentFriendRequest();
};

if (!user || !friends) {
if (!user || !friends || loadingHasSentFriendRequest) {
return <Spinner />;
}

const isOwner = userMe.id === user.id;
const mappedFriends = friends.map((x) => (isOwner ? x.friend : x.user));
const mappedFriends = friends.map((x) => {
const isUser = x.user.id === user.id;

if (isUser) {
return x.friend;
}

return x.user;
});
const areFriends = !isOwner && mappedFriends.some((x) => x.id === userMe.id);

return (
<div className="mt-5 px-5">
Expand All @@ -73,16 +96,126 @@ function Profile() {
<p className="text-muted font-size-sm">{user.address}</p>
{!isOwner && (
<div className="d-flex justify-content-center align-items-center gap-2">
<button
className="btn btn-outline-primary"
onClick={handleAddFriend}>
Add Friend
</button>
{areFriends ? null : hasSentFriendRequest ? (
<button
className="btn btn-outline-primary"
onClick={handleRemoveFriendRequest}>
Decline Request
</button>
) : (
<button
className="btn btn-outline-primary"
onClick={handleAddFriend}>
Add Friend
</button>
)}
<button className="btn btn-outline-primary">Message</button>
</div>
)}
</div>
</div>
<hr className="my-4" />
<ul className="list-group list-group-flush">
<li className="list-group-item d-flex justify-content-between align-items-center flex-wrap">
<h6 className="mb-0">
<svg
xmlns="http://www.w3.org/2000/svg"
width={24}
height={24}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
className="feather feather-globe me-2 icon-inline">
<circle cx={12} cy={12} r={10} />
<line x1={2} y1={12} x2={22} y2={12} />
<path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z" />
</svg>
Website
</h6>
<span className="text-secondary">https://bootdey.com</span>
</li>
<li className="list-group-item d-flex justify-content-between align-items-center flex-wrap">
<h6 className="mb-0">
<svg
xmlns="http://www.w3.org/2000/svg"
width={24}
height={24}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
className="feather feather-github me-2 icon-inline">
<path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22" />
</svg>
Github
</h6>
<span className="text-secondary">bootdey</span>
</li>
<li className="list-group-item d-flex justify-content-between align-items-center flex-wrap">
<h6 className="mb-0">
<svg
xmlns="http://www.w3.org/2000/svg"
width={24}
height={24}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
className="feather feather-twitter me-2 icon-inline text-info">
<path d="M23 3a10.9 10.9 0 0 1-3.14 1.53 4.48 4.48 0 0 0-7.86 3v1A10.66 10.66 0 0 1 3 4s-4 9 5 13a11.64 11.64 0 0 1-7 2c9 5 20 0 20-11.5a4.5 4.5 0 0 0-.08-.83A7.72 7.72 0 0 0 23 3z" />
</svg>
Twitter
</h6>
<span className="text-secondary">@bootdey</span>
</li>
<li className="list-group-item d-flex justify-content-between align-items-center flex-wrap">
<h6 className="mb-0">
<svg
xmlns="http://www.w3.org/2000/svg"
width={24}
height={24}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
className="feather feather-instagram me-2 icon-inline text-danger">
<rect x={2} y={2} width={20} height={20} rx={5} ry={5} />
<path d="M16 11.37A4 4 0 1 1 12.63 8 4 4 0 0 1 16 11.37z" />
<line x1="17.5" y1="6.5" x2="17.51" y2="6.5" />
</svg>
Instagram
</h6>
<span className="text-secondary">bootdey</span>
</li>
<li className="list-group-item d-flex justify-content-between align-items-center flex-wrap">
<h6 className="mb-0">
<svg
xmlns="http://www.w3.org/2000/svg"
width={24}
height={24}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
className="feather feather-facebook me-2 icon-inline text-primary">
<path d="M18 2h-3a5 5 0 0 0-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 0 1 1-1h3z" />
</svg>
Facebook
</h6>
<span className="text-secondary">bootdey</span>
</li>
</ul>
</div>
</div>
<div className="card" style={{ maxWidth: '63%' }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function ProfileFriendRequests({
<div className="card d-flex row">
{friendRequests?.map((x) => (
<ProfileFriendRequest
requester={x.friend}
requester={x.user}
refresh={refresh}
key={x.id}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function ProfileFriendRequest({
height="40"
/>
<div>
<Link to={PageEnum.Home} className="fw-semibold mb-0">
<Link
to={PageEnum.Profile.replace(':userId', requester.id.toString())}
className="fw-semibold mb-0">
<h5>
{requester.firstname} {requester.lastname}
</h5>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface UserFriendRepository extends JpaRepository<UserFriend, Long> {
Optional<UserFriend> findByUserIdAndFriendId(Long userId, Long friendId);

List<UserFriend> findByUserIdAndIsConfirmedIsTrue(Long userId);
List<UserFriend> findByUserIdAndIsConfirmedIsFalse(Long userId);
List<UserFriend> findByFriendIdAndIsConfirmedIsFalse(Long userId);

List<UserFriend> findAllByUser_IdAndIsConfirmedTrueOrFriend_IdAndIsConfirmedTrue(Long userId, Long friendId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,11 @@ public void confirmFriendRequest(Long loggedUserId, Long friendId) {
User user = userRepository.findById(loggedUserId).orElseThrow(UserNotFoundException::new);
User friend = userRepository.findById(friendId).orElseThrow(UserNotFoundException::new);

UserFriend userFriend = userFriendRepository.findByUserIdAndFriendId(user.getId(), friend.getId())
UserFriend userFriend = userFriendRepository.findByUserIdAndFriendId(friend.getId(), user.getId())
.orElseThrow(UserFriendNotFoundException::new);

if (userFriend.getFriend().getId() == friend.getId()) {
userFriend.setConfirmed(true);
userFriendRepository.save(userFriend);
} else {
throw new CannotConfirmFriendshipException();
}
userFriend.setConfirmed(true);
userFriendRepository.save(userFriend);
}

@Override
Expand All @@ -77,7 +73,7 @@ public void removeFriend(PublicUserDTO loggedUser, Long friend) {
@Override
public List<UserFriend> getFriendRequestsById(Long id) {
User user = userRepository.findById(id).orElseThrow(UserNotFoundException::new);
return userFriendRepository.findByUserIdAndIsConfirmedIsFalse(user.getId());
return userFriendRepository.findByFriendIdAndIsConfirmedIsFalse(user.getId());
}
@Override
public List<UserFriend> getFriendsForUser(Long id) {
Expand Down

0 comments on commit 85a66ca

Please sign in to comment.