-
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: enhance search functionality and user profile display; add Frie…
…ndButton component
- Loading branch information
Showing
5 changed files
with
107 additions
and
40 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 |
---|---|---|
@@ -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 ( | ||
<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 className="w-11/12 md:w-3/4 lg:w-1/2"> | ||
<input type="text" value={query} onChange={handleInputChange} 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,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 ( | ||
<button onClick={isFriend ? handleRemoveFriend : handleAddFriend} disabled={loading} className={`px-4 py-2 bg-[#121212] border-[2px] border-[#333] rounded-md`}> | ||
{loading ? "Processing..." : isFriend ? "Remove Friend" : "Add Friend"} | ||
</button> | ||
); | ||
} |
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