Skip to content

Commit

Permalink
Merge pull request #98 from shubhagarwal1/loader
Browse files Browse the repository at this point in the history
ADD: Loader to the send request button (Add Friend)
  • Loading branch information
AshuKr22 authored Oct 9, 2024
2 parents 68ef324 + f802461 commit bb12538
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion client/src/pages/Friends/Friends.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const FriendsSearch: React.FC = () => {
const [friendToRemove, setFriendToRemove] = useState("")
const { user } = useAuth()
const navigate = useNavigate()
const [loading, setLoading] = useState(false)
const [sent, setSent] = useState(false)

const {
useGetFriends,
Expand Down Expand Up @@ -64,8 +66,11 @@ const FriendsSearch: React.FC = () => {
}

const handleSendRequest = (friendUserName: string) => {
if (sent) return; // Prevent sending duplicate requests
setLoading(true); // Show loading spinner while sending
sendFriendRequestMutation.mutate(friendUserName, {
onSuccess: () => {
setSent(true); // Mark as "Sent"
toast.success("Friend request sent successfully.")
},
onError: (error) => {
Expand All @@ -78,6 +83,9 @@ const FriendsSearch: React.FC = () => {

toast.error("Failed to send friend request. Please try again.")
},
onSettled: () => {
setLoading(false); // Hide loader after request
}
})
}

Expand Down Expand Up @@ -359,11 +367,13 @@ const FriendsSearch: React.FC = () => {
</div>
<Button
onClick={() => handleSendRequest(user.userName)}
disabled={loading || sent}
size="sm"
className="p-2 sm:p-3 md:p-4 lg:p-5 rounded-md"
aria-label="Send Friend Request"
>
<UserPlus className="w-4 h-4 sm:w-5 sm:h-5 md:w-6 md:h-6 lg:w-7 lg:h-7" />
{/* <UserPlus className="w-4 h-4 sm:w-5 sm:h-5 md:w-6 md:h-6 lg:w-7 lg:h-7" /> */}
{loading ? "Sending..." : (sent ? "Sent" : "Send Request")}
</Button>
</motion.li>
))}
Expand Down

0 comments on commit bb12538

Please sign in to comment.