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

74 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import { createSelector } from "reselect";
import { selectUser } from "selectors/user";
2017-05-26 10:53:32 +02:00
const _selectState = state => state.rewards || {};
export const selectRewardsByType = createSelector(
_selectState,
2017-06-08 23:15:34 +02:00
state => state.rewardsByType || {}
2017-06-08 02:56:52 +02:00
);
2017-05-26 10:53:32 +02:00
export const selectRewards = createSelector(
selectRewardsByType,
2017-06-08 02:56:52 +02:00
byType => Object.values(byType) || []
);
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
2017-07-28 03:13:12 +02:00
export const selectTotalRewardValue = createSelector(selectRewards, rewards =>
rewards.reduce((sum, reward) => {
return sum + reward.reward_amount;
}, 0)
);
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) => {
2017-06-08 02:56:52 +02:00
return selectRewardsByType(state)[props.reward_type];
};
2017-05-26 10:53:32 +02:00
export const makeSelectRewardByType = () => {
2017-06-08 02:56:52 +02:00
return createSelector(selectRewardByType, reward => reward);
};