import { useFocusEffect } from 'expo-router'; import { useCallback, useState } from 'react'; import { ScrollView, Text, View } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { getDB } from '../../db'; import { Goal } from '../../types'; export default function GoalsScreen() { const insets = useSafeAreaInsets(); const [goals, setGoals] = useState([]); const fetchData = useCallback(async () => { try { const db = getDB(); const result = await db.getAllAsync('SELECT * FROM goals ORDER BY deadline ASC'); setGoals(result); } catch (error) { console.error('Error fetching goals', error); } }, []); useFocusEffect( useCallback(() => { fetchData(); }, [fetchData]) ); const calculateProgress = (current: number, target: number) => { if (target === 0) return 0; return Math.min((current / target) * 100, 100); }; return ( Financial Goals Stay disciplined and reach your targets. {goals.length === 0 ? ( No goals set yet. ) : ( goals.map((item) => { const progress = calculateProgress(item.current_amount, item.target_amount); return ( {item.name} {item.deadline && ( Deadline: {item.deadline} )} {progress.toFixed(0)}% €{item.current_amount.toFixed(2)} saved Target: €{item.target_amount.toFixed(2)} {/* Progress Bar */} ); }) )} ); }