import React, { useEffect, useState } from 'react'; import { Image, StyleSheet, AppState } from 'react-native'; import { ThemedText, ThemedView } from '@/components/theme/Theme'; import { getUser, getRelationship, getPartner, saveRelationshipData } from '@/components/services/SecureStore'; import RequestRelationship from '@/components/home/RequestRelationship'; import TextButton from '@/components/theme/buttons/TextButton'; import { checkRelationshipStatus, updateRelationshipStatus } from '@/constants/APIs'; import type { User, Relationship, RelationshipData } from '@/constants/Types'; const CHECK_TIME = 2; // In minutes type RelationshipProps = { pfpUrl: string | null; }; const RelationshipView: React.FC = ({ pfpUrl }) => { const [status, setStatus] = useState(null); const [user, setUser] = useState(null); const [showRequestRelationship, setShowRequestRelationship] = useState(false); const [loading, setLoading] = useState(true); useEffect(() => { fetchRelationshipStatus(); setUpPeriodicCheck(); }, []); useEffect(() => { if (pfpUrl && user) { setUser(prevUser => prevUser ? {...prevUser, pfpUrl: pfpUrl} : null); } }, [pfpUrl]); const handleRequestSent = (relationshipData: RelationshipData) => { setStatus(relationshipData); setShowRequestRelationship(false); }; const setUpPeriodicCheck = () => { let intervalId: NodeJS.Timeout | null = null; const startChecking = () => { handleCheckRelationshipStatus(); intervalId = setInterval(handleCheckRelationshipStatus, 60000*CHECK_TIME); }; const stopChecking = () => { if (intervalId) { clearInterval(intervalId); intervalId = null; } }; const handleAppStateChange = (nextAppState: string) => { if (nextAppState === 'active') startChecking(); else stopChecking(); }; const subscription = AppState.addEventListener('change', handleAppStateChange); if (AppState.currentState === 'active') startChecking(); return () => { stopChecking(); subscription.remove(); }; }; const fetchRelationshipStatus = async () => { setLoading(true); try { const userFromStore: User = await getUser() as User; if (!userFromStore) throw new Error('User not found in store.'); setUser(userFromStore); const relationshipFromStore: Relationship = await getRelationship() as Relationship; const partnerFromStore: User = await getPartner() as User; if (!relationshipFromStore || !partnerFromStore) throw new Error('Relationship not found in store.'); setStatus({ relationship: relationshipFromStore, partner: partnerFromStore, }); await handleCheckRelationshipStatus(); } catch (error) { console.log('Error fetching relationship status:', error); } finally { setLoading(false); } }; const handleCheckRelationshipStatus = async () => { try { const userFromStore: User = await getUser() as User; if (!userFromStore) throw new Error('User not found in store.'); const relationshipData: RelationshipData = await checkRelationshipStatus(userFromStore.id); setStatus(relationshipData); await saveRelationshipData(relationshipData); } catch (error) { console.log('No relationship found or error checking relationship status:', error); setStatus(null); } finally { setLoading(false); } }; const handleUpdateRelationshipStatus = async (newStatus: 'accepted' | 'rejected') => { if (!status || !status.relationship || !user || !user.id) { console.error('No relationship found.'); return; } try { if (newStatus === 'accepted') { const updatedRelationshipData: RelationshipData = await updateRelationshipStatus(user.id, newStatus); setStatus(updatedRelationshipData); await saveRelationshipData(updatedRelationshipData); } else { await updateRelationshipStatus(user.id, newStatus); console.log('Rejected relationship. Relationship deleted.'); setStatus(null); } } catch (error) { console.log('Error updating relationship status:', error); } }; const handleAcceptRequest = () => handleUpdateRelationshipStatus('accepted'); const handleRejectRequest = () => handleUpdateRelationshipStatus('rejected'); if (loading) { return ( Loading... ); } const renderRelationshipContent = () => { if (!status || !status.relationship) { // Case 1: Not in a relationship return showRequestRelationship ? ( setShowRequestRelationship(false)} /> ) : ( setShowRequestRelationship(true)} /> ); } else if (!status.relationship.isAccepted && user?.id === status.relationship.requestorId) { // Case 2: Pending relationship & our user requested it return ( <> Pending Relationship {status.partner.fullName} ); } else if (!status.relationship.isAccepted && user?.id !== status.relationship.requestorId) { // Case 3: Pending relationship & someone else requested it return ( <> Pending Relationship {status.partner.fullName} ); } else if (status.relationship.isAccepted) { // Case 4: In an accepted relationship return ( <> {status.relationship.title} {user && ( {user.fullName.split(' ')[0]} )} {status.partner && ( {status.partner.fullName.split(' ')[0]} )} ); } }; return ( {renderRelationshipContent()} ); }; export default RelationshipView; const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, title: { fontSize: 24, fontWeight: 'bold', marginBottom: 20, }, profileContainer: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', marginTop: 20, }, profileWrapper: { alignItems: 'center', marginHorizontal: 10, }, profilePicture: { width: 100, height: 100, borderRadius: 50, marginBottom: 10, }, name: { fontSize: 12, fontWeight: 'bold', }, lastChecked: { fontSize: 12, marginBottom: 10, }, buttonContainer: { justifyContent: 'space-around', width: '100%', marginTop: 20, }, });