1- import { getStoredAuth } from "@/src/components/general/AsyncStorage" ;
2- import AsyncStorage from "@react-native-async-storage/async-storage" ;
3- import { router , useFocusEffect } from "expo-router" ;
1+ import { getStoredAuth } from "@/src/components/context/AsyncStorage" ;
2+ import { useAuth } from "@/src/components/context/AuthContext" ;
3+ import { useGame } from "@/src/components/context/GameContext" ;
4+ import { useFocusEffect } from "expo-router" ;
45import { useCallback , useState } from "react" ;
56import { FlatList , StyleSheet , Text , View } from "react-native" ;
67import EventCard from "../../src/components/events/EventCard" ;
7- import type Event from "../../src/interfaces/Event" ;
88import EventIB from "../../src/interfaces/Event" ;
99import { getUserEvents } from "../../src/services/event.service" ;
1010
11- const NewEvents = ( ) => {
12- const [ events , setEvents ] = useState < Event [ ] > ( [ ] ) ;
11+ export default function EventsPage ( ) {
12+ const { user } = useAuth ( ) ;
13+ const { setCurrentParticipantId } = useGame ( ) ;
14+ const [ events , setEvents ] = useState < EventIB [ ] > ( [ ] ) ;
1315 const [ loading , setLoading ] = useState ( true ) ;
1416 const [ expandedEventId , setExpandedEventId ] = useState < string | null > ( null ) ;
1517
@@ -19,16 +21,11 @@ const NewEvents = () => {
1921
2022 try {
2123 const stored = await getStoredAuth ( ) ;
22-
23- if ( stored . isGuest ) {
24- const local = await AsyncStorage . getItem ( "guestEvents" ) ;
25- const events : EventIB [ ] = local ? JSON . parse ( local ) : [ ] ;
26- setEvents ( events ) ;
27- return ;
24+ if ( stored . userId ) {
25+ //guest that has joined event or user with account
26+ const data = await getUserEvents ( stored . userId , stored . accessToken ) ; //TODO: Needs to return participant data
27+ setEvents ( data . events || [ ] ) ;
2828 }
29-
30- const data = await getUserEvents ( stored . userId , stored . accessToken ) ;
31- setEvents ( data ?. events || [ ] ) ;
3229 } finally {
3330 setLoading ( false ) ;
3431 }
@@ -42,8 +39,21 @@ const NewEvents = () => {
4239 ) ;
4340
4441 //dropdown of event
45- const handlePress = ( eventId : string ) => {
46- setExpandedEventId ( ( prev ) => ( prev === eventId ? null : eventId ) ) ;
42+ const handlePress = async ( event : EventIB ) => {
43+ //set participantId based on event pressed
44+ const myParticipantId = event . participantIds ?. find (
45+ ( p ) => p . userId === user ?. _id ,
46+ ) ?. participantId ;
47+
48+ if ( myParticipantId ) {
49+ await setCurrentParticipantId ( myParticipantId ) ; //update context for participantId for tapped event
50+ } else {
51+ console . log ( "No participantId found for current user in this event." ) ;
52+ await setCurrentParticipantId ( "" ) ; //reset if not found
53+ }
54+
55+ //expand event based on ID
56+ setExpandedEventId ( ( prev ) => ( prev === event . _id ? null : event . _id ) ) ;
4757 } ;
4858
4959 if ( loading ) {
@@ -76,21 +86,13 @@ const NewEvents = () => {
7686 < EventCard
7787 event = { item }
7888 expanded = { expandedEventId === item . _id }
79- onPress = { ( ) => handlePress ( item . _id ) }
80- onJoinGame = { ( ) => {
81- router . push ( {
82- pathname : "/GamePages/Game" ,
83- params : { eventId : item . _id } ,
84- } ) ;
85- } }
89+ onPress = { ( ) => handlePress ( item ) }
8690 />
8791 ) }
8892 />
8993 </ View >
9094 ) ;
91- } ;
92-
93- export default NewEvents ;
95+ }
9496
9597const styles = StyleSheet . create ( {
9698 container : {
0 commit comments