import { defaultStyles } from '@/constants/defaultStyles'; import { supabase } from '@/lib/supabase'; import type { Task } from '@/lib/types'; 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, Pressable, SectionList, Text, View, } from 'react-native'; 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(); }, []); const GetTasks = async () => { const { data, error } = await supabase.from('tasks').select('*'); if (error) { Alert.alert('Tasks could not be fetched, please try again'); return; } SetTasks(data ?? []); }; useFocusEffect( useCallback(() => { if (session) { GetTasks(); } }, [session]) ); 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 ( ( await supabase.auth.signOut()} > Logout ), }} /> Tasks Break assignments into small steps and keep your progress clear. router.push('/task/createTask')} > Create Task item.tId} showsVerticalScrollIndicator={false} stickySectionHeadersEnabled={false} contentContainerStyle={{ paddingBottom: 32, }} renderSectionHeader={({ section: { title, data } }) => ( {title} {data.length} )} renderItem={({ item }) => { const isOwner = session?.user.id === item.uId; return ( router.push({ pathname: '/task/viewDetailsTask', params: { tId: item.tId }, }) } > {item.isCompleted && ( )} {item.title} {item.description ? ( {item.description} ) : null} {item.isCompleted ? 'Completed' : 'In progress'} {isOwner && ( router.push({ pathname: '/task/editTask', params: { tId: item.tId }, }) } > Edit DeleteTask(item.tId)} > Delete )} ); }} renderSectionFooter={({ section }) => section.data.length === 0 ? ( {section.emptyMessage} Tasks for this assignment will show up here. ) : ( ) } /> ); }