diff --git a/assets/fonts/SpaceMono-Regular.ttf b/assets/fonts/SpaceMono-Regular.ttf old mode 100644 new mode 100755 diff --git a/components/Types.tsx b/components/Types.tsx new file mode 100644 index 0000000..32eec5c --- /dev/null +++ b/components/Types.tsx @@ -0,0 +1,21 @@ + +export type User = { + id: number; + appleId: string | null; + appleEmail: string | null; + fullName: string; + pfpURL: string | null; + pushToken: string; + createdAt: Date; +}; +export type Relationship = { + id: number; + title: string; + status: 'pending' | 'accepted' | 'rejected'; + relationshipStartDate: Date; + createdAt: Date; +}; +export type RelationshipData = { + relationship: Relationship; + partner: User; +}; diff --git a/components/home/Relationships.tsx b/components/home/Relationships.tsx index f5d7efe..dc87e92 100644 --- a/components/home/Relationships.tsx +++ b/components/home/Relationships.tsx @@ -3,34 +3,15 @@ import { Image, StyleSheet, AppState } from 'react-native'; import { ThemedView } from '@/components/ThemedView'; import { ThemedText } from '@/components/ThemedText'; import { getUserData } from '@/components/services/securestorage/UserData'; -import { saveRelationshipData, getRelationshipData } from '@/components/services/securestorage/RelationshipData'; +import { + saveRelationshipData, + getRelationshipData +} from '@/components/services/securestorage/RelationshipData'; import Button from '@/components/buttons/Button'; import { Colors } from '@/constants/Colors'; import { useColorScheme } from '@/hooks/useColorScheme'; import RequestRelationship from '@/components/home/RequestRelationship'; - -type UserData = { - id: number; - appleId: string | null; - appleEmail: string | null; - fullName: string; - pfpURL: string | null; - pushToken: string; - createdAt: Date; -}; - -type Relationship = { - id: number; - title: string; - status: 'pending' | 'accepted' | 'rejected'; - relationshipStartDate: Date; - createdAt: Date; -}; - -type RelationshipData = { - relationship: Relationship; - partner: UserData; -}; +import type { User, Relationship, RelationshipData } from '@/components/Types'; type RelationshipProps = { profilePictureUrl: string | null; @@ -39,32 +20,33 @@ type RelationshipProps = { const Relationships: React.FC = ({ profilePictureUrl }) => { const scheme = useColorScheme() ?? 'light'; const [status, setStatus] = useState(null); - const [userData, setUserData] = useState(null); + const [user, setUser] = useState(null); const [showRequestRelationship, setShowRequestRelationship] = useState(false); const [loading, setLoading] = useState(true); - const handleRequestSent = (data: RelationshipData) => { - setStatus(data); - setShowRequestRelationship(false); - }; - useEffect(() => { fetchRelationshipStatus(); setupPeriodicCheck(); }, []); useEffect(() => { - if (profilePictureUrl && userData) { - setUserData(prevData => prevData ? {...prevData, pfpURL: profilePictureUrl} : null); + if (profilePictureUrl && user) { + setUser(prevUserData => + prevUserData ? {...prevUserData, pfpURL: profilePictureUrl} : null); } }, [profilePictureUrl]); + const handleRequestSent = (data: RelationshipData) => { + setStatus(data); + setShowRequestRelationship(false); + } + const setupPeriodicCheck = () => { let intervalId: NodeJS.Timeout | null = null; const startChecking = () => { checkRelationshipStatus(); - intervalId = setInterval(checkRelationshipStatus, 600000); // + intervalId = setInterval(checkRelationshipStatus, 60000); }; const stopChecking = () => { @@ -75,44 +57,32 @@ const Relationships: React.FC = ({ profilePictureUrl }) => { }; const handleAppStateChange = (nextAppState: string) => { - if (nextAppState === 'active') { - startChecking(); - } else { - stopChecking(); - } + if (nextAppState === 'active') startChecking(); + else stopChecking(); }; const subscription = AppState.addEventListener('change', handleAppStateChange); - - if (AppState.currentState === 'active') { - startChecking(); - } + if (AppState.currentState === 'active') startChecking(); return () => { stopChecking(); subscription.remove(); - }; + } }; const fetchRelationshipStatus = async () => { setLoading(true); try { - const userDataFromStorage: UserData = await getUserData() as UserData; - if (!userDataFromStorage || !userDataFromStorage.id) { - throw new Error('User data not found'); - } - setUserData(userDataFromStorage); - - // First, try to get relationship data from SecureStore + const userFromStorage: User = await getUserData() as User; + if (!userFromStorage || !userFromStorage.id) + throw new Error("User or user data not found"); + setUser(userFromStorage); const storedRelationshipData = await getRelationshipData(); - if (storedRelationshipData) { + if (storedRelationshipData) setStatus(storedRelationshipData); - } - - // Then, fetch the latest data from the API await checkRelationshipStatus(); } catch (error) { - console.log('Error fetching relationship status:', error); + console.log('Error fetching relationship status:', error) } finally { setLoading(false); } @@ -120,20 +90,22 @@ const Relationships: React.FC = ({ profilePictureUrl }) => { const checkRelationshipStatus = async () => { try { - const userDataFromStorage: UserData = await getUserData() as UserData; - if (!userDataFromStorage || !userDataFromStorage.id) { - throw new Error('User data not found'); - } - - const response = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/relationships/getRelationshipData?userId=${userDataFromStorage.id}`, { + const userFromStorage: User = await getUserData() as User; + if (!userFromStorage || !userFromStorage.id) + throw new Error("User or user data not found"); + const response = await fetch( + `${process.env.EXPO_PUBLIC_API_URL}/relationships/getRelationshipData` + + `?userId=${userFromStorage.id}`, { headers: { 'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '', }, }); - if (!response.ok) { const errorData = await response.json(); - throw new Error(errorData.message || `HTTP error! status: ${response.status}`); + throw new Error( + errorData.message || + `HTTP error! status: ${response.status}` + ); } const data = await response.json() as RelationshipData; setStatus(data); @@ -142,16 +114,19 @@ const Relationships: React.FC = ({ profilePictureUrl }) => { console.log('No relationship found or error checking status:', error); setStatus(null); } finally { + setLoading(false); } }; - const handleUpdateRelationshipStatus = async (newStatus: 'accepted' | 'rejected') => { + const handleUpdateRelationshipStatus = + async (newStatus: 'accepted' | 'rejected') => { if (!status || !status.relationship) { console.error('No relationship to update'); return; } try { - const response = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/relationships/updateStatus`, { + const response = await fetch( + `${process.env.EXPO_PUBLIC_API_URL}/relationships/updateStatus`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -162,19 +137,16 @@ const Relationships: React.FC = ({ profilePictureUrl }) => { status: newStatus, }), }); - if (!response.ok) { const errorData = await response.json(); - throw new Error(errorData.message || `HTTP error! status: ${response.status}`); + throw new Error( + errorData.message || + `HTTP error! status: ${response.status}` + ); } - const result = await response.json(); - if (result) { - // Refresh the relationship status - await checkRelationshipStatus(); - } else { - throw new Error('Failed to update relationship status'); - } + if (result) await checkRelationshipStatus(); + else throw new Error('Failed to update relationship status'); } catch (error) { console.error('Error updating relationship status:', error); } @@ -186,14 +158,23 @@ const Relationships: React.FC = ({ profilePictureUrl }) => { if (loading) { return Loading...; } + const renderRelationshipContent = () => { if (!status || !status.relationship) { // Case 1: Not in a relationship return showRequestRelationship ? ( ) : ( - @@ -202,24 +183,41 @@ const Relationships: React.FC = ({ profilePictureUrl }) => { // Case 2 & 3: Pending relationship (we can't differentiate who requested it) return ( <> - Pending Relationship + + Pending Relationship + - {status.partner.fullName} + + {status.partner.fullName} + @@ -230,24 +228,31 @@ const Relationships: React.FC = ({ profilePictureUrl }) => { // Case 4: In an accepted relationship return ( <> - {status.relationship.title} + + {status.relationship.title} + - {userData && ( + {user && ( - {userData.fullName.split(' ')[0]} + + {user.fullName.split(' ')[0]} + )} {status.partner && ( - {status.partner.fullName.split(' ')[0]} + + {status.partner.fullName.split(' ')[0]} + )} diff --git a/components/home/RequestRelationship.tsx b/components/home/RequestRelationship.tsx index 4770515..74c0f1a 100644 --- a/components/home/RequestRelationship.tsx +++ b/components/home/RequestRelationship.tsx @@ -6,32 +6,12 @@ import { getUserData } from '@/components/services/securestorage/UserData'; import { Colors } from '@/constants/Colors'; import { useColorScheme } from '@/hooks/useColorScheme'; import debounce from 'lodash.debounce'; - -type UserData = { - id: number; - appleId: string | null; - appleEmail: string | null; - fullName: string; - pfpURL: string | null; - pushToken: string; - createdAt: Date; -}; - -type RelationshipData = { - relationship: { - id: number; - title: string; - status: "pending" | "accepted" | "rejected"; - relationshipStartDate: Date; - createdAt: Date; - }; - partner: UserData; -}; +import type { User, Relationship, RelationshipData } from '@/components/Types'; const RequestRelationship: React.FC<{ onRequestSent: (data: RelationshipData) => void }> = ({ onRequestSent }) => { const scheme = useColorScheme() ?? 'light'; const [searchTerm, setSearchTerm] = useState(''); - const [searchResults, setSearchResults] = useState([]); + const [searchResults, setSearchResults] = useState([]); const [isLoading, setIsLoading] = useState(false); const [currentUserId, setCurrentUserId] = useState(null); @@ -94,7 +74,7 @@ const RequestRelationship: React.FC<{ onRequestSent: (data: RelationshipData) => } }; - const renderUserItem = ({ item }: { item: UserData }) => ( + const renderUserItem = ({ item }: { item: User }) => ( sendRequest(item.id)}> {item.fullName} {item.appleEmail} diff --git a/components/home/UserInfo.tsx b/components/home/UserInfo.tsx index 7988f5e..7a2c31b 100644 --- a/components/home/UserInfo.tsx +++ b/components/home/UserInfo.tsx @@ -6,29 +6,19 @@ import { getUserData, updateUserData } from "@/components/services/securestorage import { manipulateAsync, SaveFormat } from 'expo-image-manipulator'; import * as ImagePicker from 'expo-image-picker'; import * as FileSystem from 'expo-file-system'; - - -type UserData = { - id: number; - appleId: string | null; - appleEmail: string | null; - fullName: string; - pfpURL: string | null; - pushToken: string; - createdAt: Date; -}; +import type { User } from '@/components/Types'; type UserInfoProps = { onProfilePictureUpdate: (url: string) => void; }; const UserInfo: React.FC = ({ onProfilePictureUpdate }) => { - const [userData, setUserData] = useState(null); + const [userData, setUserData] = useState(null); useEffect(() => { const fetchUserData = async () => { try { - const data: UserData = await getUserData() as UserData; + const data: User = await getUserData() as User; setUserData(data); if (data.pfpURL) { onProfilePictureUpdate(data.pfpURL); @@ -94,7 +84,7 @@ const UserInfo: React.FC = ({ onProfilePictureUpdate }) => { const newPfpURL = responseData.pfpURL; // Update local state - setUserData(prevData => prevData ? {...prevData, pfpURL: newPfpURL} as UserData : null); + setUserData(prevData => prevData ? {...prevData, pfpURL: newPfpURL} as User : null); // Update SecureStorage await updateUserData({ pfpURL: newPfpURL }); diff --git a/example/assets/fonts/SpaceMono-Regular.ttf b/example/assets/fonts/SpaceMono-Regular.ttf old mode 100644 new mode 100755 diff --git a/example/scripts/reset-project.js b/example/scripts/reset-project.js old mode 100644 new mode 100755