Skip to content

Commit 78300eb

Browse files
committed
Supabase
1 parent 2e70fcf commit 78300eb

File tree

6 files changed

+219
-36
lines changed

6 files changed

+219
-36
lines changed

TAQ/Navigation.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ function RootStack() {
3030
animation: "slide_from_right",
3131
}}
3232
/>
33+
<Stack.Screen
34+
name="Queue"
35+
component={ClassQueue}
36+
options={{
37+
animation: "slide_from_right",
38+
}}
39+
/>
3340
</Stack.Navigator>
3441
);
3542
}

TAQ/components/Course.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ const Course = ({ course, navigation, disabled }) => {
4646
numberOfLines={2}
4747
style={[styles.name, { fontSize: Platform.OS === "android" ? 14 : 18 }]}
4848
>
49-
{course.name}
49+
{course.courses.name}
5050
</Text>
5151
<Text
5252
style={[styles.code, { fontSize: Platform.OS === "android" ? 13 : 15 }]}
5353
>
54-
{course.code}
54+
{course.courses.code}
5555
</Text>
5656
</AnimatedPressable>
5757
);

TAQ/screens/Auth.jsx

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,102 @@
11
import {
2+
Alert,
3+
AppState,
24
Pressable,
35
StatusBar,
46
StyleSheet,
57
Text,
68
TextInput,
79
View,
810
} from "react-native";
9-
import React, { useState } from "react";
11+
import React, { useEffect, useState } from "react";
1012
import { Colors } from "../config/Colors";
13+
import { supabase } from "../supabase";
1114

