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

105 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
2017-07-25 00:59:26 +02:00
import * as modals from "constants/modal_types";
2017-06-06 23:19:12 +02:00
import lbryio from "lbryio";
import rewards from "rewards";
import { selectUnclaimedRewardsByType } from "selectors/rewards";
2017-06-03 01:09:52 +02:00
export function doRewardList() {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
dispatch({
type: types.FETCH_REWARDS_STARTED,
2017-06-06 23:19:12 +02:00
});
2017-06-08 23:15:34 +02:00
lbryio
.call("reward", "list", { multiple_rewards_per_type: true })
2017-06-08 23:15:34 +02:00
.then(userRewards => {
dispatch({
type: types.FETCH_REWARDS_COMPLETED,
data: { userRewards },
});
2017-06-03 01:09:52 +02:00
})
2017-06-08 23:15:34 +02:00
.catch(() => {
dispatch({
type: types.FETCH_REWARDS_COMPLETED,
data: { userRewards: [] },
});
});
};
}
export function doClaimRewardType(rewardType) {
return function(dispatch, getState) {
const rewardsByType = selectUnclaimedRewardsByType(getState()),
2017-06-08 23:15:34 +02:00
reward = rewardsByType[rewardType];
if (reward.transaction_id) {
//already claimed, do nothing
return;
}
2017-05-26 10:53:32 +02:00
dispatch({
type: types.CLAIM_REWARD_STARTED,
2017-06-08 23:15:34 +02:00
data: { reward },
});
2017-06-08 23:15:34 +02:00
const success = reward => {
dispatch({
type: types.CLAIM_REWARD_SUCCESS,
data: {
2017-06-08 23:15:34 +02:00
reward,
},
});
2017-07-25 00:59:26 +02:00
if (reward.reward_type == rewards.TYPE_NEW_USER) {
dispatch({
type: types.OPEN_MODAL,
data: { modal: modals.FIRST_REWARD },
});
}
2017-06-08 23:15:34 +02:00
};
2017-06-08 23:15:34 +02:00
const failure = error => {
dispatch({
type: types.CLAIM_REWARD_FAILURE,
data: {
reward,
error: error ? error : null,
2017-06-08 23:15:34 +02:00
},
});
};
rewards.claimReward(reward.reward_type).then(success, failure);
};
}
export function doClaimEligiblePurchaseRewards() {
return function(dispatch, getState) {
2017-06-22 00:27:07 +02:00
if (!lbryio.enabled) {
2017-06-08 23:15:34 +02:00
return;
}
const rewardsByType = selectUnclaimedRewardsByType(getState());
2017-06-08 23:15:34 +02:00
if (rewardsByType[rewards.TYPE_FIRST_STREAM]) {
dispatch(doClaimRewardType(rewards.TYPE_FIRST_STREAM));
} else {
[
rewards.TYPE_MANY_DOWNLOADS,
rewards.TYPE_FEATURED_DOWNLOAD,
].forEach(type => {
dispatch(doClaimRewardType(type));
});
2017-06-08 23:15:34 +02:00
}
};
}
export function doClaimRewardClearError(reward) {
return function(dispatch, getState) {
dispatch({
type: types.CLAIM_REWARD_CLEAR_ERROR,
2017-06-08 23:15:34 +02:00
data: { reward },
});
};
}