lbry-desktop/src/renderer/redux/selectors/rewards.js

99 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import { createSelector } from "reselect";
2017-11-13 22:02:23 +01:00
import { selectUser } from "redux/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 selectClaimedRewardsByTransactionId = createSelector(
selectClaimedRewards,
rewards =>
rewards.reduce((map, reward) => {
map[reward.transaction_id] = reward;
return map;
}, {})
);
export const selectUnclaimedRewards = createSelector(
selectUnclaimedRewardsByType,
2017-08-26 05:21:26 +02:00
byType =>
2017-12-13 22:36:30 +01:00
Object.values(byType).sort(
(a, b) =>
rewards.SORT_ORDER.indexOf(a.reward_type) <
2017-08-26 05:21:26 +02:00
rewards.SORT_ORDER.indexOf(b.reward_type)
2017-12-13 22:36:30 +01:00
? -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,
2017-12-13 22:36:30 +01:00
rewards => rewards.reduce((sum, reward) => 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
2017-12-13 22:36:30 +01:00
export const makeSelectHasClaimedReward = () =>
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
);
2017-12-13 22:36:30 +01:00
const selectIsClaimRewardPending = (state, props) =>
selectClaimsPendingByType(state, props)[props.reward_type];
2017-12-13 22:36:30 +01:00
export const makeSelectIsRewardClaimPending = () =>
createSelector(selectIsClaimRewardPending, isClaiming => isClaiming);
export const selectClaimErrorsByType = createSelector(
_selectState,
2017-06-08 02:56:52 +02:00
state => state.claimErrorsByType
);
2017-12-13 22:36:30 +01:00
const selectClaimRewardError = (state, props) =>
selectClaimErrorsByType(state, props)[props.reward_type];
2017-12-13 22:36:30 +01:00
export const makeSelectClaimRewardError = () =>
createSelector(selectClaimRewardError, errorMessage => errorMessage);
2017-12-13 22:36:30 +01:00
const selectRewardByType = (state, props) =>
selectUnclaimedRewardsByType(state)[props.reward_type];
2017-05-26 10:53:32 +02:00
2017-12-13 22:36:30 +01:00
export const makeSelectRewardByType = () =>
createSelector(selectRewardByType, reward => reward);
2017-08-26 05:21:26 +02:00
2017-12-13 22:36:30 +01:00
export const makeSelectRewardAmountByType = () =>
createSelector(
2017-08-26 05:21:26 +02:00
selectRewardByType,
reward => (reward ? reward.reward_amount : 0)
);