2017-06-06 17:19:12 -04:00
|
|
|
import * as types from "constants/action_types";
|
|
|
|
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 22:14:44 +07:00
|
|
|
|
2017-04-30 00:02:25 +07:00
|
|
|
export function doFetchCostInfoForUri(uri) {
|
2017-04-28 22:14:44 +07:00
|
|
|
return function(dispatch, getState) {
|
2017-05-17 17:52:45 -04:00
|
|
|
const state = getState(),
|
2017-06-06 17:19:12 -04:00
|
|
|
claim = selectClaimsByUri(state)[uri],
|
|
|
|
isGenerous = selectSettingsIsGenerous(state);
|
2017-05-17 17:52:45 -04:00
|
|
|
|
2017-06-11 00:17:37 +07:00
|
|
|
if (!claim) return null;
|
2017-05-17 17:52:45 -04:00
|
|
|
|
|
|
|
function begin() {
|
2017-04-28 22:14:44 +07:00
|
|
|
dispatch({
|
2017-05-17 17:52:45 -04:00
|
|
|
type: types.FETCH_COST_INFO_STARTED,
|
2017-04-28 22:14:44 +07:00
|
|
|
data: {
|
|
|
|
uri,
|
2017-06-06 17:19:12 -04:00
|
|
|
},
|
|
|
|
});
|
2017-05-17 17:52:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function resolve(costInfo) {
|
2017-05-11 19:28:43 -04:00
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_COST_INFO_COMPLETED,
|
|
|
|
data: {
|
|
|
|
uri,
|
2017-05-17 17:52:45 -04:00
|
|
|
costInfo,
|
2017-06-06 17:19:12 -04:00
|
|
|
},
|
|
|
|
});
|
2017-05-17 17:52:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isGenerous && claim) {
|
2017-06-06 17:19:12 -04:00
|
|
|
let cost;
|
2017-08-30 16:56:14 -04:00
|
|
|
const fee = claim.value &&
|
|
|
|
claim.value.stream &&
|
|
|
|
claim.value.stream.metadata
|
|
|
|
? claim.value.stream.metadata.fee
|
|
|
|
: undefined;
|
2017-06-06 17:19:12 -04:00
|
|
|
if (fee === undefined) {
|
|
|
|
resolve({ cost: 0, includesData: true });
|
|
|
|
} else if (fee.currency == "LBC") {
|
|
|
|
resolve({ cost: fee.amount, includesData: true });
|
2017-05-17 17:52:45 -04:00
|
|
|
} else {
|
2017-06-06 17:19:12 -04:00
|
|
|
begin();
|
|
|
|
lbryio.getExchangeRates().then(({ lbc_usd }) => {
|
|
|
|
resolve({ cost: fee.amount / lbc_usd, includesData: true });
|
2017-05-17 17:52:45 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-06 17:19:12 -04:00
|
|
|
begin();
|
|
|
|
lbry.getCostInfo(uri).then(resolve);
|
2017-05-17 17:52:45 -04:00
|
|
|
}
|
2017-06-06 17:19:12 -04:00
|
|
|
};
|
2017-04-28 22:14:44 +07:00
|
|
|
}
|