lbry-desktop/ui/js/actions/cost_info.js

78 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-05-21 16:42:34 +02:00
import * as types from 'constants/action_types'
2017-04-28 17:14:44 +02:00
import lbry from 'lbry'
import lbryio from 'lbryio'
import {
doResolveUri
} from 'actions/content'
import {
selectResolvingUris,
} from 'selectors/content'
import {
selectClaimsByUri
} from 'selectors/claims'
import {
selectSettingsIsGenerous
} from 'selectors/settings'
2017-04-28 17:14:44 +02:00
export function doFetchCostInfoForUri(uri) {
2017-04-28 17:14:44 +02:00
return function(dispatch, getState) {
const state = getState(),
claim = selectClaimsByUri(state)[uri],
isResolving = selectResolvingUris(state).indexOf(uri) !== -1,
isGenerous = selectSettingsIsGenerous(state)
if (claim === null) { //claim doesn't exist, nothing to fetch a cost for
return
}
if (!claim) {
setTimeout(() => {
dispatch(doFetchCostInfoForUri(uri))
}, 1000)
if (!isResolving) {
dispatch(doResolveUri(uri))
}
return
}
2017-04-28 17:14:44 +02:00
function begin() {
2017-04-28 17:14:44 +02:00
dispatch({
type: types.FETCH_COST_INFO_STARTED,
2017-04-28 17:14:44 +02:00
data: {
uri,
}
})
}
function resolve(costInfo) {
2017-05-12 01:28:43 +02:00
dispatch({
type: types.FETCH_COST_INFO_COMPLETED,
data: {
uri,
costInfo,
2017-05-12 01:28:43 +02:00
}
})
}
if (isGenerous && claim) {
let cost
const fee = claim.value.stream.metadata.fee;
if (fee === undefined ) {
resolve({ cost: 0, includesData: true })
} else if (fee.currency == 'LBC') {
resolve({ cost: fee.amount, includesData: true })
} else {
begin()
lbryio.getExchangeRates().then(({lbc_usd}) => {
resolve({ cost: fee.amount / lbc_usd, includesData: true })
});
}
} else {
begin()
lbry.getCostInfo(uri).then(resolve)
}
2017-04-28 17:14:44 +02:00
}
}