lbry-desktop/ui/js/selectors/rewards.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import { createSelector } from "reselect";
import { selectUser } from "selectors/user";
2017-08-26 05:21:26 +02:00
import rewards from "rewards";
2017-05-26 10:53:32 +02:00
const _selectState = state => state.rewards || {};
export const selectUnclaimedRewardsByType = createSelector(
2017-05-26 10:53:32 +02:00
_selectState,
state => state.unclaimedRewardsByType
2017-06-08 02:56:52 +02:00
);
2017-05-26 10:53:32 +02:00
export const selectClaimedRewardsById = createSelector(
_selectState,
state => state.claimedRewardsById
);
export const selectClaimedRewards = createSelector(
selectClaimedRewardsById,
byId => Object.values(byId) || []
);
export const selectUnclaimedRewards = createSelector(
selectUnclaimedRewardsByType,
2017-08-26 05:21:26 +02:00
byType =>
Object.values(byType).sort(function(a, b) {
return rewards.SORT_ORDER.indexOf(a.reward_type) <
rewards.SORT_ORDER.indexOf(b.reward_type)
? -1
: 1;
}) || []
2017-06-08 02:56:52 +02:00
);
2017-05-26 10:53:32 +02:00
export const selectIsRewardEligible = createSelector(
selectUser,
2017-06-08 02:56:52 +02:00
user => user.can_claim_rewards
);
2017-05-26 10:53:32 +02:00
export const selectFetchingRewards = createSelector(
_selectState,
2017-06-08 02:56:52 +02:00
state => !!state.fetching
);
2017-05-26 10:53:32 +02:00
export const selectUnclaimedRewardValue = createSelector(
selectUnclaimedRewards,
rewards =>
rewards.reduce((sum, reward) => {
return sum + reward.reward_amount;
}, 0)
2017-07-28 03:13:12 +02:00
);
2017-05-26 10:53:32 +02:00
export const selectHasClaimedReward = (state, props) => {
2017-06-08 23:15:34 +02:00
const reward = selectRewardsByType(state)[props.reward_type];
return reward && reward.transaction_id !== "";
2017-06-08 02:56:52 +02:00
};
2017-05-26 10:53:32 +02:00
export const makeSelectHasClaimedReward = () => {
2017-06-08 02:56:52 +02:00
return createSelector(selectHasClaimedReward, claimed => claimed);
};
2017-05-26 10:53:32 +02:00
export const selectClaimsPendingByType = createSelector(
_selectState,
2017-06-08 02:56:52 +02:00
state => state.claimPendingByType
);
const selectIsClaimRewardPending = (state, props) => {
2017-06-08 02:56:52 +02:00
return selectClaimsPendingByType(state, props)[props.reward_type];
};
export const makeSelectIsRewardClaimPending = () => {
2017-06-08 02:56:52 +02:00
return createSelector(selectIsClaimRewardPending, isClaiming => isClaiming);
};
export const selectClaimErrorsByType = createSelector(
_selectState,
2017-06-08 02:56:52 +02:00
state => state.claimErrorsByType
);
const selectClaimRewardError = (state, props) => {
2017-06-08 02:56:52 +02:00
return selectClaimErrorsByType(state, props)[props.reward_type];
};
export const makeSelectClaimRewardError = () => {
2017-06-08 02:56:52 +02:00
return createSelector(selectClaimRewardError, errorMessage => errorMessage);
};
2017-05-26 10:53:32 +02:00
const selectRewardByType = (state, props) => {
return selectUnclaimedRewardsByType(state)[props.reward_type];
2017-06-08 02:56:52 +02:00
};
2017-05-26 10:53:32 +02:00
export const makeSelectRewardByType = () => {
2017-06-08 02:56:52 +02:00
return createSelector(selectRewardByType, reward => reward);
};
2017-08-26 05:21:26 +02:00
export const makeSelectRewardAmountByType = () => {
return createSelector(
selectRewardByType,
reward => (reward ? reward.reward_amount : 0)
);
};