1- import { getDownloadURL , ref , uploadBytes } from '@firebase/storage' ;
1+ import { getDownloadURL , ref } from '@firebase/storage' ;
22import { zodResolver } from '@hookform/resolvers/zod' ;
33import * as ImagePicker from 'expo-image-picker' ;
44import { useLocalSearchParams , useRouter } from 'expo-router' ;
@@ -18,7 +18,8 @@ import { useForm } from 'react-hook-form';
1818import { Image , KeyboardAvoidingView } from 'react-native' ;
1919import * as z from 'zod' ;
2020
21- import { db , getUserById , storage , type UserT } from '@/api' ;
21+ import { db , getUserById , storage } from '@/api' ;
22+ import { uploadImageToStorage } from '@/api/common/firebase-utils' ;
2223import { getCurrentUserId } from '@/core' ;
2324import { Button , ControlledInput , Header , ScreenContainer , View } from '@/ui' ;
2425
@@ -34,14 +35,21 @@ export default function CreateProfile() {
3435 const currentUid = getCurrentUserId ( ) ;
3536 const defaultProfilePic = require ( '/assets/images/default_profile_pic.png' ) ;
3637 const [ profileImage , setProfileImage ] = useState < string | null > ( null ) ;
37- const [ currentUserData , setCurrentUserData ] = useState < UserT | null > ( null ) ;
3838 const [ isUploading , setIsUploading ] = useState ( false ) ;
3939 const [ isSubmiting , setIsSubmiting ] = useState ( false ) ;
4040
4141 const { mode } = useLocalSearchParams < {
4242 mode : 'edit' | 'create' ;
4343 } > ( ) ;
4444
45+ const { handleSubmit, control, setError, reset } = useForm < ProfileFormType > ( {
46+ resolver : zodResolver ( profileSchema ) ,
47+ defaultValues : {
48+ displayName : '' ,
49+ username : '' ,
50+ } ,
51+ } ) ;
52+
4553 useEffect ( ( ) => {
4654 const fetchProfilePic = async ( ) => {
4755 if ( mode !== 'edit' ) return ;
@@ -60,23 +68,18 @@ export default function CreateProfile() {
6068 try {
6169 const userData = await getUserById ( currentUid ) ;
6270 if ( userData == null ) return ;
63- setCurrentUserData ( userData ) ;
71+ reset ( {
72+ displayName : userData . displayName ,
73+ username : userData . username ,
74+ } ) ;
6475 } catch ( error ) {
6576 console . log ( 'No user Data found.' ) ;
6677 }
6778 } ;
6879
6980 fetchProfilePic ( ) ;
7081 getCurrentUserData ( ) ;
71- } , [ mode , currentUid ] ) ;
72-
73- const { handleSubmit, control, setError } = useForm < ProfileFormType > ( {
74- resolver : zodResolver ( profileSchema ) ,
75- defaultValues : {
76- displayName : '' ,
77- username : '' ,
78- } ,
79- } ) ;
82+ } , [ mode , currentUid , reset ] ) ;
8083
8184 const handleImageUpload = async ( ) => {
8285 setIsUploading ( true ) ;
@@ -102,11 +105,8 @@ export default function CreateProfile() {
102105 if ( ! profileImage ) return ;
103106
104107 try {
105- const response = await fetch ( profileImage ) ;
106- const blob = await response . blob ( ) ;
107- const storageRef = ref ( storage , `profilePics/${ currentUid } ` ) ;
108- await uploadBytes ( storageRef , blob ) ;
109- return ;
108+ const filePath = `profilePics/${ currentUid } ` ;
109+ await uploadImageToStorage ( profileImage , filePath ) ;
110110 } catch ( error ) {
111111 console . error ( 'Failed to upload profile image:' , error ) ;
112112 return ;
@@ -166,7 +166,12 @@ export default function CreateProfile() {
166166 ) ;
167167 const usernameSnapshot = await getDocs ( usernameQuery ) ;
168168
169- if ( ! usernameSnapshot . empty ) {
169+ // Check if any other user has this username
170+ const usernameTakenByAnotherUser = usernameSnapshot . docs . some (
171+ ( doc ) => doc . id !== currentUid ,
172+ ) ;
173+
174+ if ( usernameTakenByAnotherUser ) {
170175 setError ( 'username' , {
171176 type : 'custom' ,
172177 message : 'This username is already taken' ,
@@ -229,13 +234,11 @@ export default function CreateProfile() {
229234 control = { control }
230235 name = "displayName"
231236 label = "Display Name"
232- defaultValue = { currentUserData ? currentUserData . displayName : '' }
233237 />
234238 < ControlledInput
235239 control = { control }
236240 name = "username"
237241 label = "Unique Username"
238- defaultValue = { currentUserData ? currentUserData . username : '' }
239242 />
240243
241244 < Button
0 commit comments