From b505900622188df00e781f11179d2a635abc29d1 Mon Sep 17 00:00:00 2001 From: GamerBoss101 Date: Sun, 13 Apr 2025 06:08:20 -0400 Subject: [PATCH] Remove Friends --- src/app/(app)/profile/Friend.tsx | 9 ++++++++- src/app/(app)/profile/page.tsx | 29 ++++++++++++++++++++++++++++- src/app/api/me/route.ts | 8 +++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/app/(app)/profile/Friend.tsx b/src/app/(app)/profile/Friend.tsx index 07dbcb1..53c7f13 100644 --- a/src/app/(app)/profile/Friend.tsx +++ b/src/app/(app)/profile/Friend.tsx @@ -4,9 +4,10 @@ import { useState, useEffect } from "react"; interface FriendProps { friendCode: string; + onRemove: (friendCode: string) => void; } -export default function Friend({ friendCode }: FriendProps) { +export default function Friend({ friendCode, onRemove }: FriendProps) { const [username, setUsername] = useState(null); useEffect(() => { @@ -31,6 +32,12 @@ export default function Friend({ friendCode }: FriendProps) { {username ? `${username} (${friendCode})` : "Loading..."} + ); } \ No newline at end of file diff --git a/src/app/(app)/profile/page.tsx b/src/app/(app)/profile/page.tsx index 007169b..1a92bd3 100644 --- a/src/app/(app)/profile/page.tsx +++ b/src/app/(app)/profile/page.tsx @@ -138,6 +138,33 @@ function Mobile() { 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 (
@@ -205,7 +232,7 @@ function Mobile() {
    {friends.length > 0 ? ( friends.map((friendCode: string, index: number) => ( - + )) ) : (

    No friends yet.

    diff --git a/src/app/api/me/route.ts b/src/app/api/me/route.ts index 564c049..e40536c 100644 --- a/src/app/api/me/route.ts +++ b/src/app/api/me/route.ts @@ -71,10 +71,16 @@ export async function POST(req: Request) { userData = await db.users.update(userData.id, { requests: JSON.parse(requests.toString()) }); 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 }); } catch (error) { - console.error("Error updating user bio:", error); + console.error("Error handling user update:", error); return NextResponse.json( { message: "Internal server error" }, { status: 500 }