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 Task = { tId: string; title: string; description: string; isCompleted: boolean; lastChanged: string; deadline: string; uId: string; } export default function Tasks() { 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() }, []) useFocusEffect( useCallback(() => { if (session) { GetTasks(); } }, [session]) ); const GetTasks = async () => { const { data, error } = await supabase.from("tasks").select("tId, title, description, isCompleted,lastChanged, deadline, uId").order("deadline", { ascending: false }); if (error) { Alert.alert("Task could not be fetched, please try again"); return; } SetTasks(data ?? []); } const DeleteTask = async (tId: 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(); } } ] ) } return ( { return (