1- import { FlatList , StatusBar , StyleSheet , Text , View } from "react-native" ;
1+ import {
2+ ActivityIndicator ,
3+ Alert ,
4+ FlatList ,
5+ StatusBar ,
6+ StyleSheet ,
7+ Text ,
8+ View ,
9+ } from "react-native" ;
210import React , { useEffect , useState } from "react" ;
311import { SafeAreaView } from "react-native-safe-area-context" ;
412
@@ -13,6 +21,8 @@ const Home = ({ navigation }) => {
1321 const [ isScrolling , setIsScrolling ] = React . useState ( false ) ;
1422 // const [user, setUser] = useState(null);
1523 const [ courses , setCourses ] = useState ( [ ] ) ;
24+ const [ user , setUser ] = useState ( null ) ;
25+ const [ loading , setLoading ] = useState ( true ) ;
1626
1727 useEffect ( ( ) => {
1828 const unsub = navigation . addListener ( "beforeRemove" , ( e ) => {
@@ -24,9 +34,15 @@ const Home = ({ navigation }) => {
2434
2535 useEffect ( ( ) => {
2636 const fetchCourses = async ( ) => {
27- const { data, error } = await supabase . from ( "user_courses" ) . select ( `
28- courses (*)
29- ` ) ;
37+ const { data, error } = await supabase
38+ . from ( "user_courses" )
39+ . select (
40+ `
41+ *,
42+ class (*)
43+ `
44+ )
45+ . eq ( "user" , ( await supabase . auth . getUser ( ) ) . data . user . id ) ;
3046
3147 if ( error ) {
3248 console . error ( "Error fetching courses:" , error ) ;
@@ -36,9 +52,34 @@ const Home = ({ navigation }) => {
3652 }
3753 } ;
3854
39- fetchCourses ( ) ;
55+ const fetchUser = async ( ) => {
56+ const { data, error } = await supabase
57+ . from ( "users" )
58+ . select ( "*" )
59+ . eq ( "id" , ( await supabase . auth . getUser ( ) ) . data . user . id ) ;
60+
61+ if ( error ) {
62+ console . error ( "Error fetching user:" , error ) ;
63+ Alert . alert ( "Error" , error . message ) ;
64+ } else {
65+ console . log ( "Fetched user:" , data ) ;
66+ setUser ( data [ 0 ] ) ;
67+ }
68+ } ;
69+
70+ Promise . all ( [ fetchCourses ( ) , fetchUser ( ) ] ) . then ( ( ) => {
71+ setLoading ( false ) ;
72+ } ) ;
4073 } , [ ] ) ;
4174
75+ if ( loading ) {
76+ return (
77+ < View style = { { flex : 1 , justifyContent : "center" , alignItems : "center" } } >
78+ < ActivityIndicator size = "large" color = { Colors . Primary } />
79+ </ View >
80+ ) ;
81+ }
82+
4283 return (
4384 < SafeAreaView style = { styles . container } >
4485 < View style = { { paddingTop : 10 , flex : 1 } } >
@@ -56,10 +97,13 @@ const Home = ({ navigation }) => {
5697 alignItems : "center" ,
5798 } }
5899 >
59- < Text style = { styles . welcomeText } > Welcome back, Amit !</ Text >
100+ < Text style = { styles . welcomeText } > Welcome back, { user . name } !</ Text >
60101 < MaterialIcons
61102 name = "logout"
62- onPress = { ( ) => supabase . auth . signOut ( ) }
103+ onPress = { ( ) => {
104+ supabase . auth . signOut ( ) ;
105+ navigation . goBack ( ) ;
106+ } }
63107 size = { 30 }
64108 color = { Colors . Red }
65109 />
@@ -73,26 +117,34 @@ const Home = ({ navigation }) => {
73117 < View
74118 style = { { height : 2 , backgroundColor : "#fff9" , marginVertical : 15 } }
75119 > </ View >
76- < FlatList
77- data = { courses }
78- numColumns = { 2 }
79- onScrollBeginDrag = { ( ) => {
80- // Disable touch events while scrolling
81- setIsScrolling ( true ) ;
82- } }
83- onScrollEndDrag = { ( ) => {
84- // Enable touch events after scrolling
85- setIsScrolling ( false , 150 ) ;
86- } }
87- renderItem = { ( { item, index } ) => (
88- < Course
89- key = { index }
90- course = { item }
91- navigation = { navigation }
92- disabled = { isScrolling }
93- />
94- ) }
95- />
120+ { courses . length === 0 ? (
121+ < View >
122+ < Text style = { styles . fallbackText } >
123+ You have no courses yet :( please check back later
124+ </ Text >
125+ </ View >
126+ ) : (
127+ < FlatList
128+ data = { courses }
129+ numColumns = { 2 }
130+ onScrollBeginDrag = { ( ) => {
131+ // Disable touch events while scrolling
132+ setIsScrolling ( true ) ;
133+ } }
134+ onScrollEndDrag = { ( ) => {
135+ // Enable touch events after scrolling
136+ setIsScrolling ( false , 150 ) ;
137+ } }
138+ renderItem = { ( { item, index } ) => (
139+ < Course
140+ key = { index }
141+ course = { item }
142+ navigation = { navigation }
143+ disabled = { isScrolling }
144+ />
145+ ) }
146+ />
147+ ) }
96148 </ View >
97149 </ SafeAreaView >
98150 ) ;
@@ -117,4 +169,9 @@ const styles = StyleSheet.create({
117169 color : Colors . Secondary ,
118170 marginVertical : 10 ,
119171 } ,
172+ fallbackText : {
173+ color : Colors . Secondary ,
174+ fontSize : 16 ,
175+ textAlign : "center" ,
176+ } ,
120177} ) ;
0 commit comments