@@ -42,13 +42,24 @@ const EditProfileScreen = () => {
4242
4343 const navigation = useNavigation < EditScreenProps [ 'navigation' ] > ( ) ;
4444 const queryClient = useQueryClient ( ) ;
45- const { user, updateUser } = useUserStore ( ( state ) => state ) ;
4645
46+ const { user, updateUser } = useUserStore ( ( state ) => state ) ;
4747 const [ isLoading , setLoading ] = useState ( false ) ;
4848 const [ error , setError ] = useState < string | null > ( null ) ;
4949 const clearError = ( ) => setError ( null ) ;
5050
51+ const [ birthDate , setBirthDate ] = useState < string | null > ( user . birthDate || null ) ;
52+ const [ birthDateInput , setBirthDateInput ] = useState < string > (
53+ formatBirthDate ( user . birthDate ) || ''
54+ ) ;
55+ const [ showDatePicker , setShowDatePicker ] = useState < boolean > ( false ) ;
56+
57+ const [ avatarUrl , setAvatarUrl ] = useState < string | null > ( user . avatarUrl || null ) ;
58+ const [ bannerUrl , setBannerUrl ] = useState < string | null > ( user . bannerUrl || null ) ;
59+ const [ isEditingBanner , setIsEditingBanner ] = useState < boolean > ( false ) ;
5160 const [ menuVisible , setMenuVisible ] = useState < boolean > ( false ) ;
61+
62+ const [ formDataChanged , setFormDataChanged ] = useState < boolean > ( false ) ;
5263 const [ formData , setFormData ] = useState < UpdateProfileRequest > ( {
5364 displayName : user . displayName || '' ,
5465 bio : user . bio || '' ,
@@ -60,16 +71,6 @@ const EditProfileScreen = () => {
6071 } ,
6172 } ) ;
6273
63- const [ birthDate , setBirthDate ] = useState < string | null > ( user . birthDate || null ) ;
64- const [ birthDateInput , setBirthDateInput ] = useState < string > (
65- formatBirthDate ( user . birthDate ) || ''
66- ) ;
67- const [ profileImage , setProfileImage ] = useState < string | null > ( user . avatarUrl || null ) ;
68- const [ bannerImage , setBannerImage ] = useState < string | null > ( user . bannerUrl || null ) ;
69-
70- const [ showDatePicker , setShowDatePicker ] = useState < boolean > ( false ) ;
71- const [ isEditingBanner , setIsEditingBanner ] = useState < boolean > ( false ) ;
72-
7374 const openDatePicker = ( ) => {
7475 setShowDatePicker ( true ) ;
7576 } ;
@@ -122,8 +123,8 @@ const EditProfileScreen = () => {
122123
123124 if ( ! result . canceled && result . assets [ 0 ] ) {
124125 const newUri = result . assets [ 0 ] . uri ;
125- if ( isEditingBanner ) setBannerImage ( newUri ) ;
126- else setProfileImage ( newUri ) ;
126+ if ( isEditingBanner ) setBannerUrl ( newUri ) ;
127+ else setAvatarUrl ( newUri ) ;
127128 setMenuVisible ( false ) ;
128129 }
129130 } ;
@@ -143,6 +144,11 @@ const EditProfileScreen = () => {
143144 // }
144145 // };
145146
147+ const removeBanner = ( ) => {
148+ setBannerUrl ( null ) ;
149+ setIsEditingBanner ( false ) ;
150+ } ;
151+
146152 const handleFormSubmit = useCallback ( async ( ) => {
147153 try {
148154 setLoading ( true ) ;
@@ -157,17 +163,22 @@ const EditProfileScreen = () => {
157163 // update birthday
158164 if ( user . birthDate !== birthDate && birthDate ) {
159165 await updateBirthdate ( birthDate ) ;
160- updateUser ( { birthDate } ) ;
161166 }
162167
163- await updateUserProfile ( formData ) ;
168+ if ( formDataChanged ) {
169+ await updateUserProfile ( formData ) ;
170+ }
171+
164172 // update user store
165- updateUser ( { ...formData , birthDate } ) ;
173+ updateUser ( { ...formData , birthDate, avatarUrl, bannerUrl } ) ;
174+
166175 // update react query cache for profile screen
167176 queryClient . setQueryData ( [ 'profile' , user . username ] , ( user : UserData ) => ( {
168177 ...user ,
169178 ...formData ,
170179 birthDate,
180+ avatarUrl,
181+ bannerUrl,
171182 } ) ) ;
172183 // navigate back to profile screen
173184 navigation . goBack ( ) ;
@@ -178,7 +189,17 @@ const EditProfileScreen = () => {
178189 } finally {
179190 setLoading ( false ) ;
180191 }
181- } , [ formData , navigation , birthDate , user , updateUser , queryClient ] ) ;
192+ } , [
193+ formData ,
194+ navigation ,
195+ birthDate ,
196+ user ,
197+ updateUser ,
198+ avatarUrl ,
199+ bannerUrl ,
200+ formDataChanged ,
201+ queryClient ,
202+ ] ) ;
182203
183204 useEffect ( ( ) => {
184205 navigation . setOptions ( {
@@ -227,7 +248,7 @@ const EditProfileScreen = () => {
227248 } }
228249 >
229250 < ProfileBanner
230- uri = { bannerImage }
251+ uri = { bannerUrl }
231252 username = { user ?. username }
232253 displayName = { user ?. displayName }
233254 isEdit = { true }
@@ -240,7 +261,7 @@ const EditProfileScreen = () => {
240261 setIsEditingBanner ( false ) ;
241262 } }
242263 >
243- < Avatar uri = { profileImage } />
264+ < Avatar uri = { avatarUrl } />
244265 </ Pressable >
245266 </ View >
246267
@@ -250,6 +271,7 @@ const EditProfileScreen = () => {
250271 label = "Name"
251272 onChangeText = { ( value ) => {
252273 setFormData ( ( prev ) => ( { ...prev , [ 'displayName' ] : value } ) ) ;
274+ setFormDataChanged ( true ) ;
253275 } }
254276 autoCapitalize = "none"
255277 />
@@ -258,6 +280,7 @@ const EditProfileScreen = () => {
258280 label = "Bio"
259281 onChangeText = { ( value ) => {
260282 setFormData ( ( prev ) => ( { ...prev , [ 'bio' ] : value } ) ) ;
283+ setFormDataChanged ( true ) ;
261284 } }
262285 autoCapitalize = "none"
263286 />
@@ -266,13 +289,15 @@ const EditProfileScreen = () => {
266289 label = "Location"
267290 onChangeText = { ( value ) => {
268291 setFormData ( ( prev ) => ( { ...prev , [ 'location' ] : value } ) ) ;
292+ setFormDataChanged ( true ) ;
269293 } }
270294 />
271295 < TextInput
272296 value = { formData . websiteUrl }
273297 label = "Website"
274298 onChangeText = { ( value ) => {
275299 setFormData ( ( prev ) => ( { ...prev , [ 'displayName' ] : value } ) ) ;
300+ setFormDataChanged ( true ) ;
276301 } }
277302 />
278303 < Pressable onPress = { openDatePicker } >
@@ -283,8 +308,6 @@ const EditProfileScreen = () => {
283308 disabled = { true }
284309 />
285310 </ Pressable >
286-
287- { /* birthday date picker */ }
288311 </ View >
289312
290313 < Modal
@@ -306,6 +329,18 @@ const EditProfileScreen = () => {
306329 >
307330 < Text style = { styles . dropdownText } > Choose Existing Photo</ Text >
308331 </ Pressable >
332+
333+ { isEditingBanner && bannerUrl && (
334+ < Pressable
335+ style = { styles . dropdownItem }
336+ onPress = { ( ) => {
337+ removeBanner ( ) ;
338+ setMenuVisible ( false ) ;
339+ } }
340+ >
341+ < Text style = { styles . dropdownText } > Remove header</ Text >
342+ </ Pressable >
343+ ) }
309344 </ View >
310345 </ Pressable >
311346 </ Modal >
0 commit comments