diff --git a/src/components/(layout)/SearchBar.jsx b/src/components/(layout)/SearchBar.jsx index 6f65dd5..80aa900 100644 --- a/src/components/(layout)/SearchBar.jsx +++ b/src/components/(layout)/SearchBar.jsx @@ -1,18 +1,24 @@ -import { useState } from 'react'; -import { useRouter } from 'next/router'; +import { useState } from "react"; +import { useRouter } from "next/router"; export default function SearchBar() { - const [query, setQuery] = useState(''); + const [query, setQuery] = useState(""); const router = useRouter(); - const handleSearch = (e) => { - e.preventDefault(); - if (query.trim()) { router.push(`/search?query=${query}`); } + const handleInputChange = (e) => { + const value = e.target.value; + setQuery(value); + + if (value.trim()) { + router.push(`/search?query=${encodeURIComponent(value)}`, undefined, { shallow: true }); + } else { + router.push(`/search`, undefined, { shallow: true }); + } }; return ( -
); } \ No newline at end of file diff --git a/src/components/(user)/FriendButton.jsx b/src/components/(user)/FriendButton.jsx new file mode 100644 index 0000000..5507611 --- /dev/null +++ b/src/components/(user)/FriendButton.jsx @@ -0,0 +1,64 @@ +import { useState, useEffect } from "react"; +import { doc, updateDoc, arrayUnion, arrayRemove, getDoc } from "firebase/firestore"; +import { db } from "@/lib/firebaseConfig"; + +export default function FriendButton({ currentUserId, userId }) { + const [isFriend, setIsFriend] = useState(false); + const [loading, setLoading] = useState(false); + + useEffect(() => { + const checkFriendshipStatus = async () => { + try { + const currentUserRef = doc(db, "users", currentUserId); + const currentUserDoc = await getDoc(currentUserRef); + + if (currentUserDoc.exists()) { + const currentUserData = currentUserDoc.data(); + setIsFriend(currentUserData.friends?.includes(userId) || false); + } + } catch (error) { + console.error("Error checking friendship status:", error); + } + }; + + checkFriendshipStatus(); + }, [currentUserId, userId]); + + const handleAddFriend = async () => { + setLoading(true); + try { + const currentUserRef = doc(db, "users", currentUserId); + await updateDoc(currentUserRef, { + friends: arrayUnion(userId), + }); + setIsFriend(true); + } catch (error) { + console.error("Error adding friend:", error); + } finally { + setLoading(false); + } + }; + + const handleRemoveFriend = async () => { + setLoading(true); + try { + const currentUserRef = doc(db, "users", currentUserId); + await updateDoc(currentUserRef, { + friends: arrayRemove(userId), + }); + setIsFriend(false); + } catch (error) { + console.error("Error removing friend:", error); + } finally { + setLoading(false); + } + }; + + if (currentUserId === userId) return null; + + return ( + + ); +} \ No newline at end of file diff --git a/src/components/(user)/UserData.jsx b/src/components/(user)/UserData.jsx index 22df5c8..ba8fc30 100644 --- a/src/components/(user)/UserData.jsx +++ b/src/components/(user)/UserData.jsx @@ -1,8 +1,10 @@ /* eslint-disable @next/next/no-img-element */ import { signOut } from "next-auth/react"; +import FriendButton from "@/components/(user)/FriendButton"; export default function UserData({ session, user, userId }) { - console.log(user) + const currentUserId = session?.user?.id; + return ({user.points} TuneStats Points
Loading...
+ ) : users.length > 0 ? ( +No users found
- ) +No users found
)}