From a01b919c72139d82fa981df6be4e0fe902ff8f70 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Mon, 27 May 2019 14:59:21 +0100 Subject: [PATCH] remove the uri from the failed list if the user reattempts getting the same uri (#151) * remove uri from failed purchase uris for a new purchase * add doDeletePurchasedUri action * add parseFloat for costInfo.cost * add purhcaseUriErrorMessage and more failure handling --- dist/bundle.es.js | 53 +++++++++++++++++++++++++++++++---- dist/flow-typed/File.js | 16 +++++++++++ flow-typed/File.js | 16 +++++++++++ src/constants/action_types.js | 1 + src/index.js | 3 +- src/redux/actions/file.js | 10 +++++-- src/redux/reducers/file.js | 39 +++++++++++++++++++++++++- src/redux/selectors/file.js | 5 ++++ 8 files changed, 133 insertions(+), 10 deletions(-) diff --git a/dist/bundle.es.js b/dist/bundle.es.js index 5ee8ee8..2977116 100644 --- a/dist/bundle.es.js +++ b/dist/bundle.es.js @@ -119,6 +119,7 @@ const SET_FILE_LIST_SORT = 'SET_FILE_LIST_SORT'; const PURCHASE_URI_STARTED = 'PURCHASE_URI_STARTED'; const PURCHASE_URI_COMPLETED = 'PURCHASE_URI_COMPLETED'; const PURCHASE_URI_FAILED = 'PURCHASE_URI_FAILED'; +const DELETE_PURCHASED_URI = 'DELETE_PURCHASED_URI'; const LOADING_FILE_STARTED = 'LOADING_FILE_STARTED'; const LOADING_FILE_COMPLETED = 'LOADING_FILE_COMPLETED'; const LOADING_FILE_FAILED = 'LOADING_FILE_FAILED'; @@ -341,6 +342,7 @@ var action_types = /*#__PURE__*/Object.freeze({ PURCHASE_URI_STARTED: PURCHASE_URI_STARTED, PURCHASE_URI_COMPLETED: PURCHASE_URI_COMPLETED, PURCHASE_URI_FAILED: PURCHASE_URI_FAILED, + DELETE_PURCHASED_URI: DELETE_PURCHASED_URI, LOADING_FILE_STARTED: LOADING_FILE_STARTED, LOADING_FILE_COMPLETED: LOADING_FILE_COMPLETED, LOADING_FILE_FAILED: LOADING_FILE_FAILED, @@ -2317,6 +2319,8 @@ const selectFileListDownloadedSort = reselect.createSelector(selectState$3, stat const selectState$4 = state => state.file || {}; +const selectPurchaseUriErrorMessage = reselect.createSelector(selectState$4, state => state.purchaseUriErrorMessage); + const selectFailedPurchaseUris = reselect.createSelector(selectState$4, state => state.failedPurchaseUris); const selectPurchasedUris = reselect.createSelector(selectState$4, state => state.purchasedUris); @@ -2402,8 +2406,7 @@ function doPurchaseUri(uri, costInfo, saveFile = true) { } const { cost } = costInfo; - - if (cost > balance) { + if (parseFloat(cost) > balance) { dispatch({ type: PURCHASE_URI_FAILED, data: { uri, error: 'Insufficient credits' } @@ -2415,6 +2418,13 @@ function doPurchaseUri(uri, costInfo, saveFile = true) { }; } +function doDeletePurchasedUri(uri) { + return { + type: DELETE_PURCHASED_URI, + data: { uri } + }; +} + function doFetchFileInfo(uri) { return (dispatch, getState) => { const state = getState(); @@ -2912,7 +2922,21 @@ const reducers$1 = {}; const defaultState$1 = { failedPurchaseUris: [], purchasedUris: [], - purchasedStreamingUrls: {} + purchasedStreamingUrls: {}, + purchaseUriErrorMessage: '' +}; + +reducers$1[PURCHASE_URI_STARTED] = (state, action) => { + const { uri } = action.data; + const newFailedPurchaseUris = state.failedPurchaseUris.slice(); + if (newFailedPurchaseUris.includes(uri)) { + newFailedPurchaseUris.splice(newFailedPurchaseUris.indexOf(uri), 1); + } + + return _extends$4({}, state, { + failedPurchaseUris: newFailedPurchaseUris, + purchaseUriErrorMessage: '' + }); }; reducers$1[PURCHASE_URI_COMPLETED] = (state, action) => { @@ -2934,19 +2958,34 @@ reducers$1[PURCHASE_URI_COMPLETED] = (state, action) => { return _extends$4({}, state, { failedPurchaseUris: newFailedPurchaseUris, purchasedUris: newPurchasedUris, - purchasedStreamingUrls: newPurchasedStreamingUrls + purchasedStreamingUrls: newPurchasedStreamingUrls, + purchaseUriErrorMessage: '' }); }; reducers$1[PURCHASE_URI_FAILED] = (state, action) => { - const { uri } = action.data; + const { uri, error } = action.data; const newFailedPurchaseUris = state.failedPurchaseUris.slice(); + if (!newFailedPurchaseUris.includes(uri)) { newFailedPurchaseUris.push(uri); } return _extends$4({}, state, { - failedPurchaseUris: newFailedPurchaseUris + failedPurchaseUris: newFailedPurchaseUris, + purchaseUriErrorMessage: error + }); +}; + +reducers$1[DELETE_PURCHASED_URI] = (state, action) => { + const { uri } = action.data; + const newPurchasedUris = state.purchasedUris.slice(); + if (newPurchasedUris.includes(uri)) { + newPurchasedUris.splice(newPurchasedUris.indexOf(uri), 1); + } + + return _extends$4({}, state, { + purchasedUris: newPurchasedUris }); }; @@ -3649,6 +3688,7 @@ exports.doBalanceSubscribe = doBalanceSubscribe; exports.doBlurSearchInput = doBlurSearchInput; exports.doCheckAddressIsMine = doCheckAddressIsMine; exports.doCreateChannel = doCreateChannel; +exports.doDeletePurchasedUri = doDeletePurchasedUri; exports.doDismissError = doDismissError; exports.doDismissToast = doDismissToast; exports.doError = doError; @@ -3770,6 +3810,7 @@ exports.selectMyClaimsWithoutChannels = selectMyClaimsWithoutChannels; exports.selectPendingById = selectPendingById; exports.selectPendingClaims = selectPendingClaims; exports.selectPlayingUri = selectPlayingUri; +exports.selectPurchaseUriErrorMessage = selectPurchaseUriErrorMessage; exports.selectPurchasedStreamingUrls = selectPurchasedStreamingUrls; exports.selectPurchasedUris = selectPurchasedUris; exports.selectReceiveAddress = selectReceiveAddress; diff --git a/dist/flow-typed/File.js b/dist/flow-typed/File.js index 4384d3f..42c9b16 100644 --- a/dist/flow-typed/File.js +++ b/dist/flow-typed/File.js @@ -51,6 +51,22 @@ declare type PurchaseUriCompleted = { declare type PurchaseUriFailed = { type: ACTIONS.PURCHASE_URI_FAILED, + data: { + uri: string, + error: any + }, +}; + +declare type PurchaseUriStarted = { + type: ACTIONS.PURCHASE_URI_STARTED, + data: { + uri: string, + streamingUrl: string, + }, +}; + +declare type DeletePurchasedUri = { + type: ACTIONS.DELETE_PURCHASED_URI, data: { uri: string }, diff --git a/flow-typed/File.js b/flow-typed/File.js index 4384d3f..42c9b16 100644 --- a/flow-typed/File.js +++ b/flow-typed/File.js @@ -51,6 +51,22 @@ declare type PurchaseUriCompleted = { declare type PurchaseUriFailed = { type: ACTIONS.PURCHASE_URI_FAILED, + data: { + uri: string, + error: any + }, +}; + +declare type PurchaseUriStarted = { + type: ACTIONS.PURCHASE_URI_STARTED, + data: { + uri: string, + streamingUrl: string, + }, +}; + +declare type DeletePurchasedUri = { + type: ACTIONS.DELETE_PURCHASED_URI, data: { uri: string }, diff --git a/src/constants/action_types.js b/src/constants/action_types.js index 9582f77..cb1cdd0 100644 --- a/src/constants/action_types.js +++ b/src/constants/action_types.js @@ -109,6 +109,7 @@ export const SET_FILE_LIST_SORT = 'SET_FILE_LIST_SORT'; export const PURCHASE_URI_STARTED = 'PURCHASE_URI_STARTED'; export const PURCHASE_URI_COMPLETED = 'PURCHASE_URI_COMPLETED'; export const PURCHASE_URI_FAILED = 'PURCHASE_URI_FAILED'; +export const DELETE_PURCHASED_URI = 'DELETE_PURCHASED_URI'; export const LOADING_FILE_STARTED = 'LOADING_FILE_STARTED'; export const LOADING_FILE_COMPLETED = 'LOADING_FILE_COMPLETED'; export const LOADING_FILE_FAILED = 'LOADING_FILE_FAILED'; diff --git a/src/index.js b/src/index.js index 9265f41..848d0e5 100644 --- a/src/index.js +++ b/src/index.js @@ -47,7 +47,7 @@ export { doCreateChannel, } from 'redux/actions/claims'; -export { doPurchaseUri, doFileGet } from 'redux/actions/file'; +export { doDeletePurchasedUri, doPurchaseUri, doFileGet } from 'redux/actions/file'; export { doFetchFileInfo, @@ -111,6 +111,7 @@ export { selectFailedPurchaseUris, selectPurchasedUris, selectPurchasedStreamingUrls, + selectPurchaseUriErrorMessage, selectLastPurchasedUri, makeSelectStreamingUrlForUri, } from 'redux/selectors/file'; diff --git a/src/redux/actions/file.js b/src/redux/actions/file.js index 6d7bb37..337f85f 100644 --- a/src/redux/actions/file.js +++ b/src/redux/actions/file.js @@ -88,8 +88,7 @@ export function doPurchaseUri(uri: string, costInfo: { cost: number }, saveFile: } const { cost } = costInfo; - - if (cost > balance) { + if (parseFloat(cost) > balance) { dispatch({ type: ACTIONS.PURCHASE_URI_FAILED, data: { uri, error: 'Insufficient credits' }, @@ -102,3 +101,10 @@ export function doPurchaseUri(uri: string, costInfo: { cost: number }, saveFile: dispatch(doFileGet(uri, saveFile)); }; } + +export function doDeletePurchasedUri(uri: string) { + return { + type: ACTIONS.DELETE_PURCHASED_URI, + data: { uri }, + }; +} diff --git a/src/redux/reducers/file.js b/src/redux/reducers/file.js index 2e593fa..95a3b7e 100644 --- a/src/redux/reducers/file.js +++ b/src/redux/reducers/file.js @@ -6,6 +6,24 @@ const defaultState = { failedPurchaseUris: [], purchasedUris: [], purchasedStreamingUrls: {}, + purchaseUriErrorMessage: '', +}; + +reducers[ACTIONS.PURCHASE_URI_STARTED] = ( + state: FileState, + action: PurchaseUriStarted +): FileState => { + const { uri } = action.data; + const newFailedPurchaseUris = state.failedPurchaseUris.slice(); + if (newFailedPurchaseUris.includes(uri)) { + newFailedPurchaseUris.splice(newFailedPurchaseUris.indexOf(uri), 1); + } + + return { + ...state, + failedPurchaseUris: newFailedPurchaseUris, + purchaseUriErrorMessage: '', + }; }; reducers[ACTIONS.PURCHASE_URI_COMPLETED] = ( @@ -32,6 +50,7 @@ reducers[ACTIONS.PURCHASE_URI_COMPLETED] = ( failedPurchaseUris: newFailedPurchaseUris, purchasedUris: newPurchasedUris, purchasedStreamingUrls: newPurchasedStreamingUrls, + purchaseUriErrorMessage: '', }; }; @@ -39,8 +58,9 @@ reducers[ACTIONS.PURCHASE_URI_FAILED] = ( state: FileState, action: PurchaseUriFailed ): FileState => { - const { uri } = action.data; + const { uri, error } = action.data; const newFailedPurchaseUris = state.failedPurchaseUris.slice(); + if (!newFailedPurchaseUris.includes(uri)) { newFailedPurchaseUris.push(uri); } @@ -48,6 +68,23 @@ reducers[ACTIONS.PURCHASE_URI_FAILED] = ( return { ...state, failedPurchaseUris: newFailedPurchaseUris, + purchaseUriErrorMessage: error, + }; +}; + +reducers[ACTIONS.DELETE_PURCHASED_URI] = ( + state: FileState, + action: DeletePurchasedUri +): FileState => { + const { uri } = action.data; + const newPurchasedUris = state.purchasedUris.slice(); + if (newPurchasedUris.includes(uri)) { + newPurchasedUris.splice(newPurchasedUris.indexOf(uri), 1); + } + + return { + ...state, + purchasedUris: newPurchasedUris, }; }; diff --git a/src/redux/selectors/file.js b/src/redux/selectors/file.js index 11caee5..7e0a1a1 100644 --- a/src/redux/selectors/file.js +++ b/src/redux/selectors/file.js @@ -5,6 +5,11 @@ type State = { file: FileState }; export const selectState = (state: State): FileState => state.file || {}; +export const selectPurchaseUriErrorMessage: (state: State) => string = createSelector( + selectState, + state => state.purchaseUriErrorMessage +); + export const selectFailedPurchaseUris: (state: State) => Array = createSelector( selectState, state => state.failedPurchaseUris