diff options
| author | Ivan Chen <ivan@tagg.id> | 2021-07-22 16:08:45 -0400 |
|---|---|---|
| committer | Ivan Chen <ivan@tagg.id> | 2021-07-22 16:08:45 -0400 |
| commit | 848204d684f6dfb8ada0856a50487d00ad2c77df (patch) | |
| tree | 6d554b75cfbe2fbee1e03477aaa0c3f94927e935 /src/store/actions | |
| parent | bc82aaf481949d690af814b9dd4a0e9cab387011 (diff) | |
Add action for handling all video uploads
Diffstat (limited to 'src/store/actions')
| -rw-r--r-- | src/store/actions/user.ts | 98 |
1 files changed, 96 insertions, 2 deletions
diff --git a/src/store/actions/user.ts b/src/store/actions/user.ts index b1cb8719..1acbb519 100644 --- a/src/store/actions/user.ts +++ b/src/store/actions/user.ts @@ -1,13 +1,21 @@ import AsyncStorage from '@react-native-community/async-storage'; -import {StreamChat} from 'stream-chat'; import {Action, ThunkAction} from '@reduxjs/toolkit'; +import {StreamChat} from 'stream-chat'; import { getProfilePic, + handlePresignedURL, + handleVideoUpload, loadProfileInfo, + postMomentTags, removeBadgesService, sendSuggestedPeopleLinked, } from '../../services'; -import {UniversityBadge, UserType} from '../../types/types'; +import { + MomentUploadProgressBarType, + MomentUploadStatusType, + UniversityBadge, + UserType, +} from '../../types/types'; import {getTokenOrLogout} from '../../utils'; import { clearHeaderAndProfileImages, @@ -15,6 +23,7 @@ import { profileBadgesUpdated, profileCompletionStageUpdated, setIsOnboardedUser, + setMomentUploadProgressBar, setNewNotificationReceived, setNewVersionAvailable, setReplyPosted, @@ -275,3 +284,88 @@ export const suggestedPeopleAnimatedTutorialFinished = ); } }; + +/** + * state is now UploadingToS3: + * - get presigned url (backend creates the moment object) + * - upload moment tags + * - upload video to s3 + * state is now WaitingForDoneProcessing + */ +export const handleVideoMomentUpload = + ( + videoUri: string, + videoLength: number, + momentCategory: 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: videoLength, + }; + // set progress bar as loading + dispatch({ + type: setMomentUploadProgressBar.type, + payload: {momentUploadProgressBar}, + }); + // get a presigned url for the video + const presignedURLResponse = await handlePresignedURL(momentCategory); + if (!presignedURLResponse) { + handleError('Presigned URL failed'); + return; + } + const momentId = presignedURLResponse.moment_id; + const fileHash = presignedURLResponse.response_url.fields.key; + // upload moment tags, now that we have a moment id + const momentTagResponse = await postMomentTags(momentId, formattedTags); + if (!momentTagResponse) { + handleError('Upload moment tags failed'); + return; + } + if (!fileHash) { + handleError('Unable to parse file hash from presigned response'); + return; + } + // upload video to s3 + const videoUploadResponse = await handleVideoUpload( + videoUri, + presignedURLResponse, + ); + if (!videoUploadResponse) { + handleError('Video upload failed'); + return; + } + dispatch({ + type: setMomentUploadProgressBar.type, + payload: { + momentUploadProgressBar: { + ...momentUploadProgressBar, + status: MomentUploadStatusType.WaitingForDoneProcessing, + momentId, + }, + }, + }); + } catch (error) { + console.log(error); + } + }; |
