diff --git a/thoughts-frontend/components/edit-profile-form.tsx b/thoughts-frontend/components/edit-profile-form.tsx index 5c17ca0..13c90cf 100644 --- a/thoughts-frontend/components/edit-profile-form.tsx +++ b/thoughts-frontend/components/edit-profile-form.tsx @@ -3,9 +3,8 @@ import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; -import { useRouter } from "next/navigation"; -import { useAuth } from "@/hooks/use-auth"; -import { Me, UpdateProfileSchema, updateProfile } from "@/lib/api"; +import { Me, UpdateProfileSchema } from "@/lib/api"; +import { updateProfile } from "@/app/actions/profile"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardFooter } from "@/components/ui/card"; @@ -25,8 +24,6 @@ interface EditProfileFormProps { } export function EditProfileForm({ currentUser }: EditProfileFormProps) { - const router = useRouter(); - const { token } = useAuth(); const form = useForm>({ resolver: zodResolver(UpdateProfileSchema), @@ -40,13 +37,10 @@ export function EditProfileForm({ currentUser }: EditProfileFormProps) { }); async function onSubmit(values: z.infer) { - if (!token) return; toast.info("Updating your profile..."); try { - await updateProfile(values, token); + await updateProfile(currentUser.username, values); toast.success("Profile updated successfully!"); - router.push(`/users/${currentUser.username}`); - router.refresh(); } catch (err) { toast.error(`Failed to update profile. ${err}`); } diff --git a/thoughts-frontend/components/follow-button.tsx b/thoughts-frontend/components/follow-button.tsx index 0462601..494292c 100644 --- a/thoughts-frontend/components/follow-button.tsx +++ b/thoughts-frontend/components/follow-button.tsx @@ -1,66 +1,41 @@ -"use client"; +"use client" -import { useState } from "react"; -import { useRouter } from "next/navigation"; -import { useAuth } from "@/hooks/use-auth"; -import { followUser, unfollowUser } from "@/lib/api"; -import { Button } from "@/components/ui/button"; -import { toast } from "sonner"; -import { UserPlus, UserMinus } from "lucide-react"; +import { useOptimistic } from "react" +import { followUser, unfollowUser } from "@/app/actions/social" +import { Button } from "@/components/ui/button" +import { toast } from "sonner" +import { UserPlus, UserMinus } from "lucide-react" interface FollowButtonProps { - username: string; - isInitiallyFollowing: boolean; + username: string + isInitiallyFollowing: boolean } -export function FollowButton({ - username, - isInitiallyFollowing, -}: FollowButtonProps) { - const [isFollowing, setIsFollowing] = useState(isInitiallyFollowing); - const [isLoading, setIsLoading] = useState(false); - const { token } = useAuth(); - const router = useRouter(); - - const handleClick = async () => { - if (!token) { - toast.error("You must be logged in to follow users."); - return; - } - - setIsLoading(true); - const action = isFollowing ? unfollowUser : followUser; +export function FollowButton({ username, isInitiallyFollowing }: FollowButtonProps) { + const [optimisticFollowing, setOptimisticFollowing] = useOptimistic(isInitiallyFollowing) + async function handleClick() { + const next = !optimisticFollowing + setOptimisticFollowing(next) try { - // Optimistic update - setIsFollowing(!isFollowing); - await action(username, token); - router.refresh(); // Re-fetch server component data to get the latest follower count etc. + await (next ? followUser(username) : unfollowUser(username)) } catch { - // Revert on error - setIsFollowing(isFollowing); - toast.error(`Failed to ${isFollowing ? "unfollow" : "follow"} user.`); - } finally { - setIsLoading(false); + setOptimisticFollowing(!next) // revert + toast.error(`Failed to ${next ? "follow" : "unfollow"} user.`) } - }; + } return ( - ); + ) }