diff --git a/app/(tabs)/profile.tsx b/app/(tabs)/profile.tsx index 5074790..bfa3d34 100644 --- a/app/(tabs)/profile.tsx +++ b/app/(tabs)/profile.tsx @@ -1,12 +1,58 @@ "use client"; import { Image } from "expo-image"; -import { useState } from "react"; -import { ScrollView, Text, TouchableOpacity, View } from "react-native"; +import { useEffect, useState } from "react"; +import { + ActivityIndicator, + ScrollView, + Text, + TouchableOpacity, + View, +} from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; +import { useAuth } from "@/context/auth-context"; +import { getProfile } from "@/services/data/profile"; +import { supabase } from "@/services/supabase"; + export default function ProfileScreen() { const [activeTab, setActiveTab] = useState("Repost"); + const [username, setUsername] = useState(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + const { isAuthenticated } = useAuth(); + + useEffect(() => { + const fetchProfile = async () => { + setLoading(true); + setError(null); + try { + const { data: userData, error: userError } = + await supabase.auth.getUser(); + if (userError || !userData?.user?.id) { + setError("Unable to get user info."); + setUsername(null); + setLoading(false); + return; + } + const { data, error } = await getProfile(userData.user.id); + if (error) { + setError(error.message || "Failed to fetch profile"); + setUsername(null); + } else { + setUsername(data?.username || null); + } + } catch (e: any) { + setError(e.message || "Unknown error"); + setUsername(null); + } finally { + setLoading(false); + } + }; + if (isAuthenticated) { + fetchProfile(); + } + }, [isAuthenticated]); const foodItems = [ { @@ -69,7 +115,17 @@ export default function ProfileScreen() { 👨‍🍳 - Mr. Chef + {loading ? ( + + ) : error ? ( + {error} + ) : ( + {username ?? "-"} + )} Edit diff --git a/services/data/profile.ts b/services/data/profile.ts new file mode 100644 index 0000000..4a3dc75 --- /dev/null +++ b/services/data/profile.ts @@ -0,0 +1,42 @@ +import { supabase } from "@/services/supabase"; +import { PostgrestError } from "@supabase/supabase-js"; + +/** + * Retrieves a user's profile from the `profiles` table. + */ +export async function getProfile(userId: string): Promise<{ + data: { + id: any; + updated_at: any; + username: any; + avatar_url: any; + } | null; + error: PostgrestError | null; +}> { + const { data, error } = await supabase + .from('profiles') + .select(` + id, + updated_at, + username, + avatar_url + `) + .eq('id', userId) + .single() + + return { data, error } +} + +/** + * Updates the username of a user in the `profiles` table. + */ +export async function updateProfile(userId: string, username: string): Promise<{ data: any; error: PostgrestError | null }> { + const { data, error } = await supabase + .from('profiles') + .update({ username: username }) + .eq('id', userId) + .select() + .single() + + return { data, error } +} \ No newline at end of file