1215
const Auth = ({ navigation }) => {
13-
const [username, setUsername] = useState("");
16+
const [email, setEmail] = useState("");
1417
const [password, setPassword] = useState("");
18+
const [loading, setLoading] = useState(false);
19+
20+
useEffect(() => {
21+
const handleAppStateChange = (state) => {
22+
if (state === "active") {
23+
supabase.auth.startAutoRefresh();
24+
checkSession();
25+
} else {
26+
supabase.auth.stopAutoRefresh();
27+
}
28+
};
29+
30+
// Initial session check
31+
checkSession();
32+
33+
// Add the AppState change listener
34+
const subscription = AppState.addEventListener(
35+
"change",
36+
handleAppStateChange
37+
);
38+
39+
// Clean up the listener on component unmount
40+
return () => {
41+
subscription.remove();
42+
supabase.auth.stopAutoRefresh();
43+
};
44+
}, [navigation]);
45+
46+
const checkSession = async () => {
47+
const {
48+
data: { session },
49+
} = await supabase.auth.getSession();
50+
if (session) {
51+
navigation.navigate("Courses");
52+
}
53+
};
54+
55+
async function signInWithEmail() {
56+
setLoading(true);
57+
try {
58+
const { error } = await supabase.auth.signInWithPassword({
59+
email: email,
60+
password: password,
61+
});
62+
63+
if (error) {
64+
Alert.alert("Error", error.message);
65+
} else {
66+
navigation.navigate("Courses");
67+
}
68+
} catch (error) {
69+
Alert.alert("Error", error.message);
70+
} finally {
71+
setLoading(false);
72+
}
73+
}
74+
75+
async function signUpWithEmail() {
76+
setLoading(true);
77+
try {
78+
const { data, error } = await supabase.auth.signUp({
79+
email: email,
80+
password: password,
81+
options: {
82+
data: {
83+
name: "KDT",
84+
},
85+
},
86+
// You can remove the options.data part unless you need it for other metadata
87+
});
88+
89+
if (error) {
90+
Alert.alert("Error", error.message);
91+
} else if (data.session) {
92+
navigation.navigate("Courses");
93+
}
94+
} catch (error) {
95+
Alert.alert("Error", error.message);
96+
} finally {
97+
setLoading(false);
98+
}
99+
}
15100

16101
return (
17102
<View style={styles.container}>
@@ -22,20 +107,17 @@ const Auth = ({ navigation }) => {
22107
/>
23108
<TextInput
24109
style={styles.input}
25-
placeholder="Username"
26-
value={username}
27-
onChangeText={setUsername}
110+
placeholder="Email"
111+
value={email}
112+
onChangeText={setEmail}
28113
/>
29114
<TextInput
30115
style={styles.input}
31116
placeholder="Password"
32117
value={password}
33118
onChangeText={setPassword}
34119
/>
35-
<Pressable
36-
onPress={() => navigation.navigate("Courses")}
37-
style={styles.login}
38-
>
120+
<Pressable onPress={signUpWithEmail} style={styles.login}>
39121
<Text style={{ color: Colors.Background, fontWeight: "bold" }}>
40122
Login
41123
</Text>

TAQ/screens/Home.jsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { FlatList, StatusBar, StyleSheet, Text, View } from "react-native";
2-
import React, { useEffect } from "react";
2+
import React, { useEffect, useState } from "react";
33
import { SafeAreaView } from "react-native-safe-area-context";
44

55
import { Colors } from "../config/Colors";
6-
import courses from "../assets/data/courses";
6+
// import courses from "../assets/data/courses";
77
import Course from "../components/Course";
88

99
import MaterialIcons from "@expo/vector-icons/MaterialIcons";
10+
import { supabase } from "../supabase";
1011

1112
const Home = ({ navigation }) => {
1213
const [isScrolling, setIsScrolling] = React.useState(false);
14+
// const [user, setUser] = useState(null);
15+
const [courses, setCourses] = useState([]);
1316

1417
useEffect(() => {
1518
const unsub = navigation.addListener("beforeRemove", (e) => {
@@ -19,6 +22,23 @@ const Home = ({ navigation }) => {
1922
return unsub;
2023
}, []);
2124

25+
useEffect(() => {
26+
const fetchCourses = async () => {
27+
const { data, error } = await supabase.from("user_courses").select(`
28+
courses (*)
29+
`);
30+
31+
if (error) {
32+
console.error("Error fetching courses:", error);
33+
} else {
34+
console.log("Fetched courses:", data);
35+
setCourses(data);
36+
}
37+
};
38+
39+
fetchCourses();
40+
}, []);
41+
2242
return (
2343
<SafeAreaView style={styles.container}>
2444
<View style={{ paddingTop: 10, flex: 1 }}>
@@ -37,7 +57,12 @@ const Home = ({ navigation }) => {
3757
}}
3858
>
3959
<Text style={styles.welcomeText}>Welcome back, Amit!</Text>
40-
<MaterialIcons name="logout" size={30} color={Colors.Red} />
60+
<MaterialIcons
61+
name="logout"
62+
onPress={() => supabase.auth.signOut()}
63+
size={30}
64+
color={Colors.Red}
65+
/>
4166
</View>
4267
<Text style={styles.sub}>
4368
Please choose from the list of available office hours below:

TAQ/screens/Locations.jsx

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,114 @@
11
import {
22
FlatList,
33
Platform,
4+
Pressable,
45
StatusBar,
56
StyleSheet,
67
Text,
78
View,
89
} from "react-native";
9-
import React from "react";
10+
import React, { useState } from "react";
1011
import courses from "../assets/data/courses";
1112
import { Colors } from "../config/Colors";
13+
import Animated, {
14+
FadeIn,
15+
useAnimatedStyle,
16+
useSharedValue,
17+
withSpring,
18+
} from "react-native-reanimated";
19+
import Constants from "expo-constants";
1220

13-
const Locations = () => {
21+
const Locations = ({ navigation }) => {
1422
const course = courses[1];
1523

24+
const [isScrolling, setIsScrolling] = useState(false);
25+
1626
return (
1727
<View style={styles.container}>
1828
<StatusBar
1929
barStyle={"light-content"}
2030
backgroundColor="transparent"
2131
translucent
2232
/>
23-
<Text style={styles.heading}>Locations</Text>
24-
<Text style={styles.sub}>Please choose one of the following: </Text>
33+
<Animated.Text style={styles.heading}>Locations</Animated.Text>
34+
<Animated.Text style={styles.sub}>
35+
Please choose one of the following:
36+
</Animated.Text>
2537
<View
2638
style={{ height: 2, backgroundColor: "#fff9", marginVertical: 15 }}
2739
/>
2840
<FlatList
2941
data={course.location}
30-
// numColumns={2}
3142
contentContainerStyle={{ gap: 10 }}
43+
onScrollBeginDrag={() => {
44+
// Disable touch events while scrolling
45+
setIsScrolling(true);
46+
}}
47+
onScrollEndDrag={() => {
48+
// Enable touch events after scrolling
49+
setIsScrolling(false, 150);
50+
}}
3251
renderItem={({ item, index }) => (
33-
<LocationItem key={index} location={item} />
52+
<LocationItem
53+
key={index}
54+
location={item}
55+
navigation={navigation}
56+
disabled={isScrolling}
57+
/>
3458
)}
3559
/>
3660
</View>
3761
);
3862
};
3963

40-
const LocationItem = ({ location }) => {
64+
const LocationItem = ({ location, navigation, disabled }) => {
65+
const scale = useSharedValue(1);
66+
67+
const animatedStyle = useAnimatedStyle(() => ({
68+
transform: [{ scale: scale.value }],
69+
}));
70+
71+
const onPressIn = () => {
72+
scale.value = withSpring(0.9, { damping: 15, stiffness: 100 });
73+
};
74+
75+
const onPressOut = () => {
76+
scale.value = withSpring(1, { damping: 15, stiffness: 200 });
77+
if (disabled) return;
78+
navigation.navigate("Queue"); // Navigate after animation
79+
};
80+
81+
const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
82+
4183
return (
42-
<View
43-
style={{
44-
flex: 1,
45-
backgroundColor: Colors.Secondary,
46-
padding: 30,
47-
borderRadius: 15,
48-
margin: 10,
49-
alignItems: "center",
50-
}}
84+
<AnimatedPressable
85+
onPressIn={onPressIn}
86+
onPressOut={onPressOut}
87+
entering={FadeIn.delay(300).damping(0.5).stiffness(100)}
88+
exiting={FadeIn.delay(300).damping(0.5).stiffness(100)}
89+
style={[{ flex: 1 }, animatedStyle]}
5190
>
52-
<Text
53-
style={{ color: Colors.Background, fontWeight: "bold", fontSize: 14 }}
91+
<View
92+
style={{
93+
flex: 1,
94+
backgroundColor: Colors.Secondary,
95+
padding: 30,
96+
borderRadius: 15,
97+
margin: 10,
98+
alignItems: "center",
99+
}}
54100
>
55-
{location}
56-
</Text>
57-
</View>
101+
<Text
102+
style={{
103+
color: Colors.Background,
104+
fontWeight: "bold",
105+
fontSize: Platform.OS === "android" ? 14 : 18,
106+
}}
107+
>
108+
{location}
109+
</Text>
110+
</View>
111+
</AnimatedPressable>
58112
);
59113
};
60114

@@ -64,7 +118,7 @@ const styles = StyleSheet.create({
64118
container: {
65119
flex: 1,
66120
backgroundColor: Colors.Background,
67-
paddingTop: Platform.OS === "android" ? StatusBar.currentHeight + 20 : 0,
121+
paddingTop: Constants.statusBarHeight + 20,
68122
padding: 10,
69123
},
70124
heading: {

TAQ/supabase.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import AsyncStorage from "@react-native-async-storage/async-storage";
2+
import { createClient } from "@supabase/supabase-js";
3+
4+
const supabaseUrl = "https://ijcorhcaxkyxheioiqti.supabase.co";
5+
const supabaseAnonKey =
6+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImlqY29yaGNheGt5eGhlaW9pcXRpIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MzExNjkxMzYsImV4cCI6MjA0Njc0NTEzNn0.a5jb9HY-Sx6u4AgpxM9ctTtTfD44DVOuSHYjT3wH3Us";
7+
8+
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
9+
auth: {
10+
storage: AsyncStorage,
11+
autoRefreshToken: true,
12+
persistSession: true,
13+
detectSessionInUrl: false,
14+
},
15+
});

0 commit comments

Comments
 (0)