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 Subject = { sId: string; title: string; description: string; isActive: boolean; lastChanged: string; uId: string; }; export default function Subjects() { const [subjects, SetSubjects] = useState([]); const [session, SetSession] = useState(null); const subjectSections = [ { title: 'Active Subjects', data: subjects.filter((subject) => subject.isActive), emptyMessage: 'No active subjects', }, { title: 'Inactive Subjects', data: subjects.filter((subject) => !subject.isActive), emptyMessage: 'No inactive subjects', }, ]; 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 GetSubjects = async () => { const { data, error } = await supabase .from('subjects') .select('*') .order('lastChanged', { ascending: false }); if (error) { Alert.alert('Subjects could not be fetched, please try again'); return; } SetSubjects(data ?? []); }; useFocusEffect( useCallback(() => { if (session) { GetSubjects(); } }, [session]) ); 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!'); GetSubjects(); }, }, ] ); }; return ( ( await supabase.auth.signOut()} > Logout ), }} /> Subjects Organize your study work by subject, then break it into assignments and tasks. router.push('/subject/createSubject')} > Create Subject item.sId} 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: '/subject/viewDetailsSubject', params: { sId: item.sId }, }) } > {item.isActive && ( )} {item.title} {item.description ? ( {item.description} ) : null} {item.isActive ? 'Active' : 'Inactive'} {isOwner && ( router.push({ pathname: '/subject/editSubject', params: { sId: item.sId }, }) } > Edit DeleteSubject(item.sId)} > Delete )} ); }} renderSectionFooter={({ section }) => section.data.length === 0 ? ( {section.emptyMessage} Subjects you create will show up here. ) : ( ) } /> ); }