-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add search functionality and user profile display; update UI co…
…mponents
- Loading branch information
Showing
9 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import SearchBar from "@/components/(layout)/SearchBar"; | ||
|
||
export default function Navbar() { | ||
return ( | ||
<nav className="bg-black p-3 flex item-center justify-center w-full"> | ||
<SearchBar /> | ||
</nav> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useState } from 'react'; | ||
import { useRouter } from 'next/router'; | ||
|
||
export default function SearchBar() { | ||
const [query, setQuery] = useState(''); | ||
const router = useRouter(); | ||
|
||
const handleSearch = (e) => { | ||
e.preventDefault(); | ||
if (query.trim()) { router.push(`/search?query=${query}`); } | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSearch} className="w-11/12 md:w-3/4 lg:w-1/2"> | ||
<input type="text" value={query} onChange={(e) => setQuery(e.target.value)} placeholder="Search users by name" className="py-3 px-5 bg-[#121212] rounded-full w-full" /> | ||
</form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* eslint-disable @next/next/no-img-element */ | ||
export default function UserSearchProfile({ user }) { | ||
return ( | ||
<div className="flex flex-col items-center bg-[#121212] p-3 rounded-lg gap-2"> | ||
<img src={user.image === 'unknown' ? 'https://github.com/user-attachments/assets/bf57cb96-b259-4290-b35b-0ede9d618802' : user.image} alt={user.name} className="w-32 h-32 rounded-full" /> | ||
<h3 className="text-xl font-bold">{user.name}</h3> | ||
<a href={`/user/${user.spotifyId}`} className="px-4 py-2 bg-[#121212] border-[2px] border-[#333] rounded-md">View Profile</a> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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(); | ||
const { query: searchQuery } = router.query; | ||
const [users, setUsers] = useState([]); | ||
const [loading, setLoading] = useState(true); | ||
|
||
useEffect(() => { | ||
if (!searchQuery) return; | ||
|
||
const fetchUsers = async () => { | ||
setLoading(true); | ||
try { | ||
const querySnapshot = await getDocs(collection(db, 'users')); | ||
const usersList = querySnapshot.docs.map(doc => doc.data()); | ||
|
||
const filteredUsers = usersList.filter(user => user.name.toLowerCase().includes(searchQuery.toLowerCase())); | ||
|
||
setUsers(filteredUsers); | ||
} catch (error) { | ||
console.error('Error fetching users:', error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
fetchUsers(); | ||
}, [searchQuery]); | ||
|
||
if (!searchQuery) { | ||
return ( | ||
<div className="w-full h-screen"> | ||
<Navbar /> | ||
<h2 className="text-2xl text-center mt-10">Please enter a search query.</h2> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div className="w-full min-h-screen"> | ||
<Navbar /> | ||
<div className="w-full p-4"> | ||
<h2 className="text-2xl font-bold mb-6">Search Results</h2> | ||
{loading ? ( | ||
<p>Loading...</p> | ||
) : ( | ||
users.length > 0 ? ( | ||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3"> | ||
{users.map((user) => ( | ||
<UserSearchProfile key={user.spotifyId} user={user} /> | ||
))} | ||
</div> | ||
) : ( | ||
<p>No users found</p> | ||
) | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |