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"; type Assignment = { aId: string; title: string; description: string; deadline: string; isCompleted: boolean; lastChanged: string; uId: string; sId: string; } export default function Assignments() { 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" }, ]; useEffect(() => { supabase.auth.getSession().then(({ data }) => SetSession(data.session ?? null)) 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"); return; } SetAssignments(data ?? []); } useFocusEffect( useCallback(() => { if (session) { GetAssignments(); } }, [session]) ); const DeleteAssignment = async (aId: string) => { Alert.alert( "Delete Assignment", "Are you sure you want to delete this assignment?", [ { text: "Cancel", style: "cancel" }, { text: "Delete", style: "destructive", onPress: async () => { const { error } = await supabase.from("assignments").delete().eq("aId", aId); if (error) { Alert.alert("Assignment could not be deleted, please try again"); return; } Alert.alert("Assignment deleted successfully!"); GetAssignments(); } } ] ) } return ( { return (