import React, { useEffect, useState } from 'react'; import { StyleSheet, ActivityIndicator, TouchableOpacity } from 'react-native'; import { ThemedText, ThemedView } from '@/components/theme/Theme'; import { Countdown, Relationship, User } from '@/constants/Types'; import { getCountdown } from '@/constants/APIs'; import TextButton from '@/components/theme/buttons/TextButton'; import { getCountdown as getCountdownFromSecureStore, getUser, getRelationship, saveCountdown, } from '@/components/services/SecureStore'; import CountdownChangeDateModal from '@/components/home/CountdownChangeDateModal'; const CountdownView = () => { const [countdownData, setCountdownData] = useState({ days: 0, hours: 0, minutes: 0, seconds: 0, }); const [countdown, setCountdown] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isDateModalOpen, setIsDateModalOpen] = useState(false); const [user, setUser] = useState(null); const [relationship, setRelationship] = useState(null); useEffect(() => { const loadData = async () => { setIsLoading(true); const userData = await getUser(); setUser(userData); const relationshipData = await getRelationship(); setRelationship(relationshipData); const countdownFromSecureStore = await getCountdownFromSecureStore(); if (countdownFromSecureStore) { setCountdown(countdownFromSecureStore); } else if (userData) { const countdownFromServer = await getCountdown(userData.id); if (countdownFromServer) { setCountdown(countdownFromServer); await saveCountdown(countdownFromServer); } } setIsLoading(false); }; loadData(); }, []); useEffect(() => { if (countdown === null) return; const interval = setInterval(() => { const now = new Date(); const diff = new Date(countdown.date).getTime() - now.getTime(); const days = Math.floor(diff / (1000 * 60 * 60 * 24)); const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); const seconds = Math.floor((diff % (1000 * 60)) / 1000); setCountdownData({ days, hours, minutes, seconds }); if (diff <= 0) { clearInterval(interval); setCountdownData({ days: 0, hours: 0, minutes: 0, seconds: 0 }); } }, 1000); return () => clearInterval(interval); }, [countdown]); const handleCountdownUpdate = async (newDate: Date, newTitle: string) => { if (relationship) { const newCountdown: Countdown = countdown ? { ...countdown, date: newDate, title: newTitle } : { id: 0, // This will be set by the server relationshipId: relationship.id, title: newTitle ? newTitle : 'Countdown to Next Visit', date: newDate, createdAt: new Date(), }; setCountdown(newCountdown); await saveCountdown(newCountdown); } }; if (isLoading) { return ( ); } if (!relationship) { return ( You are not in a relationship yet. ); } if (!countdown) { return ( No countdown set yet. setIsDateModalOpen(true)} /> ); } return ( {countdown?.title ?? 'Countdown til Next Visit'} setIsDateModalOpen(true)} /> {user && countdown && ( setIsDateModalOpen(false)} onDateChange={handleCountdownUpdate} currentCountdown={countdown} /> )} ); }; export default CountdownView; const CountdownItem = ({value, label}: { value: number, label: string }) => { return ( {value} {label} ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: 'transparent', }, innerContainer: { flex: 1, alignItems: 'center', paddingHorizontal: 10, backgroundColor: 'transparent', }, title: { fontSize: 24, lineHeight: 32, fontWeight: '600', textAlign: 'center', marginHorizontal: 'auto', }, countdownContainer: { flexDirection: 'row', justifyContent: 'space-around', alignItems: 'center', width: '90%', backgroundColor: 'transparent', marginVertical: 10, }, countdownItem: { alignItems: 'center', marginHorizontal: 5, backgroundColor: 'transparent', }, countdownValue: { fontSize: 28, lineHeight: 42, fontWeight: 'bold', }, countdownLabel: { fontSize: 18, lineHeight: 24, }, });