2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
|
|
|
import Lbryio from 'lbryio';
|
2018-05-08 15:12:50 +02:00
|
|
|
import { doNotify, MODALS } from 'lbry-redux';
|
2018-05-17 22:31:49 +02:00
|
|
|
import { selectUnclaimedRewards } from 'redux/selectors/rewards';
|
2017-12-21 18:32:51 +01:00
|
|
|
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: [] },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-07 05:37:48 +02:00
|
|
|
export function doClaimRewardType(rewardType, options) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return (dispatch, getState) => {
|
2017-12-21 18:32:51 +01:00
|
|
|
const state = getState();
|
2018-05-17 22:31:49 +02:00
|
|
|
const unclaimedRewards = selectUnclaimedRewards(state);
|
|
|
|
const reward = unclaimedRewards.find(ur => ur.reward_type === rewardType);
|
2017-12-21 18:32:51 +01:00
|
|
|
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) {
|
2018-05-08 15:12:50 +02:00
|
|
|
const action = doNotify({
|
|
|
|
id: MODALS.REWARD_APPROVAL_REQUIRED,
|
|
|
|
isError: false,
|
2017-08-26 05:21:26 +02:00
|
|
|
});
|
2018-05-08 15:12:50 +02:00
|
|
|
dispatch(action);
|
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 => {
|
2018-07-18 01:14:29 +02:00
|
|
|
dispatch(doRewardList());
|
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) {
|
2018-05-08 15:12:50 +02:00
|
|
|
const action = doNotify({
|
|
|
|
id: MODALS.FIRST_REWARD,
|
|
|
|
isError: false,
|
2017-07-25 00:59:26 +02:00
|
|
|
});
|
2018-05-08 15:12:50 +02:00
|
|
|
dispatch(action);
|
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,
|
2018-06-07 05:37:48 +02:00
|
|
|
data: {
|
|
|
|
reward,
|
|
|
|
error: !options || !options.failSilently ? error : undefined,
|
|
|
|
},
|
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();
|
2018-05-17 22:31:49 +02:00
|
|
|
const unclaimedRewards = selectUnclaimedRewards(state);
|
2017-12-21 18:32:51 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-17 22:31:49 +02:00
|
|
|
if (unclaimedRewards.find(ur => ur.reward_type === rewards.TYPE_FIRST_STREAM)) {
|
2017-08-19 05:08:01 +02:00
|
|
|
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 => {
|
2018-06-07 05:37:48 +02:00
|
|
|
dispatch(doClaimRewardType(type, { failSilently: true }));
|
2017-12-21 18:32:51 +01:00
|
|
|
});
|
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
|
|
|
}
|