2020-06-15 22:33:03 +02:00
|
|
|
import { Lbryio } from 'lbryinc';
|
2021-10-17 10:36:14 +02:00
|
|
|
import { doUpdateBalance } from 'redux/actions/wallet';
|
|
|
|
import { doToast } from 'redux/actions/notifications';
|
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2021-12-23 04:57:53 +01:00
|
|
|
import { selectUnclaimedRewards, selectWeeklyWatchClaimedThisWeek } from 'redux/selectors/rewards';
|
2020-06-15 22:33:03 +02:00
|
|
|
import { selectUserIsRewardApproved } from 'redux/selectors/user';
|
|
|
|
import { doFetchInviteStatus } from 'redux/actions/user';
|
|
|
|
import rewards from 'rewards';
|
2022-03-24 08:13:14 +01:00
|
|
|
import { resolveApiMessage } from 'util/api-message';
|
2020-06-15 22:33:03 +02:00
|
|
|
|
|
|
|
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 &&
|
2021-12-23 04:57:53 +01:00
|
|
|
rewardType !== rewards.TYPE_WEEKLY_WATCH &&
|
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();
|
|
|
|
} else if (successReward.reward_type === rewards.TYPE_REFERRAL) {
|
|
|
|
dispatch(doFetchInviteStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-03-24 08:13:14 +01:00
|
|
|
dispatch(doToast({ message: resolveApiMessage(error.message), isError: true }));
|
2020-06-15 22:33:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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_NEW_USER));
|
|
|
|
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-12-23 04:57:53 +01:00
|
|
|
if (!selectWeeklyWatchClaimedThisWeek(state)) {
|
|
|
|
dispatch(doClaimRewardType(rewards.TYPE_WEEKLY_WATCH, { failSilently: true }));
|
|
|
|
}
|
2020-06-15 22:33:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|