2017-06-06 17:19:12 -04:00
|
|
|
import * as types from "constants/action_types";
|
2017-07-24 18:59:26 -04:00
|
|
|
import * as modals from "constants/modal_types";
|
2017-06-06 17:19:12 -04:00
|
|
|
import lbryio from "lbryio";
|
|
|
|
import rewards from "rewards";
|
2017-08-18 23:08:01 -04:00
|
|
|
import { selectUnclaimedRewardsByType } from "selectors/rewards";
|
2017-08-25 23:21:26 -04:00
|
|
|
import { selectUserIsRewardApproved } from "selectors/user";
|
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
|
2017-08-18 23:08:01 -04:00
|
|
|
.call("reward", "list", { multiple_rewards_per_type: true })
|
2017-06-08 17:15:34 -04:00
|
|
|
.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) {
|
2017-08-25 23:21:26 -04:00
|
|
|
const state = getState(),
|
|
|
|
rewardsByType = selectUnclaimedRewardsByType(state),
|
|
|
|
reward = rewardsByType[rewardType],
|
|
|
|
userIsRewardApproved = selectUserIsRewardApproved(state);
|
2017-06-08 17:15:34 -04:00
|
|
|
|
2017-09-06 16:20:05 -04:00
|
|
|
if (!reward || reward.transaction_id) {
|
|
|
|
//already claimed or doesn't exist, do nothing
|
2017-06-08 17:15:34 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-25 23:21:26 -04:00
|
|
|
if (!userIsRewardApproved) {
|
|
|
|
return dispatch({
|
|
|
|
type: types.OPEN_MODAL,
|
|
|
|
data: { modal: modals.REWARD_APPROVAL_REQUIRED },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-07-24 18:59:26 -04:00
|
|
|
if (reward.reward_type == rewards.TYPE_NEW_USER) {
|
|
|
|
dispatch({
|
|
|
|
type: types.OPEN_MODAL,
|
|
|
|
data: { modal: modals.FIRST_REWARD },
|
|
|
|
});
|
|
|
|
}
|
2017-06-08 17:15:34 -04:00
|
|
|
};
|
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,
|
2017-08-30 09:02:40 -04:00
|
|
|
data: { reward, error },
|
2017-06-08 17:15:34 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-31 14:23:57 -04:00
|
|
|
rewards.claimReward(rewardType).then(success, failure);
|
2017-06-08 17:15:34 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doClaimEligiblePurchaseRewards() {
|
|
|
|
return function(dispatch, getState) {
|
2017-08-29 18:27:12 -04:00
|
|
|
const state = getState(),
|
|
|
|
rewardsByType = selectUnclaimedRewardsByType(state),
|
|
|
|
userIsRewardApproved = selectUserIsRewardApproved(state);
|
|
|
|
|
|
|
|
if (!userIsRewardApproved || !lbryio.enabled) {
|
2017-06-08 17:15:34 -04:00
|
|
|
return;
|
2017-06-01 20:51:52 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 23:08:01 -04: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 17:15:34 -04:00
|
|
|
}
|
|
|
|
};
|
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
|
|
|
}
|