diff --git a/src/app/(app)/profile/page.tsx b/src/app/(app)/profile/page.tsx
index e80e7dd..530ba0a 100644
--- a/src/app/(app)/profile/page.tsx
+++ b/src/app/(app)/profile/page.tsx
@@ -4,147 +4,198 @@ import { useEffect, useState } from "react";
import { useDevice } from "@/lib/context/DeviceContext";
function Mobile() {
- const { isAuthenticated, session } = useDevice();
- const [bio, setBio] = useState(session?.bio || "");
- const [username, setUsername] = useState(session?.username || "");
+ const { isAuthenticated, session } = useDevice();
+ const [bio, setBio] = useState(session?.bio || "");
+ const [username, setUsername] = useState(session?.username || "");
+ const [points, setPoints] = useState(session?.points || 0);
+ const [friends, setFriends] = useState(session?.friends || []);
+ const [requests, setRequests] = useState(session?.requests || []);
+ const [friendCode, setFriendCode] = useState(""); // Input for sending friend requests
- useEffect(() => {
- if (session) {
- setBio(session.bio || "");
- setUsername(session.username || "");
- }
- }, [session]);
+ useEffect(() => {
+ if (isAuthenticated && session) {
+ setBio(session.bio || "");
+ setUsername(session.username || "");
+ setPoints(session.points || 0);
+ setFriends(session.friends || []);
+ setRequests(session.requests || []);
+ }
+ }, [session]);
- function handleSubmit(e: React.FormEvent) {
- e.preventDefault();
- if (bio.length > 0 || username.length > 0) {
- const formData = new FormData();
- if (bio.length > 0) formData.append("bio", bio);
- if (username.length > 0) formData.append("username", username);
+ function handleSubmit(e: React.FormEvent) {
+ e.preventDefault();
+ if (bio.length > 0 || username.length > 0) {
+ const formData = new FormData();
+ if (bio.length > 0) formData.append("bio", bio);
+ if (username.length > 0) formData.append("username", username);
- fetch("/api/me", {
- method: "POST",
- body: formData, // Automatically sets Content-Type to multipart/form-data
- })
- .then((res) => res.json())
- .then((data) => {
- if (data.message === "User updated successfully") {
- alert("Profile updated successfully!");
- } else {
- alert("Failed to update profile.");
- }
- })
- .catch((err) => {
- console.error("Error updating profile:", err);
- alert("An error occurred while updating your profile.");
- });
- } else {
- alert("Please enter a bio or username.");
- }
- }
+ fetch("/api/me", {
+ method: "POST",
+ body: formData,
+ })
+ .then((res) => res.json())
+ .then((data) => {
+ if (data.message === "User updated successfully") {
+ alert("Profile updated successfully!");
+ setPoints(data.updatedPoints || points);
+ } else {
+ alert("Failed to update profile.");
+ }
+ })
+ .catch((err) => {
+ console.error("Error updating profile:", err);
+ alert("An error occurred while updating your profile.");
+ });
+ } else {
+ alert("Please enter a bio or username.");
+ }
+ }
- return (
-
-
-
- Hi, {username || ""}!!
-
+ function handleSendFriendRequest(e: React.FormEvent) {
+ e.preventDefault();
+ if (friendCode.length !== 5) {
+ alert("Friend code must be exactly 5 characters.");
+ return;
+ }
-
- {isAuthenticated && (
-

- )}
-
+ const formData = new FormData();
+ formData.append("friendCode", friendCode);
-
+
-function Web() {
- const { isAuthenticated, session } = useDevice();
+ {/* Friends, Friend Requests, and Send Friend Request Section */}
+
+
+ Friends & Requests
+
- return (
-
-
- {isAuthenticated ? `Welcome, ${session.username} !!` : ""}
-
-
- Use the mobile app for a better experience!
-
-
- );
+ {/* Friend Code */}
+
+
+ Friend Code: {session?.id}
+
+
+
+ {/* Friends List */}
+
+
+ Friends
+
+
+ {friends.length > 0 ? (
+ friends.map((friend: any, index: any) => - {friend}
)
+ ) : (
+ No friends yet.
+ )}
+
+
+
+ {/* Friend Requests */}
+
+
+ Friend Requests
+
+
+ {requests.length > 0 ? (
+ requests.map((request: any, index: any) => - {request}
)
+ ) : (
+ No friend requests yet.
+ )}
+
+
+
+ {/* Send Friend Request */}
+
+
+ Send Friend Request
+
+
+
+
+
+ );
}
export default function ProfilePage() {
- const { isMobile, isSafari } = useDevice();
- if (isMobile && isSafari) return Mobile();
- else return Mobile();
+ const { isMobile, isSafari } = useDevice();
+ if (isMobile && isSafari) return ;
+ else return ;
}
diff --git a/src/app/api/me/route.ts b/src/app/api/me/route.ts
index 64bbceb..429800a 100644
--- a/src/app/api/me/route.ts
+++ b/src/app/api/me/route.ts
@@ -47,6 +47,15 @@ export async function POST(req: Request) {
userData = await db.users.update(userData.id, { inventory: inventoryData });
if (!userData) return NextResponse.json({ message: "Failed to update inventory" }, { status: 500 });
}
+
+ let friends = formData.get("friends");
+ if(friends) {
+ let friendsData = userData.friends;
+ if (!friendsData) friendsData = [];
+ friendsData.push(friends.toString());
+ userData = await db.users.update(userData.id, { friends: friendsData });
+ 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) {
diff --git a/src/app/api/user/[id]/route.ts b/src/app/api/user/[id]/route.ts
index 80a0089..abfcd12 100644
--- a/src/app/api/user/[id]/route.ts
+++ b/src/app/api/user/[id]/route.ts
@@ -34,3 +34,38 @@ export async function GET(req: Request, { params }: any) {
return NextResponse.json({ message: "Internal server error" }, { status: 500 });
}
}
+
+// for making friend requests
+export async function POST(req: Request, { params }: any) {
+ try {
+ if (!(await authenticateUser())) return;
+
+ const { id } = params;
+
+ let user = await db.users.findById(id);
+
+ let formData = await req.formData();
+ if (!formData) return NextResponse.json({ message: "No form data found" }, { status: 400 });
+
+ let friendCode = formData.get("friendCode");
+
+ if (!friendCode) return NextResponse.json({ message: "No friend code found" }, { status: 400 });
+ let friendCodeString = friendCode.toString();
+ if (friendCodeString.length !== 5) return NextResponse.json({ message: "Invalid friend code" }, { status: 400 });
+
+ let requests = user.requests || [];
+
+ if (requests.includes(friendCodeString)) {
+ return NextResponse.json({ message: "Already sent a friend request" }, { status: 400 });
+ }
+
+ requests.push(friendCodeString);
+ user = await db.users.update(user.id, { requests });
+ if (!user) return NextResponse.json({ message: "Failed to update requests" }, { status: 500 });
+ return NextResponse.json({ message: "Friend request sent successfully" }, { status: 200 });
+ } catch (error) {
+ console.error("Error sending friend request:", error);
+ return NextResponse.json({ message: "Internal server error" }, { status: 500 });
+ }
+
+}
\ No newline at end of file
diff --git a/src/lib/css/globals.css b/src/lib/css/globals.css
index 83bf848..0d89c63 100644
--- a/src/lib/css/globals.css
+++ b/src/lib/css/globals.css
@@ -29,3 +29,7 @@ body {
height: 100vh;
@apply dark:bg-[url('/darkmodebackground.png')] bg-[url('/lightmodebackground.png')];
}
+
+code {
+ @apply bg-[#f5f5f5] text-[#333] dark:bg-[#333] dark:text-[#f5f5f5] p-1 rounded-md;
+}
\ No newline at end of file