mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? Docs - [React Native Expo user management guide](https://supabase.com/docs/guides/getting-started/tutorials/with-expo-react-native) ## What is the new behavior? Guide has been updated to be in line with #42269 - Removing `@rneui/themed` package and using native components. - Update guide screenshot. - Main stylesheet for the example. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Documentation - Updated Expo React Native tutorial with modernized component approach using React Native primitives and centralized styling patterns. - Simplified codebase examples with consistent formatting and improved maintainability. - Core functionality preserved with updated implementation patterns. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Chris Chinchilla <chris.ward@supabase.io> Co-authored-by: Chris Chinchilla <chris@chrischinchilla.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
133 lines
3.4 KiB
TypeScript
133 lines
3.4 KiB
TypeScript
import { useState, useEffect } from 'react'
|
|
import { supabase } from '../lib/supabase'
|
|
import { View, Alert, TextInput, Text, TouchableOpacity } from 'react-native'
|
|
import Avatar from './Avatar'
|
|
import { appStyles } from '../styles/styles'
|
|
|
|
export default function Account({ userId, email }: { userId: string; email?: string }) {
|
|
const [loading, setLoading] = useState(true)
|
|
const [username, setUsername] = useState('')
|
|
const [website, setWebsite] = useState('')
|
|
const [avatarUrl, setAvatarUrl] = useState('')
|
|
const styles = appStyles
|
|
|
|
useEffect(() => {
|
|
if (userId) getProfile()
|
|
}, [userId])
|
|
|
|
async function getProfile() {
|
|
try {
|
|
setLoading(true)
|
|
|
|
let { data, error, status } = await supabase
|
|
.from('profiles')
|
|
.select(`username, website, avatar_url`)
|
|
.eq('id', userId)
|
|
.single()
|
|
if (error && status !== 406) {
|
|
throw error
|
|
}
|
|
|
|
if (data) {
|
|
setUsername(data.username)
|
|
setWebsite(data.website)
|
|
setAvatarUrl(data.avatar_url)
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
Alert.alert(error.message)
|
|
}
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
async function updateProfile({
|
|
username,
|
|
website,
|
|
avatar_url,
|
|
}: {
|
|
username: string
|
|
website: string
|
|
avatar_url: string
|
|
}) {
|
|
try {
|
|
setLoading(true)
|
|
|
|
const updates = {
|
|
id: userId,
|
|
username,
|
|
website,
|
|
avatar_url,
|
|
updated_at: new Date(),
|
|
}
|
|
|
|
let { error } = await supabase.from('profiles').upsert(updates)
|
|
|
|
if (error) {
|
|
throw error
|
|
}
|
|
} catch (error: any) {
|
|
Alert.alert(error.message)
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<View style={styles.container}>
|
|
<View>
|
|
<Avatar
|
|
size={200}
|
|
url={avatarUrl}
|
|
onUpload={(url: string) => {
|
|
setAvatarUrl(url)
|
|
updateProfile({ username, website, avatar_url: url })
|
|
}}
|
|
/>
|
|
</View>
|
|
<View style={[styles.verticallySpaced, styles.mt20]}>
|
|
<Text style={styles.label}>Email</Text>
|
|
<TextInput
|
|
value={email ?? ''}
|
|
editable={false}
|
|
selectTextOnFocus={false}
|
|
style={[styles.input, styles.inputDisabled]}
|
|
/>
|
|
</View>
|
|
<View style={styles.verticallySpaced}>
|
|
<Text style={styles.label}>Username</Text>
|
|
<TextInput
|
|
value={username || ''}
|
|
onChangeText={(text) => setUsername(text)}
|
|
style={styles.input}
|
|
/>
|
|
</View>
|
|
<View style={styles.verticallySpaced}>
|
|
<Text style={styles.label}>Website</Text>
|
|
<TextInput
|
|
value={website || ''}
|
|
onChangeText={(text) => setWebsite(text)}
|
|
style={styles.input}
|
|
/>
|
|
</View>
|
|
|
|
<View style={[styles.verticallySpaced, styles.mt20]}>
|
|
<TouchableOpacity
|
|
style={[styles.button, loading && styles.buttonDisabled]}
|
|
onPress={() => updateProfile({ username, website, avatar_url: avatarUrl })}
|
|
disabled={loading}
|
|
>
|
|
<Text style={styles.buttonText}>{loading ? 'Loading ...' : 'Update'}</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
|
|
<View style={styles.verticallySpaced}>
|
|
<TouchableOpacity style={styles.button} onPress={() => supabase.auth.signOut()}>
|
|
<Text style={styles.buttonText}>Sign Out</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
</View>
|
|
)
|
|
}
|