diff options
author | Ivan Chen <ivan@tagg.id> | 2021-07-27 16:52:32 -0400 |
---|---|---|
committer | Ivan Chen <ivan@tagg.id> | 2021-07-27 16:52:32 -0400 |
commit | ee8ccd2b48dee19c5526c1fccb613b718c0cf759 (patch) | |
tree | 5d574d5816a63d537c6ab5804f24714455a55a19 | |
parent | abf6bf12722845218f0947ca119f848c41dd12f4 (diff) |
Add image moment upload action
-rw-r--r-- | src/screens/profile/CaptionScreen.tsx | 68 | ||||
-rw-r--r-- | src/store/actions/user.ts | 84 |
2 files changed, 104 insertions, 48 deletions
diff --git a/src/screens/profile/CaptionScreen.tsx b/src/screens/profile/CaptionScreen.tsx index 3ee0bd5b..f3b8788b 100644 --- a/src/screens/profile/CaptionScreen.tsx +++ b/src/screens/profile/CaptionScreen.tsx @@ -36,6 +36,7 @@ import * as RootNavigation from '../../RootNavigation'; import {MainStackParams} from '../../routes'; import {patchMoment, postMoment, postMomentTags} from '../../services'; import { + handleImageMomentUpload, handleVideoMomentUpload, loadUserMoments, updateProfileCompletionStage, @@ -138,11 +139,6 @@ const CaptionScreen: React.FC<CaptionScreenProps> = ({route, navigation}) => { // then switch to the profile tab navigation.popToTop(); RootNavigation.navigate('ProfileTab'); - setTimeout(() => { - if (!isMediaAVideo) { - Alert.alert(SUCCESS_PIC_UPLOAD); - } - }, 500); } else { // if editing, simply go back to profile screen navigation.navigate('Profile', { @@ -167,53 +163,29 @@ const CaptionScreen: React.FC<CaptionScreenProps> = ({route, navigation}) => { handleFailed(true); return; } - let profileCompletionStage; - // separate upload logic for image/video if (isMediaAVideo) { - if (videoDuration) { - dispatch( - handleVideoMomentUpload( - mediaUri, - videoDuration, - momentCategory, - formattedTags(), - ), - ); - } else { - handleFailed(); - return; - } - } else { - const momentResponse = await postMoment( - mediaUri, - caption, - momentCategory, - userId, + dispatch( + handleVideoMomentUpload( + mediaUri, + videoDuration ?? 30, + momentCategory, + formattedTags(), + ), ); - if (!momentResponse) { - handleFailed(); - return; - } - profileCompletionStage = momentResponse.profile_completion_stage; - const momentId = momentResponse.moment_id; - if (momentId) { - const momentTagResponse = await postMomentTags( - momentId, + } else { + dispatch( + handleImageMomentUpload( + mediaUri, + caption, + momentCategory, + userId, formattedTags(), - ); - if (!momentTagResponse) { - handleFailed(); - return; - } - } - } - if (!isMediaAVideo) { - dispatch(loadUserMoments(userId)); - } - if (profileCompletionStage) { - dispatch(updateProfileCompletionStage(profileCompletionStage)); + ), + ); } - handleSuccess(); + setTimeout(() => { + handleSuccess(); + }, 500); }; const handleSubmitEditChanges = async () => { diff --git a/src/store/actions/user.ts b/src/store/actions/user.ts index 1acbb519..14865f25 100644 --- a/src/store/actions/user.ts +++ b/src/store/actions/user.ts @@ -6,6 +6,7 @@ import { handlePresignedURL, handleVideoUpload, loadProfileInfo, + postMoment, postMomentTags, removeBadgesService, sendSuggestedPeopleLinked, @@ -285,6 +286,89 @@ export const suggestedPeopleAnimatedTutorialFinished = } }; +export const handleImageMomentUpload = + ( + imageUri: string, + caption: string, + momentCategory: string, + userId: string, + formattedTags: { + x: number; + y: number; + z: number; + user_id: string; + }[], + ): ThunkAction<Promise<void>, RootState, unknown, Action<string>> => + async (dispatch) => { + try { + const handleError = (reason: string) => { + console.error('Moment video upload failed,', reason); + dispatch({ + type: setMomentUploadProgressBar.type, + payload: { + momentUploadProgressBar: { + ...momentUploadProgressBar, + status: MomentUploadStatusType.Error, + }, + }, + }); + }; + let momentUploadProgressBar: MomentUploadProgressBarType = { + status: MomentUploadStatusType.UploadingToS3, + momentId: '', + originalVideoDuration: 1, // assume upload time for an image is same as a 1s video + }; + // set progress bar as loading + dispatch({ + type: setMomentUploadProgressBar.type, + payload: {momentUploadProgressBar}, + }); + // upload image moment + const momentPostResponse = await postMoment( + imageUri, + caption, + momentCategory, + userId, + ); + if (!momentPostResponse) { + handleError('Moment post failed'); + return; + } + const profileCompletionStage = + momentPostResponse.profile_completion_stage; + const momentId = momentPostResponse.moment_id; + if (!momentId) { + handleError('Unable to parse moment id from moment post response'); + return; + } + // upload moment tags + const momentTagResponse = await postMomentTags(momentId, formattedTags); + if (!momentTagResponse) { + handleError('Moment tag post failed'); + return; + } + if (profileCompletionStage) { + dispatch(updateProfileCompletionStage(profileCompletionStage)); + } else { + console.error( + 'failed to parse profile complete stage from moment post response', + ); + } + // mark progress bar state as done + dispatch({ + type: setMomentUploadProgressBar.type, + payload: { + momentUploadProgressBar: { + ...momentUploadProgressBar, + status: MomentUploadStatusType.Done, + }, + }, + }); + } catch (error) { + console.log(error); + } + }; + /** * state is now UploadingToS3: * - get presigned url (backend creates the moment object) |