diff --git a/app/(tabs)/_layout.tsx b/app/(tabs)/_layout.tsx index 0e4bd89..2464474 100644 --- a/app/(tabs)/_layout.tsx +++ b/app/(tabs)/_layout.tsx @@ -28,9 +28,9 @@ export default function TabLayout() { return null; } - // if (!session) { - // return ; - // } +// if (!session) { +// return ; +// } return ( diff --git a/app/(tabs)/assignments.tsx b/app/(tabs)/assignments.tsx index 6cc5358..abeb58e 100644 --- a/app/(tabs)/assignments.tsx +++ b/app/(tabs)/assignments.tsx @@ -1,10 +1,16 @@ -import { defaultStyles } from "@/constants/defaultStyles"; -import { supabase } from "@/lib/supabase"; +import { defaultStyles } from '@/constants/defaultStyles'; +import { supabase } from '@/lib/supabase'; import { Ionicons } from '@expo/vector-icons'; -import { Session } from "@supabase/supabase-js"; -import { router, Stack, useFocusEffect } from "expo-router"; -import { useCallback, useEffect, useState } from "react"; -import { Alert, Button, Pressable, SectionList, Text, View } from "react-native"; +import { Session } from '@supabase/supabase-js'; +import { router, Stack, useFocusEffect } from 'expo-router'; +import { useCallback, useEffect, useState } from 'react'; +import { + Alert, + Pressable, + SectionList, + Text, + View, +} from 'react-native'; type Assignment = { aId: string; @@ -15,36 +21,52 @@ type Assignment = { lastChanged: string; uId: string; sId: string; -} +}; export default function Assignments() { - const [assignments, SetAssignments] = useState([]) - const [session, SetSession] = useState(null) + const [assignments, SetAssignments] = useState([]); + const [session, SetSession] = useState(null); const assignmentSections = [ - { title: "Upcoming Assignments", data: assignments.filter((assignment) => !assignment.isCompleted), emptyMessage: "No upcoming assignments" }, - { title: "Completed Assignments", data: assignments.filter((assignment) => assignment.isCompleted), emptyMessage: "No completed assignments" }, + { + title: 'Upcoming Assignments', + data: assignments.filter((assignment) => !assignment.isCompleted), + emptyMessage: 'No upcoming assignments', + }, + { + title: 'Completed Assignments', + data: assignments.filter((assignment) => assignment.isCompleted), + emptyMessage: 'No completed assignments', + }, ]; useEffect(() => { - supabase.auth.getSession().then(({ data }) => SetSession(data.session ?? null)) - const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => { - SetSession(newSession) - }) - return () => sub.subscription.unsubscribe() - }, - []) + supabase.auth + .getSession() + .then(({ data }) => SetSession(data.session ?? null)); - const GetAssignments = async () => { - const { data, error } = await supabase.from("assignments").select("*").order("deadline", { ascending: false }); + const { data: sub } = supabase.auth.onAuthStateChange( + (_event, newSession) => { + SetSession(newSession); + } + ); + + return () => sub.subscription.unsubscribe(); + }, []); + + const GetAssignments = async () => { + const { data, error } = await supabase + .from('assignments') + .select('*') + .order('deadline', { ascending: false }); if (error) { - Alert.alert("Assignments could not be fetched, please try again"); + Alert.alert('Assignments could not be fetched, please try again'); return; } SetAssignments(data ?? []); - } + }; useFocusEffect( useCallback(() => { @@ -56,92 +78,205 @@ export default function Assignments() { const DeleteAssignment = async (aId: string) => { Alert.alert( - "Delete Assignment", - "Are you sure you want to delete this assignment?", + 'Delete Assignment', + 'Are you sure you want to delete this assignment?', [ { - text: "Cancel", - style: "cancel" + text: 'Cancel', + style: 'cancel', }, { - text: "Delete", - style: "destructive", + text: 'Delete', + style: 'destructive', onPress: async () => { - const { error } = await supabase.from("assignments").delete().eq("aId", aId); + const { error } = await supabase + .from('assignments') + .delete() + .eq('aId', aId); if (error) { - Alert.alert("Assignment could not be deleted, please try again"); + Alert.alert('Assignment could not be deleted, please try again'); return; } - Alert.alert("Assignment deleted successfully!"); + Alert.alert('Assignment deleted successfully!'); GetAssignments(); - } - } + }, + }, ] - ) - } + ); + }; return ( - + { - return ( - - - - -