diff --git a/shatter-mobile/app/(tabs)/Events.tsx b/shatter-mobile/app/(tabs)/EventsPage.tsx similarity index 53% rename from shatter-mobile/app/(tabs)/Events.tsx rename to shatter-mobile/app/(tabs)/EventsPage.tsx index 9ddf410..7ffc8dd 100644 --- a/shatter-mobile/app/(tabs)/Events.tsx +++ b/shatter-mobile/app/(tabs)/EventsPage.tsx @@ -1,15 +1,17 @@ -import { getStoredAuth } from "@/src/components/general/AsyncStorage"; -import AsyncStorage from "@react-native-async-storage/async-storage"; -import { router, useFocusEffect } from "expo-router"; +import { getStoredAuth } from "@/src/components/context/AsyncStorage"; +import { useAuth } from "@/src/components/context/AuthContext"; +import { useGame } from "@/src/components/context/GameContext"; +import { useFocusEffect } from "expo-router"; import { useCallback, useState } from "react"; import { FlatList, StyleSheet, Text, View } from "react-native"; import EventCard from "../../src/components/events/EventCard"; -import type Event from "../../src/interfaces/Event"; import EventIB from "../../src/interfaces/Event"; import { getUserEvents } from "../../src/services/event.service"; -const NewEvents = () => { - const [events, setEvents] = useState([]); +export default function EventsPage() { + const { user } = useAuth(); + const { setCurrentParticipantId } = useGame(); + const [events, setEvents] = useState([]); const [loading, setLoading] = useState(true); const [expandedEventId, setExpandedEventId] = useState(null); @@ -19,16 +21,11 @@ const NewEvents = () => { try { const stored = await getStoredAuth(); - - if (stored.isGuest) { - const local = await AsyncStorage.getItem("guestEvents"); - const events: EventIB[] = local ? JSON.parse(local) : []; - setEvents(events); - return; + if (stored.userId) { + //guest that has joined event or user with account + const data = await getUserEvents(stored.userId, stored.accessToken); //TODO: Needs to return participant data + setEvents(data.events || []); } - - const data = await getUserEvents(stored.userId, stored.accessToken); - setEvents(data?.events || []); } finally { setLoading(false); } @@ -42,8 +39,21 @@ const NewEvents = () => { ); //dropdown of event - const handlePress = (eventId: string) => { - setExpandedEventId((prev) => (prev === eventId ? null : eventId)); + const handlePress = async (event: EventIB) => { + //set participantId based on event pressed + const myParticipantId = event.participantIds?.find( + (p) => p.userId === user?._id, + )?.participantId; + + if (myParticipantId) { + await setCurrentParticipantId(myParticipantId); //update context for participantId for tapped event + } else { + console.log("No participantId found for current user in this event."); + await setCurrentParticipantId(""); //reset if not found + } + + //expand event based on ID + setExpandedEventId((prev) => (prev === event._id ? null : event._id)); }; if (loading) { @@ -76,21 +86,13 @@ const NewEvents = () => { handlePress(item._id)} - onJoinGame={() => { - router.push({ - pathname: "/GamePages/Game", - params: { eventId: item._id }, - }); - }} + onPress={() => handlePress(item)} /> )} /> ); -}; - -export default NewEvents; +} const styles = StyleSheet.create({ container: { diff --git a/shatter-mobile/app/(tabs)/JoinEvent.tsx b/shatter-mobile/app/(tabs)/JoinEventPage.tsx similarity index 74% rename from shatter-mobile/app/(tabs)/JoinEvent.tsx rename to shatter-mobile/app/(tabs)/JoinEventPage.tsx index 7f59d5d..c4702dc 100644 --- a/shatter-mobile/app/(tabs)/JoinEvent.tsx +++ b/shatter-mobile/app/(tabs)/JoinEventPage.tsx @@ -10,8 +10,26 @@ export default function JoinEventPage() { const [showScanner, setShowScanner] = useState(false); const [eventCode, setEventCode] = useState(""); const { joinEvent } = useJoinEvent(); + const [loading, setLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); + //for manual code entry + const handleJoinEvent = async () => { + setLoading(true); + try { + const eventId = await joinEvent(eventCode); + setErrorMessage(""); + router.push({ + pathname: "/EventPages/EventLobby", + params: { eventId }, + }); + } catch (err) { + setErrorMessage((err as Error).message); + } finally { + setLoading(false); + } + }; + return ( {user ? ( @@ -34,7 +52,7 @@ export default function JoinEventPage() { value={eventCode} onChangeText={(text) => { setEventCode(text); - setErrorMessage(null); + setErrorMessage(""); }} autoCapitalize="characters" autoCorrect={false} @@ -42,30 +60,10 @@ export default function JoinEventPage() {