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 && ( - Profile Preview - )} -
+ const formData = new FormData(); + formData.append("friendCode", friendCode); -
- {/* Username Input */} - setUsername(e.target.value)} - value={username || ""} - placeholder="Update your username..." - /> + fetch(`/api/user/${session?.id}`, { + method: "POST", + body: formData, + }) + .then((res) => res.json()) + .then((data) => { + if (data.message === "Friend request sent successfully") { + alert("Friend request sent!"); + setRequests((prev: any) => [...prev, friendCode]); + setFriendCode(""); // Clear the input field + } else { + alert(data.message || "Failed to send friend request."); + } + }) + .catch((err) => { + console.error("Error sending friend request:", err); + alert("An error occurred while sending the friend request."); + }); + } - {/* Bio Input */} -