create a media download selector

This commit is contained in:
Reinaldo Neto 2023-05-18 19:03:22 -03:00
parent 154deb62ac
commit f136dfb03c
3 changed files with 31 additions and 5 deletions

View File

@ -2,7 +2,7 @@ import { Action } from 'redux';
import { DownloadResumable } from 'expo-file-system'; import { DownloadResumable } from 'expo-file-system';
import { MEDIA_DOWNLOAD } from './actionsTypes'; import { MEDIA_DOWNLOAD } from './actionsTypes';
import { MediaTypes } from '../lib/methods/handleMediaDownload'; import { MediaTypes, mediaDownloadKey } from '../lib/methods/handleMediaDownload';
interface IMediaDownloadInProgressAction extends Action { interface IMediaDownloadInProgressAction extends Action {
key: string; key: string;
@ -26,14 +26,12 @@ interface IMediaDownloadRemove {
messageId: string; messageId: string;
} }
const downloadKey = (mediaType: MediaTypes, messageId: string) => `${mediaType}-${messageId}`;
export const mediaDownloadInProgress = ({ export const mediaDownloadInProgress = ({
mediaType, mediaType,
messageId, messageId,
downloadResumable downloadResumable
}: IMediaDownloadInprogress): IMediaDownloadInProgressAction => { }: IMediaDownloadInprogress): IMediaDownloadInProgressAction => {
const key = downloadKey(mediaType, messageId); const key = mediaDownloadKey(mediaType, messageId);
return { return {
type: MEDIA_DOWNLOAD.IN_PROGRESS, type: MEDIA_DOWNLOAD.IN_PROGRESS,
@ -43,7 +41,7 @@ export const mediaDownloadInProgress = ({
}; };
export const mediaDownloadRemove = ({ mediaType, messageId }: IMediaDownloadRemove): IMediaDownloadRemoveAction => { export const mediaDownloadRemove = ({ mediaType, messageId }: IMediaDownloadRemove): IMediaDownloadRemoveAction => {
const key = downloadKey(mediaType, messageId); const key = mediaDownloadKey(mediaType, messageId);
return { return {
type: MEDIA_DOWNLOAD.REMOVE, type: MEDIA_DOWNLOAD.REMOVE,

View File

@ -22,6 +22,8 @@ const defaultType = {
[MediaTypes.video]: 'mp4' [MediaTypes.video]: 'mp4'
}; };
export const mediaDownloadKey = (mediaType: MediaTypes, messageId: string) => `${mediaType}-${messageId}`;
export const LOCAL_DOCUMENT_PATH = `${FileSystem.documentDirectory}`; export const LOCAL_DOCUMENT_PATH = `${FileSystem.documentDirectory}`;
const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1)); const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1));

View File

@ -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
);