2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2018-05-05 21:04:47 +02:00
|
|
|
import { MODALS } from 'lbry-redux';
|
2017-12-21 18:32:51 +01:00
|
|
|
import Lbryio from 'lbryio';
|
|
|
|
import { selectUnclaimedRewardsByType } from 'redux/selectors/rewards';
|
|
|
|
import { selectUserIsRewardApproved } from 'redux/selectors/user';
|
2017-12-28 00:48:11 +01:00
|
|
|
import rewards from 'rewards';
|
2017-04-24 09:25:27 +02:00
|
|
|
|
2017-06-03 01:09:52 +02:00
|
|
|
export function doRewardList() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-04-24 09:25:27 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.FETCH_REWARDS_STARTED,
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-04-24 09:25:27 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbryio.call('reward', 'list', { multiple_rewards_per_type: true })
|
2017-06-08 23:15:34 +02:00
|
|
|
.then(userRewards => {
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.FETCH_REWARDS_COMPLETED,
|
2017-06-08 23:15:34 +02:00
|
|
|
data: { userRewards },
|
|
|
|
});
|
2017-06-03 01:09:52 +02:00
|
|
|
})
|
2017-06-08 23:15:34 +02:00
|
|
|
.catch(() => {
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.FETCH_REWARDS_COMPLETED,
|
2017-06-08 23:15:34 +02:00
|
|
|
data: { userRewards: [] },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doClaimRewardType(rewardType) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return (dispatch, getState) => {
|
2017-12-21 18:32:51 +01:00
|
|
|
const state = getState();
|
|
|
|
const rewardsByType = selectUnclaimedRewardsByType(state);
|
|
|
|
const reward = rewardsByType[rewardType];
|
|
|
|
const userIsRewardApproved = selectUserIsRewardApproved(state);
|
2017-06-08 23:15:34 +02:00
|
|
|
|
2017-09-06 22:20:05 +02:00
|
|
|
if (!reward || reward.transaction_id) {
|
2017-12-13 22:36:30 +01:00
|
|
|
// already claimed or doesn't exist, do nothing
|
2017-06-08 23:15:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:07:30 +01:00
|
|
|
if (!userIsRewardApproved && rewardType !== rewards.TYPE_CONFIRM_EMAIL) {
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.OPEN_MODAL,
|
|
|
|
data: { modal: MODALS.REWARD_APPROVAL_REQUIRED },
|
2017-08-26 05:21:26 +02:00
|
|
|
});
|
2017-12-21 18:32:51 +01:00
|
|
|
|
|
|
|
return;
|
2017-08-26 05:21:26 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 10:53:32 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.CLAIM_REWARD_STARTED,
|
2017-06-08 23:15:34 +02:00
|
|
|
data: { reward },
|
|
|
|
});
|
2017-06-02 02:51:52 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
const success = successReward => {
|
2017-06-02 02:51:52 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.CLAIM_REWARD_SUCCESS,
|
2017-06-02 02:51:52 +02:00
|
|
|
data: {
|
2017-12-21 18:32:51 +01:00
|
|
|
reward: successReward,
|
2017-06-08 23:15:34 +02:00
|
|
|
},
|
|
|
|
});
|
2017-12-30 21:59:37 +01:00
|
|
|
if (successReward.reward_type === rewards.TYPE_NEW_USER) {
|
2017-07-25 00:59:26 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.OPEN_MODAL,
|
|
|
|
data: { modal: MODALS.FIRST_REWARD },
|
2017-07-25 00:59:26 +02:00
|
|
|
});
|
|
|
|
}
|
2017-06-08 23:15:34 +02:00
|
|
|
};
|
2017-06-02 02:51:52 +02:00
|
|
|
|
2017-06-08 23:15:34 +02:00
|
|
|
const failure = error => {
|
2017-06-02 02:51:52 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.CLAIM_REWARD_FAILURE,
|
2017-08-30 15:02:40 +02:00
|
|
|
data: { reward, error },
|
2017-06-08 23:15:34 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-31 20:23:57 +02:00
|
|
|
rewards.claimReward(rewardType).then(success, failure);
|
2017-06-08 23:15:34 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doClaimEligiblePurchaseRewards() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return (dispatch, getState) => {
|
2017-12-21 18:32:51 +01:00
|
|
|
const state = getState();
|
|
|
|
const rewardsByType = selectUnclaimedRewardsByType(state);
|
|
|
|
const userIsRewardApproved = selectUserIsRewardApproved(state);
|
2017-08-30 00:27:12 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
if (!userIsRewardApproved || !Lbryio.enabled) {
|
2017-06-08 23:15:34 +02:00
|
|
|
return;
|
2017-06-02 02:51:52 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 05:08:01 +02:00
|
|
|
if (rewardsByType[rewards.TYPE_FIRST_STREAM]) {
|
|
|
|
dispatch(doClaimRewardType(rewards.TYPE_FIRST_STREAM));
|
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
[rewards.TYPE_MANY_DOWNLOADS, rewards.TYPE_FEATURED_DOWNLOAD].forEach(type => {
|
|
|
|
dispatch(doClaimRewardType(type));
|
|
|
|
});
|
2017-06-08 23:15:34 +02:00
|
|
|
}
|
|
|
|
};
|
2017-06-02 02:51:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doClaimRewardClearError(reward) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-06-02 02:51:52 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.CLAIM_REWARD_CLEAR_ERROR,
|
2017-06-08 23:15:34 +02:00
|
|
|
data: { reward },
|
|
|
|
});
|
|
|
|
};
|
2017-04-24 09:25:27 +02:00
|
|
|
}
|