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 ( -
- setQuery(e.target.value)} placeholder="Search users by name" className="py-3 px-5 bg-[#121212] rounded-full w-full" /> + +
); } \ 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.name} @@ -10,7 +12,13 @@ export default function UserData({ session, user, userId }) {

{user.points} TuneStats Points

Spotify - {session?.user?.id === userId && ()} + {session?.user?.id === userId ? + ( + + ) : ( + + ) + }
) diff --git a/src/pages/api/updatePoints/index.js b/src/pages/api/updatePoints/index.js index 3c60909..16b4617 100644 --- a/src/pages/api/updatePoints/index.js +++ b/src/pages/api/updatePoints/index.js @@ -21,8 +21,6 @@ export default async function handler(req, res) { const usersSnapshot = await getDocs(usersQuery); - console.log(`Batch size fetched: ${usersSnapshot.size}`); - if (usersSnapshot.empty) break; const users = usersSnapshot.docs.map(doc => ({ diff --git a/src/pages/search/index.jsx b/src/pages/search/index.jsx index fc64859..f2396e1 100644 --- a/src/pages/search/index.jsx +++ b/src/pages/search/index.jsx @@ -1,9 +1,9 @@ -import { useState, useEffect } from 'react'; -import { useRouter } from 'next/router'; -import { db } from '@/lib/firebaseConfig'; -import { collection, getDocs } from 'firebase/firestore'; -import Navbar from '@/components/(layout)/NavBar'; -import UserSearchProfile from '@/components/(search)/UserSearchProfile'; +import { useState, useEffect } from "react"; +import { useRouter } from "next/router"; +import { db } from "@/lib/firebaseConfig"; +import { collection, getDocs } from "firebase/firestore"; +import Navbar from "@/components/(layout)/NavBar"; +import UserSearchProfile from "@/components/(search)/UserSearchProfile"; export default function Search() { const router = useRouter(); @@ -17,14 +17,18 @@ export default function Search() { const fetchUsers = async () => { setLoading(true); try { - const querySnapshot = await getDocs(collection(db, 'users')); - const usersList = querySnapshot.docs.map(doc => doc.data()); + const usersRef = collection(db, "users"); + const querySnapshot = await getDocs(usersRef); - const filteredUsers = usersList.filter(user => user.name.toLowerCase().includes(searchQuery.toLowerCase())); + const filteredUsers = querySnapshot.docs + .map((doc) => doc.data()) + .filter((user) => + user.name.toLowerCase().includes(searchQuery.toLowerCase()) + ); setUsers(filteredUsers); } catch (error) { - console.error('Error fetching users:', error); + console.error("Error fetching users:", error); } finally { setLoading(false); } @@ -33,15 +37,6 @@ export default function Search() { fetchUsers(); }, [searchQuery]); - if (!searchQuery) { - return ( -
- -

Please enter a search query.

-
- ); - } - return (
@@ -49,16 +44,12 @@ export default function Search() {

Search Results

{loading ? (

Loading...

+ ) : users.length > 0 ? ( +
+ {users.map((user) => ())} +
) : ( - users.length > 0 ? ( -
- {users.map((user) => ( - - ))} -
- ) : ( -

No users found

- ) +

No users found

)}