lbry-desktop/src/renderer/redux/actions/rewards.js

110 lines
2.8 KiB
JavaScript
Raw Normal View History

import * as ACTIONS from 'constants/action_types';
import { MODALS } from 'lbry-redux';
import Lbryio from 'lbryio';
import { selectUnclaimedRewardsByType } from 'redux/selectors/rewards';
import { selectUserIsRewardApproved } from 'redux/selectors/user';
import rewards from 'rewards';
2017-06-03 01:09:52 +02:00
export function doRewardList() {
return dispatch => {
dispatch({
type: ACTIONS.FETCH_REWARDS_STARTED,
2017-06-06 23:19:12 +02:00
});
Lbryio.call('reward', 'list', { multiple_rewards_per_type: true })
2017-06-08 23:15:34 +02:00
.then(userRewards => {
dispatch({
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({
type: ACTIONS.FETCH_REWARDS_COMPLETED,
2017-06-08 23:15:34 +02:00
data: { userRewards: [] },
});
});
};
}
export function doClaimRewardType(rewardType) {
return (dispatch, getState) => {
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;
}
if (!userIsRewardApproved && rewardType !== rewards.TYPE_CONFIRM_EMAIL) {
dispatch({
type: ACTIONS.OPEN_MODAL,
data: { modal: MODALS.REWARD_APPROVAL_REQUIRED },
2017-08-26 05:21:26 +02:00
});
return;
2017-08-26 05:21:26 +02:00
}
2017-05-26 10:53:32 +02:00
dispatch({
type: ACTIONS.CLAIM_REWARD_STARTED,
2017-06-08 23:15:34 +02:00
data: { reward },
});
const success = successReward => {
dispatch({
type: ACTIONS.CLAIM_REWARD_SUCCESS,
data: {
reward: successReward,
2017-06-08 23:15:34 +02:00
},
});
if (successReward.reward_type === rewards.TYPE_NEW_USER) {
2017-07-25 00:59:26 +02:00
dispatch({
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-08 23:15:34 +02:00
const failure = error => {
dispatch({
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() {
return (dispatch, getState) => {
const state = getState();
const rewardsByType = selectUnclaimedRewardsByType(state);
const userIsRewardApproved = selectUserIsRewardApproved(state);
if (!userIsRewardApproved || !Lbryio.enabled) {
2017-06-08 23:15:34 +02:00
return;
}
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 dispatch => {
dispatch({
type: ACTIONS.CLAIM_REWARD_CLEAR_ERROR,
2017-06-08 23:15:34 +02:00
data: { reward },
});
};
}