import { defaultStyles } from '@/constants/defaultStyles'; import { CheckSubjectCompletion } from '@/lib/progress'; import { supabase } from '@/lib/supabase'; import type { Assignment, Subject } from '@/lib/types'; import { Session } from '@supabase/supabase-js'; import { router, Stack, useFocusEffect, useLocalSearchParams } from 'expo-router'; import { useCallback, useEffect, useState } from 'react'; import { Alert, Button, Pressable, SectionList, Text, View } from "react-native"; export default function ViewDetailsSubject() { const { sId } = useLocalSearchParams<{ sId: string }>(); const [subject, SetSubject] = 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" }, ]; 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 GetSubject = async (sId: string) => { const { data, error } = await supabase.from("subjects").select("*").eq("sId", sId).single(); if (error) { Alert.alert("Subject could not be fetched, please try again"); return; } SetSubject(data ?? null); } const GetAssignments = async (sId: string) => { const { data, error } = await supabase.from("assignments").select("*").eq("sId", sId).order("deadline", { ascending: true }); if (error) { Alert.alert("Assignments could not be fetched, please try again"); return; } SetAssignments(data ?? []); } useFocusEffect( useCallback(() => { if (session && sId) { GetSubject(sId); GetAssignments(sId); } }, [session, sId]) ); const DeleteSubject = async (sId: string) => { Alert.alert( "Delete Subject", "Are you sure you want to delete this subject?", [ { text: "Cancel", style: "cancel" }, { text: "Delete", style: "destructive", onPress: async () => { const { error } = await supabase.from("subjects").delete().eq("sId", sId); if (error) { Alert.alert("Subject could not be deleted, please try again"); return; } Alert.alert("Subject deleted successfully!"); router.back(); } } ] ) } const DeleteAssignment = async (aId: string, sId: 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!"); if (sId) { try { await CheckSubjectCompletion(sId); } catch { Alert.alert("Failed to update subject status"); } } GetAssignments(sId); } } ] ) } const progress = assignments.length === 0 ? 0 : Math.round((assignments.filter(assignment => assignment.isCompleted).length / assignments.length) * 100); return ( { return (