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, 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 ( ( await supabase.auth.signOut()} > Logout ), }} /> Assignments Track what is coming up and what you have already finished. router.push('/assignment/createAssignment')} > Create Assignment item.aId} 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: '/assignment/viewDetailsAssignment', params: { aId: item.aId }, }) } > {item.isCompleted && ( )} {item.title} {item.description ? ( {item.description} ) : null} Deadline: {item.deadline || 'No deadline'} {isOwner && ( router.push({ pathname: '/assignment/editAssignment', params: { aId: item.aId }, }) } > Edit DeleteAssignment(item.aId)} > Delete )} ); }} renderSectionFooter={({ section }) => section.data.length === 0 ? ( {section.emptyMessage} New assignments will show up here. ) : ( ) } /> ); }