diff --git a/README.md b/README.md index 99abf53..5eb824f 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ Drink Happy is a fun and engaging web application designed to help users track t ### 🎮 Gamified Drink Tracking - Earn points for healthy drink choices. -- Lose points for exceeding caffeine or sugar limits. - View a detailed **Points Guide** to understand how points are calculated. ### 🏆 Activity Feed diff --git a/src/app/(app)/profile/page.tsx b/src/app/(app)/profile/page.tsx index dcb2dd9..d4309b9 100644 --- a/src/app/(app)/profile/page.tsx +++ b/src/app/(app)/profile/page.tsx @@ -13,6 +13,7 @@ function Mobile() { const [friends, setFriends] = useState(session?.friends || []); const [requests, setRequests] = useState(session?.requests || []); const [friendCode, setFriendCode] = useState(""); // Input for sending friend requests + const [leaderboard, setLeaderboard] = useState([]); // Leaderboard data useEffect(() => { if (isAuthenticated && session) { @@ -21,8 +22,59 @@ function Mobile() { setPoints(session.points || 0); setFriends(session.friends || []); setRequests(session.requests || []); + + // Fetch leaderboard data + const fetchLeaderboard = async () => { + try { + const friendsData = await Promise.all( + (session.friends || []).map((friendId: string) => + fetch(`/api/user/${friendId}`) + .then((res) => { + if (!res.ok) { + throw new Error(`Failed to fetch data for friend ${friendId}`); + } + return res.json(); + }) + .then((data) => { + if (data.user) { + return { + id: data.user.id, + username: data.user.username || "Unknown User", + points: data.user.points || 0, + }; + } else { + console.error(`No user found for friend ID: ${friendId}`); + return null; + } + }) + .catch((err) => { + console.error(`Error fetching data for friend ${friendId}:`, err); + return null; + }) + ) + ); + + // Include the current user in the leaderboard + const userData = { + id: session.id, + username: session.username || "You", + points: session.points || 0, + }; + + // Combine and sort by points in descending order + const sortedLeaderboard = [userData, ...friendsData.filter(Boolean)].sort( + (a, b) => b.points - a.points + ); + + setLeaderboard(sortedLeaderboard); + } catch (error) { + console.error("Error fetching leaderboard data:", error); + } + }; + + fetchLeaderboard(); } - }, [session]); + }, [isAuthenticated, session]); function handleSubmit(e: React.FormEvent) { e.preventDefault(); @@ -235,6 +287,32 @@ function Mobile() { + {/* Leaderboard Card */} +
+

+ Leaderboard +

+
+ {leaderboard.map((entry, index) => ( +
+ + {index + 1}. {entry.username} + + + {entry.points} pts + +
+ ))} +
+
+ {/* Friends, Friend Requests, and Send Friend Request Section */}

@@ -319,8 +397,34 @@ function Mobile() { ); } +function Web() { + const { isAuthenticated, session } = useDevice(); + return ( +
+ Drink Happy Logo Image +

+ {isAuthenticated ? `Welcome, ${session.username} !!` : ""} +

+ {!isAuthenticated ? ( +
+ + +
+ ) : null} +
+ ); +} + export default function ProfilePage() { const { isMobile, isSafari } = useDevice(); - if (isMobile && isSafari) return ; - else return ; + if (isMobile && isSafari) return Mobile(); + else return Mobile(); }