Remove Friends

This commit is contained in:
2025-04-13 06:08:20 -04:00
parent 8095c71bf9
commit b505900622
3 changed files with 43 additions and 3 deletions

View File

@@ -4,9 +4,10 @@ import { useState, useEffect } from "react";
interface FriendProps { interface FriendProps {
friendCode: string; friendCode: string;
onRemove: (friendCode: string) => void;
} }
export default function Friend({ friendCode }: FriendProps) { export default function Friend({ friendCode, onRemove }: FriendProps) {
const [username, setUsername] = useState<string | null>(null); const [username, setUsername] = useState<string | null>(null);
useEffect(() => { useEffect(() => {
@@ -31,6 +32,12 @@ export default function Friend({ friendCode }: FriendProps) {
<span className="text-neutral-200"> <span className="text-neutral-200">
{username ? `${username} (${friendCode})` : "Loading..."} {username ? `${username} (${friendCode})` : "Loading..."}
</span> </span>
<button
className="px-3 py-1 bg-red-600 text-white rounded hover:bg-red-700"
onClick={() => onRemove(friendCode)}
>
Remove
</button>
</li> </li>
); );
} }

View File

@@ -138,6 +138,33 @@ function Mobile() {
alert(`Denied friend request from ${request}`); alert(`Denied friend request from ${request}`);
} }
function handleRemoveFriend(friendCode: string) {
// Remove the friend from the friends array and update the state
setFriends((prev: string[]) => prev.filter((friend) => friend !== friendCode));
const formData = new FormData();
formData.append("friends", JSON.stringify(friends.filter((friend: string) => friend !== friendCode)));
// Update the user's friends in the database
fetch(`/api/me`, {
method: "POST",
body: formData,
})
.then((res) => res.json())
.then((data) => {
if (data.message === "Friend removed successfully") {
alert("Friend removed successfully!");
} else {
alert("Failed to remove friend.");
}
})
.catch((err) => {
console.error("Error removing friend:", err);
alert("An error occurred while removing the friend.");
});
}
return ( return (
<div className="px-6 py-10 my-10 max-w-full lg:max-w-1/2 mx-auto font-sans text-neutral-100"> <div className="px-6 py-10 my-10 max-w-full lg:max-w-1/2 mx-auto font-sans text-neutral-100">
<div className="bg-[color:var(--color-surface-800)]/70 backdrop-blur-md rounded-xl px-6 py-5 my-6 shadow-sm"> <div className="bg-[color:var(--color-surface-800)]/70 backdrop-blur-md rounded-xl px-6 py-5 my-6 shadow-sm">
@@ -205,7 +232,7 @@ function Mobile() {
<ul className="list-none space-y-4"> <ul className="list-none space-y-4">
{friends.length > 0 ? ( {friends.length > 0 ? (
friends.map((friendCode: string, index: number) => ( friends.map((friendCode: string, index: number) => (
<Friend key={index} friendCode={friendCode} /> <Friend key={index} friendCode={friendCode} onRemove={handleRemoveFriend} />
)) ))
) : ( ) : (
<p className="text-neutral-400">No friends yet.</p> <p className="text-neutral-400">No friends yet.</p>

View File

@@ -71,10 +71,16 @@ export async function POST(req: Request) {
userData = await db.users.update(userData.id, { requests: JSON.parse(requests.toString()) }); userData = await db.users.update(userData.id, { requests: JSON.parse(requests.toString()) });
if (!userData) return NextResponse.json({ message: "Failed to update requests" }, { status: 500 }); if (!userData) return NextResponse.json({ message: "Failed to update requests" }, { status: 500 });
} }
let friends = formData.get("friends");
if(friends) {
userData = await db.users.update(userData.id, { friends: JSON.parse(friends.toString()) });
if (!userData) return NextResponse.json({ message: "Failed to update friends" }, { status: 500 });
}
return NextResponse.json({ message: "User updated successfully", user: userData }, { status: 200 }); return NextResponse.json({ message: "User updated successfully", user: userData }, { status: 200 });
} catch (error) { } catch (error) {
console.error("Error updating user bio:", error); console.error("Error handling user update:", error);
return NextResponse.json( return NextResponse.json(
{ message: "Internal server error" }, { message: "Internal server error" },
{ status: 500 } { status: 500 }