2020-06-15 22:33:03 +02:00
|
|
|
import { Lbryio } from 'lbryinc';
|
2021-10-08 05:47:39 +02:00
|
|
|
import { doUpdateBalance } from 'redux/actions/wallet';
|
|
|
|
import { doToast } from 'redux/actions/notifications';
|
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2020-06-15 22:33:03 +02:00
|
|
|
import { selectUnclaimedRewards } from 'redux/selectors/rewards';
|
|
|
|
import { selectUserIsRewardApproved } from 'redux/selectors/user';
|
|
|
|
import rewards from 'rewards';
|
|
|
|
|
|
|
|
export function doRewardList() {
|
2021-08-18 18:34:24 +02:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.FETCH_REWARDS_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.call('reward', 'list', { multiple_rewards_per_type: true })
|
2021-08-18 18:34:24 +02:00
|
|
|
.then((userRewards) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.FETCH_REWARDS_COMPLETED,
|
|
|
|
data: { userRewards },
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.FETCH_REWARDS_COMPLETED,
|
|
|
|
data: { userRewards: [] },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doClaimRewardType(rewardType, options = {}) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const userIsRewardApproved = selectUserIsRewardApproved(state);
|
|
|
|
const unclaimedRewards = selectUnclaimedRewards(state);
|
|
|
|
const reward =
|
|
|
|
rewardType === rewards.TYPE_REWARD_CODE || rewardType === rewards.TYPE_NEW_ANDROID
|
|
|
|
? { reward_type: rewards.TYPE_REWARD_CODE }
|
2021-08-18 18:34:24 +02:00
|
|
|
: unclaimedRewards.find((ur) => ur.reward_type === rewardType);
|
2020-06-15 22:33:03 +02:00
|
|
|
|
|
|
|
// Try to claim the email reward right away, even if we haven't called reward_list yet
|
|
|
|
if (
|
|
|
|
rewardType !== rewards.TYPE_REWARD_CODE &&
|
|
|
|
rewardType !== rewards.TYPE_CONFIRM_EMAIL &&
|
|
|
|
rewardType !== rewards.TYPE_DAILY_VIEW &&
|
2020-07-22 18:42:06 +02:00
|
|
|
rewardType !== rewards.TYPE_NEW_ANDROID
|
2020-06-15 22:33:03 +02:00
|
|
|
) {
|
|
|
|
if (!reward || reward.transaction_id) {
|
|
|
|
// already claimed or doesn't exist, do nothing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!userIsRewardApproved &&
|
|
|
|
rewardType !== rewards.TYPE_CONFIRM_EMAIL &&
|
|
|
|
rewardType !== rewards.TYPE_REWARD_CODE &&
|
|
|
|
rewardType !== rewards.TYPE_NEW_ANDROID
|
|
|
|
) {
|
|
|
|
if (!options || (!options.failSilently && rewards.callbacks.rewardApprovalRequested)) {
|
|
|
|
rewards.callbacks.rewardApprovalRequested();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set `claim_code` so the api knows which reward to give if there are multiple of the same type
|
|
|
|
const params = options.params || {};
|
|
|
|
if (!params.claim_code && reward) {
|
|
|
|
params.claim_code = reward.claim_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.CLAIM_REWARD_STARTED,
|
|
|
|
data: { reward },
|
|
|
|
});
|
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
const success = (successReward) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
// Temporary timeout to ensure the sdk has the correct balance after claiming a reward
|
|
|
|
setTimeout(() => {
|
|
|
|
dispatch(doUpdateBalance()).then(() => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.CLAIM_REWARD_SUCCESS,
|
|
|
|
data: {
|
|
|
|
reward: successReward,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (successReward.reward_type === rewards.TYPE_NEW_USER && rewards.callbacks.claimFirstRewardSuccess) {
|
|
|
|
rewards.callbacks.claimFirstRewardSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(doRewardList());
|
|
|
|
|
|
|
|
if (options.callback) {
|
|
|
|
options.callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 2000);
|
|
|
|
};
|
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
const failure = (error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.CLAIM_REWARD_FAILURE,
|
|
|
|
data: {
|
|
|
|
reward,
|
|
|
|
error: !options || !options.failSilently ? error : undefined,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.notifyError) {
|
|
|
|
dispatch(doToast({ message: error.message, isError: true }));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.callback) {
|
|
|
|
options.callback(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return rewards.claimReward(rewardType, params).then(success, failure);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
export function doClaimInitialRewards() {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(doClaimRewardType(rewards.TYPE_CONFIRM_EMAIL));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-15 22:33:03 +02:00
|
|
|
export function doClaimEligiblePurchaseRewards() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const unclaimedRewards = selectUnclaimedRewards(state);
|
|
|
|
const userIsRewardApproved = selectUserIsRewardApproved(state);
|
|
|
|
|
|
|
|
if (!userIsRewardApproved || !Lbryio.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
if (unclaimedRewards.find((ur) => ur.reward_type === rewards.TYPE_FIRST_STREAM)) {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch(doClaimRewardType(rewards.TYPE_FIRST_STREAM));
|
|
|
|
} else {
|
2021-08-18 18:34:24 +02:00
|
|
|
[rewards.TYPE_MANY_DOWNLOADS, rewards.TYPE_DAILY_VIEW].forEach((type) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch(doClaimRewardType(type, { failSilently: true }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doClaimRewardClearError(reward) {
|
2021-08-18 18:34:24 +02:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.CLAIM_REWARD_CLEAR_ERROR,
|
|
|
|
data: { reward },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doFetchRewardedContent() {
|
2021-08-18 18:34:24 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
const success = (nameToClaimId) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.FETCH_REWARD_CONTENT_COMPLETED,
|
|
|
|
data: {
|
|
|
|
claimIds: Object.values(nameToClaimId),
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const failure = () => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.FETCH_REWARD_CONTENT_COMPLETED,
|
|
|
|
data: {
|
|
|
|
claimIds: [],
|
|
|
|
success: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Lbryio.call('reward', 'list_featured').then(success, failure);
|
|
|
|
};
|
|
|
|
}
|