diff --git a/native/avatars/avatar-hooks.js b/native/avatars/avatar-hooks.js --- a/native/avatars/avatar-hooks.js +++ b/native/avatars/avatar-hooks.js @@ -128,7 +128,7 @@ function useUploadSelectedMedia( setProcessingOrUploadInProgress: SetState, -): (selection: ?NativeMediaSelection) => Promise { +): (selection: ?NativeMediaSelection) => Promise { const processSelectedMedia = useProcessSelectedMedia(); const uploadProcessedMedia = useUploadProcessedMedia(); @@ -177,7 +177,14 @@ return undefined; } - return uploadedMedia.id; + if (!uploadedMedia.id) { + return undefined; + } + + return { + type: 'image', + uploadID: uploadedMedia.id, + }; }, [ processSelectedMedia, @@ -218,17 +225,12 @@ const selectFromGalleryAndUpdateThreadAvatar = React.useCallback(async () => { const selection: ?MediaLibrarySelection = await selectFromGallery(); - const uploadedMediaID = await uploadSelectedMedia(selection); + const imageAvatarUpdateRequest = await uploadSelectedMedia(selection); - if (!uploadedMediaID) { + if (!imageAvatarUpdateRequest) { return; } - const imageAvatarUpdateRequest: ImageAvatarDBContent = { - type: 'image', - uploadID: uploadedMediaID, - }; - const updateThreadRequest: UpdateThreadRequest = { threadID, changes: { diff --git a/native/avatars/edit-user-avatar-provider.react.js b/native/avatars/edit-user-avatar-provider.react.js --- a/native/avatars/edit-user-avatar-provider.react.js +++ b/native/avatars/edit-user-avatar-provider.react.js @@ -9,7 +9,6 @@ } from 'lib/actions/user-actions.js'; import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js'; import type { - ImageAvatarDBContent, ENSAvatarDBContent, UpdateUserAvatarRemoveRequest, } from 'lib/types/avatar-types.js'; @@ -57,39 +56,38 @@ userAvatarMediaUploadInProgress || updateUserAvatarLoadingStatus === 'loading'; - const uploadUserAvatarSelectedMedia = useUploadSelectedMedia( + const uploadSelectedMedia = useUploadSelectedMedia( setUserAvatarMediaUploadInProgress, ); - const selectFromGalleryAndUpdateUserAvatar = React.useCallback(async () => { - const selection: ?MediaLibrarySelection = await selectFromGallery(); - const uploadedMediaID = await uploadUserAvatarSelectedMedia(selection); - - if (!uploadedMediaID) { - return; - } - const imageAvatarUpdateRequest: ImageAvatarDBContent = { - type: 'image', - uploadID: uploadedMediaID, - }; + const updateImageUserAvatar = React.useCallback( + async (selection: ?MediaLibrarySelection) => { + const imageAvatarUpdateRequest = await uploadSelectedMedia(selection); + + if (!imageAvatarUpdateRequest) { + return; + } + + dispatchActionPromise( + updateUserAvatarActionTypes, + (async () => { + setUserAvatarMediaUploadInProgress(false); + try { + return await updateUserAvatarCall(imageAvatarUpdateRequest); + } catch (e) { + Alert.alert('Avatar update failed', 'Unable to update avatar.'); + throw e; + } + })(), + ); + }, + [dispatchActionPromise, updateUserAvatarCall, uploadSelectedMedia], + ); - dispatchActionPromise( - updateUserAvatarActionTypes, - (async () => { - setUserAvatarMediaUploadInProgress(false); - try { - return await updateUserAvatarCall(imageAvatarUpdateRequest); - } catch (e) { - Alert.alert('Avatar update failed', 'Unable to update avatar.'); - throw e; - } - })(), - ); - }, [ - dispatchActionPromise, - updateUserAvatarCall, - uploadUserAvatarSelectedMedia, - ]); + const selectFromGalleryAndUpdateUserAvatar = React.useCallback(async () => { + const selection = await selectFromGallery(); + await updateImageUserAvatar(selection); + }, [updateImageUserAvatar]); const setENSUserAvatar = React.useCallback(() => { const ensAvatarRequest: ENSAvatarDBContent = {