2017-06-06 17:19:12 -04:00
|
|
|
import * as types from "constants/action_types";
|
|
|
|
import lbryio from "lbryio";
|
|
|
|
import rewards from "rewards";
|
2017-06-21 20:09:30 -04:00
|
|
|
import { selectRewardsByType } from "selectors/rewards";
|
2017-04-24 14:25:27 +07:00
|
|
|
|
2017-06-02 19:09:52 -04:00
|
|
|
export function doRewardList() {
|
2017-04-24 14:25:27 +07:00
|
|
|
return function(dispatch, getState) {
|
2017-06-06 17:19:12 -04:00
|
|
|
const state = getState();
|
2017-04-24 14:25:27 +07:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_REWARDS_STARTED,
|
2017-06-06 17:19:12 -04:00
|
|
|
});
|
2017-04-24 14:25:27 +07:00
|
|
|
|
2017-06-08 17:15:34 -04:00
|
|
|
lbryio
|
|
|
|
.call("reward", "list", {})
|
|
|
|
.then(userRewards => {
|
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_REWARDS_COMPLETED,
|
|
|
|
data: { userRewards },
|
|
|
|
});
|
2017-06-02 19:09:52 -04:00
|
|
|
})
|
2017-06-08 17:15:34 -04:00
|
|
|
.catch(() => {
|
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_REWARDS_COMPLETED,
|
|
|
|
data: { userRewards: [] },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doClaimRewardType(rewardType) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const rewardsByType = selectRewardsByType(getState()),
|
|
|
|
reward = rewardsByType[rewardType];
|
|
|
|
|
|
|
|
if (reward) {
|
|
|
|
dispatch(doClaimReward(reward));
|
|
|
|
}
|
2017-06-06 17:19:12 -04:00
|
|
|
};
|
2017-04-24 14:25:27 +07:00
|
|
|
}
|
|
|
|
|
2017-06-08 17:15:34 -04:00
|
|
|
export function doClaimReward(reward, saveError = false) {
|
2017-04-24 14:25:27 +07:00
|
|
|
return function(dispatch, getState) {
|
2017-06-08 17:15:34 -04:00
|
|
|
if (reward.transaction_id) {
|
|
|
|
//already claimed, do nothing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-26 12:53:32 +04:00
|
|
|
dispatch({
|
|
|
|
type: types.CLAIM_REWARD_STARTED,
|
2017-06-08 17:15:34 -04:00
|
|
|
data: { reward },
|
|
|
|
});
|
2017-06-01 20:51:52 -04:00
|
|
|
|
2017-06-08 17:15:34 -04:00
|
|
|
const success = reward => {
|
2017-06-01 20:51:52 -04:00
|
|
|
dispatch({
|
|
|
|
type: types.CLAIM_REWARD_SUCCESS,
|
|
|
|
data: {
|
2017-06-08 17:15:34 -04:00
|
|
|
reward,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2017-06-01 20:51:52 -04:00
|
|
|
|
2017-06-08 17:15:34 -04:00
|
|
|
const failure = error => {
|
2017-06-01 20:51:52 -04:00
|
|
|
dispatch({
|
|
|
|
type: types.CLAIM_REWARD_FAILURE,
|
|
|
|
data: {
|
|
|
|
reward,
|
2017-06-08 17:15:34 -04:00
|
|
|
error: saveError ? error : null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
rewards.claimReward(reward.reward_type).then(success, failure);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doClaimEligiblePurchaseRewards() {
|
|
|
|
return function(dispatch, getState) {
|
2017-06-21 18:27:07 -04:00
|
|
|
if (!lbryio.enabled) {
|
2017-06-08 17:15:34 -04:00
|
|
|
return;
|
2017-06-01 20:51:52 -04:00
|
|
|
}
|
|
|
|
|
2017-06-08 17:15:34 -04:00
|
|
|
const rewardsByType = selectRewardsByType(getState());
|
|
|
|
|
|
|
|
let types = {};
|
|
|
|
|
|
|
|
types[rewards.TYPE_FIRST_STREAM] = false;
|
|
|
|
types[rewards.TYPE_FEATURED_DOWNLOAD] = false;
|
|
|
|
types[rewards.TYPE_MANY_DOWNLOADS] = false;
|
|
|
|
Object.values(rewardsByType).forEach(reward => {
|
|
|
|
if (types[reward.reward_type] === false && reward.transaction_id) {
|
|
|
|
types[reward.reward_type] = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let unclaimedType = Object.keys(types).find(type => {
|
|
|
|
return types[type] === false && type !== rewards.TYPE_FEATURED_DOWNLOAD; //handled below
|
|
|
|
});
|
|
|
|
if (unclaimedType) {
|
|
|
|
dispatch(doClaimRewardType(unclaimedType));
|
|
|
|
}
|
|
|
|
if (types[rewards.TYPE_FEATURED_DOWNLOAD] === false) {
|
|
|
|
dispatch(doClaimRewardType(rewards.TYPE_FEATURED_DOWNLOAD));
|
|
|
|
}
|
|
|
|
};
|
2017-06-01 20:51:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doClaimRewardClearError(reward) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
dispatch({
|
|
|
|
type: types.CLAIM_REWARD_CLEAR_ERROR,
|
2017-06-08 17:15:34 -04:00
|
|
|
data: { reward },
|
|
|
|
});
|
|
|
|
};
|
2017-04-24 14:25:27 +07:00
|
|
|
}
|