diff --git a/app/actions/mediaDownload.ts b/app/actions/mediaDownload.ts index 342010339..a8759d408 100644 --- a/app/actions/mediaDownload.ts +++ b/app/actions/mediaDownload.ts @@ -2,7 +2,7 @@ import { Action } from 'redux'; import { DownloadResumable } from 'expo-file-system'; import { MEDIA_DOWNLOAD } from './actionsTypes'; -import { MediaTypes } from '../lib/methods/handleMediaDownload'; +import { MediaTypes, mediaDownloadKey } from '../lib/methods/handleMediaDownload'; interface IMediaDownloadInProgressAction extends Action { key: string; @@ -26,14 +26,12 @@ interface IMediaDownloadRemove { messageId: string; } -const downloadKey = (mediaType: MediaTypes, messageId: string) => `${mediaType}-${messageId}`; - export const mediaDownloadInProgress = ({ mediaType, messageId, downloadResumable }: IMediaDownloadInprogress): IMediaDownloadInProgressAction => { - const key = downloadKey(mediaType, messageId); + const key = mediaDownloadKey(mediaType, messageId); return { type: MEDIA_DOWNLOAD.IN_PROGRESS, @@ -43,7 +41,7 @@ export const mediaDownloadInProgress = ({ }; export const mediaDownloadRemove = ({ mediaType, messageId }: IMediaDownloadRemove): IMediaDownloadRemoveAction => { - const key = downloadKey(mediaType, messageId); + const key = mediaDownloadKey(mediaType, messageId); return { type: MEDIA_DOWNLOAD.REMOVE, diff --git a/app/lib/methods/handleMediaDownload.ts b/app/lib/methods/handleMediaDownload.ts index 17cbeb315..de58e7b37 100644 --- a/app/lib/methods/handleMediaDownload.ts +++ b/app/lib/methods/handleMediaDownload.ts @@ -22,6 +22,8 @@ const defaultType = { [MediaTypes.video]: 'mp4' }; +export const mediaDownloadKey = (mediaType: MediaTypes, messageId: string) => `${mediaType}-${messageId}`; + export const LOCAL_DOCUMENT_PATH = `${FileSystem.documentDirectory}`; const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1)); diff --git a/app/selectors/mediaDownload.ts b/app/selectors/mediaDownload.ts new file mode 100644 index 000000000..0331abfdc --- /dev/null +++ b/app/selectors/mediaDownload.ts @@ -0,0 +1,26 @@ +import { createSelector } from 'reselect'; + +import { IApplicationState } from '../definitions'; +import { MediaTypes, mediaDownloadKey } from '../lib/methods/handleMediaDownload'; +import { IDownloads } from '../reducers/mediaDownload'; + +const selectMediaDownload = (state: IApplicationState) => state.mediaDownload; + +const getMediaDownload = (mediaDownload: IDownloads, { mediaType, messageId }: { mediaType: MediaTypes; messageId: string }) => { + console.log('🚀 ~ file: mediaDownload.ts:10 ~ getMediaDownload ~ { mediaType, messageId }:', { mediaType, messageId }); + console.log('🚀 ~ file: mediaDownload.ts:10 ~ getMediaDownload ~ mediaDownload:', mediaDownload); + const key = mediaDownloadKey(mediaType, messageId); + if (mediaDownload[key]) return mediaDownload[key]; + return null; +}; + +export const getDownloadResumable = createSelector( + [ + selectMediaDownload, + (_state: IApplicationState, { mediaType, messageId }: { mediaType: MediaTypes; messageId: string }) => ({ + mediaType, + messageId + }) + ], + getMediaDownload +);