add logic for reward codes #7

Merged
neb-b merged 1 commit from reward-codes into master 2018-10-02 02:47:58 +02:00
4 changed files with 971 additions and 527 deletions

1420
dist/bundle.js vendored

File diff suppressed because it is too large Load diff

View file

@ -21,6 +21,7 @@
"main": "dist/bundle.js", "main": "dist/bundle.js",
"scripts": { "scripts": {
"build": "webpack", "build": "webpack",
"dev": "webpack --watch",
"precommit": "lint-staged", "precommit": "lint-staged",
"lint": "eslint 'src/**/*.js' --fix", "lint": "eslint 'src/**/*.js' --fix",
"format": "prettier 'src/**/*.{js,json}' --write" "format": "prettier 'src/**/*.{js,json}' --write"

View file

@ -26,17 +26,22 @@ export function doRewardList() {
}; };
} }
export function doClaimRewardType(rewardType, options) { export function doClaimRewardType(rewardType, options = {}) {
return (dispatch, getState) => { return (dispatch, getState) => {
const state = getState(); const state = getState();
const unclaimedRewards = selectUnclaimedRewards(state);
const reward = unclaimedRewards.find(ur => ur.reward_type === rewardType);
const userIsRewardApproved = selectUserIsRewardApproved(state); const userIsRewardApproved = selectUserIsRewardApproved(state);
const unclaimedRewards = selectUnclaimedRewards(state);
const reward =
rewardType === rewards.TYPE_REWARD_CODE
? { reward_type: rewards.TYPE_REWARD_CODE }
: unclaimedRewards.find(ur => ur.reward_type === rewardType);
if (!rewards.TYPE_REWARD_CODE) {
if (!reward || reward.transaction_id) { if (!reward || reward.transaction_id) {
// already claimed or doesn't exist, do nothing // already claimed or doesn't exist, do nothing
return; return;
} }
}
if (!userIsRewardApproved && rewardType !== rewards.TYPE_CONFIRM_EMAIL) { if (!userIsRewardApproved && rewardType !== rewards.TYPE_CONFIRM_EMAIL) {
const action = doNotify({ const action = doNotify({
@ -80,7 +85,7 @@ export function doClaimRewardType(rewardType, options) {
}); });
}; };
rewards.claimReward(rewardType).then(success, failure); rewards.claimReward(rewardType, options.params).then(success, failure);
}; };
} }

View file

@ -1,4 +1,4 @@
import { Lbry, doNotify } from 'lbry-redux'; import { Lbry, doNotify, doHideNotification } from 'lbry-redux';
import Lbryio from 'lbryio'; import Lbryio from 'lbryio';
const rewards = {}; const rewards = {};
@ -12,18 +12,25 @@ rewards.TYPE_MANY_DOWNLOADS = 'many_downloads';
rewards.TYPE_FIRST_PUBLISH = 'first_publish'; rewards.TYPE_FIRST_PUBLISH = 'first_publish';
rewards.TYPE_FEATURED_DOWNLOAD = 'featured_download'; rewards.TYPE_FEATURED_DOWNLOAD = 'featured_download';
rewards.TYPE_REFERRAL = 'referral'; rewards.TYPE_REFERRAL = 'referral';
rewards.TYPE_REWARD_CODE = 'reward_code';
rewards.YOUTUBE_CREATOR = 'youtube_creator'; rewards.YOUTUBE_CREATOR = 'youtube_creator';
rewards.claimReward = type => { rewards.claimReward = (type, rewardParams) => {
function requestReward(resolve, reject, params) { function requestReward(resolve, reject, params) {
if (!Lbryio.enabled) { if (!Lbryio.enabled) {
reject(new Error(__('Rewards are not enabled.'))); reject(new Error(__('Rewards are not enabled.')));
return; return;
} }
Lbryio.call('reward', 'new', params, 'post').then(reward => { Lbryio.call('reward', 'new', params, 'post').then(reward => {
const message = const message =
reward.reward_notification || `You have claimed a ${reward.reward_amount} LBC reward.`; reward.reward_notification || `You have claimed a ${reward.reward_amount} LBC reward.`;
// We use a modal in the desktop app for this reward code. Dismiss it before showing the snackbar
if (type === rewards.TYPE_REWARD_CODE) {
window.store.dispatch(doHideNotification());
}
// Display global notice // Display global notice
const action = doNotify({ const action = doNotify({
message, message,
@ -45,6 +52,7 @@ rewards.claimReward = type => {
const params = { const params = {
reward_type: type, reward_type: type,
wallet_address: address, wallet_address: address,
...rewardParams,
}; };
switch (type) { switch (type) {