diff --git a/src/components/dashboard/ProfileSection.astro b/src/components/dashboard/ProfileSection.astro index 088726b..7124c78 100644 --- a/src/components/dashboard/ProfileSection.astro +++ b/src/components/dashboard/ProfileSection.astro @@ -1,6 +1,7 @@ --- import { Icon } from "astro-icon/components"; import ShowProfileLogs from "./ProfileSection/ShowProfileLogs"; +import { Stats } from "./ProfileSection/Stats"; ---
@@ -9,66 +10,7 @@ import ShowProfileLogs from "./ProfileSection/ShowProfileLogs";

Welcome to your IEEE UCSD dashboard

- -
-
-
-
- Events Attended -
-
- 0 -
-
-
- Since joining -
-
-
-
-
-
-
- Loyalty Points -
-
- 0 -
-
-
- No activity -
-
-
-
-
-
-
- Activity Level -
-
- Low -
-
-
- New Member -
-
-
-
-
+
; +} + +export function Stats() { + const [eventsAttended, setEventsAttended] = useState(0); + const [loyaltyPoints, setLoyaltyPoints] = useState(0); + const [activityLevel, setActivityLevel] = useState("Low"); + const [activityDesc, setActivityDesc] = useState("New Member"); + const [pointsChange, setPointsChange] = useState("No activity"); + const [isLoading, setIsLoading] = useState(true); + + useEffect(() => { + const fetchStats = async () => { + try { + setIsLoading(true); + const get = Get.getInstance(); + const auth = Authentication.getInstance(); + const userId = auth.getCurrentUser()?.id; + + if (!userId) return; + + // Fetch all events + const events = await get.getAll("events"); + + // Count events where user is in attendees + const attendedEvents = events.filter(event => + event.attendees?.some(attendee => attendee.user_id === userId) + ); + + const numEventsAttended = attendedEvents.length; + setEventsAttended(numEventsAttended); + + // Calculate loyalty points (1 point per event) + const points = numEventsAttended; + setLoyaltyPoints(points); + + // Set points change message + if (points > 0) { + setPointsChange(`+${points} this semester`); + } + + // Determine activity level + if (points >= 10) { + setActivityLevel("High"); + setActivityDesc("Very Active"); + } else if (points >= 5) { + setActivityLevel("Medium"); + setActivityDesc("Active Member"); + } else if (points >= 1) { + setActivityLevel("Low"); + setActivityDesc("Getting Started"); + } + } catch (error) { + console.error("Error fetching stats:", error); + } finally { + setIsLoading(false); + } + }; + + fetchStats(); + }, []); + + if (isLoading) { + return ( +
+ {[...Array(3)].map((_, i) => ( +
+
+
+
+
+
+
+
+
+ ))} +
+ ); + } + + return ( +
+
+
+
Events Attended
+
{eventsAttended}
+
+
Since joining
+
+
+
+
+
+
Loyalty Points
+
{loyaltyPoints}
+
+
{pointsChange}
+
+
+
+
+
+
Activity Level
+
{activityLevel}
+
+
{activityDesc}
+
+
+
+
+ ); +}