import { defaultStyles } from '@/constants/defaultStyles'; import { supabase } from '@/lib/supabase'; import type { Assignment, Task } 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 ViewDetailsAssignment() { const { aId } = useLocalSearchParams<{ aId: string }>(); const [assignment, SetAssignment] = useState(null) const [tasks, SetTasks] = useState([]) const [session, SetSession] = useState(null) const taskSections = [ { title: "Upcoming Tasks", data: tasks.filter((task) => !task.isCompleted), emptyMessage: "No upcoming tasks" }, { title: "Completed Tasks", data: tasks.filter((task) => task.isCompleted), emptyMessage: "No completed tasks" }, ]; 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 GetAssignment = async (aId: string) => { const { data, error } = await supabase.from("assignments").select("*").eq("aId", aId).single(); if (error) { Alert.alert("Assignment could not be fetched, please try again"); return; } SetAssignment(data ?? null); } const GetTasks = async (aId: string) => { const { data, error } = await supabase.from("tasks").select("*").eq("aId", aId); if (error) { Alert.alert("Tasks could not be fetched, please try again"); return; } SetTasks(data ?? []); } useFocusEffect( useCallback(() => { if (session && aId) { GetAssignment(aId); GetTasks(aId); } }, [session, aId]) ); 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!"); router.back(); } } ] ) } const DeleteTask = async (tId: string, aId: string) => { Alert.alert( "Delete Task", "Are you sure you want to delete this task?", [ { text: "Cancel", style: "cancel" }, { text: "Delete", style: "destructive", onPress: async () => { const { error } = await supabase.from("tasks").delete().eq("tId", tId); if (error) { Alert.alert("Task could not be deleted, please try again"); return; } Alert.alert("Task deleted successfully!"); GetTasks(aId); } } ] ) } const progress = tasks.length === 0 ? 0 : Math.round((tasks.filter(task => task.isCompleted).length / tasks.length) * 100); return ( { return (