2020-06-15 22:33:03 +02:00
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
import REWARDS from 'rewards';
|
2022-03-22 19:13:19 +01:00
|
|
|
import { selectUser } from 'redux/selectors/user';
|
2020-06-15 22:33:03 +02:00
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
const selectState = (state) => state.rewards || {};
|
2020-06-15 22:33:03 +02:00
|
|
|
|
2021-10-23 04:41:43 +02:00
|
|
|
export const selectUnclaimedRewardsByType = (state) => selectState(state).unclaimedRewardsByType;
|
|
|
|
export const selectClaimedRewardsById = (state) => selectState(state).claimedRewardsById;
|
2020-06-15 22:33:03 +02:00
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
export const selectClaimedRewards = createSelector(selectClaimedRewardsById, (byId) => Object.values(byId) || []);
|
2020-06-15 22:33:03 +02:00
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
export const selectClaimedRewardsByTransactionId = createSelector(selectClaimedRewards, (rewards) =>
|
2020-06-15 22:33:03 +02:00
|
|
|
rewards.reduce((mapParam, reward) => {
|
|
|
|
const map = mapParam;
|
|
|
|
map[reward.transaction_id] = reward;
|
|
|
|
return map;
|
|
|
|
}, {})
|
|
|
|
);
|
|
|
|
|
2021-10-23 04:41:43 +02:00
|
|
|
export const selectUnclaimedRewards = (state) => selectState(state).unclaimedRewards;
|
|
|
|
export const selectFetchingRewards = (state) => !!selectState(state).fetching;
|
2020-06-15 22:33:03 +02:00
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
export const selectUnclaimedRewardValue = createSelector(selectUnclaimedRewards, (rewards) =>
|
2020-06-15 22:33:03 +02:00
|
|
|
rewards.reduce((sum, reward) => sum + reward.reward_amount, 0)
|
|
|
|
);
|
|
|
|
|
2021-10-23 04:41:43 +02:00
|
|
|
export const selectClaimsPendingByType = (state) => selectState(state).claimPendingByType;
|
2020-06-15 22:33:03 +02:00
|
|
|
|
|
|
|
const selectIsClaimRewardPending = (state, props) => selectClaimsPendingByType(state, props)[props.reward_type];
|
|
|
|
|
|
|
|
export const makeSelectIsRewardClaimPending = () =>
|
2021-08-18 18:34:24 +02:00
|
|
|
createSelector(selectIsClaimRewardPending, (isClaiming) => isClaiming);
|
2020-06-15 22:33:03 +02:00
|
|
|
|
2021-10-23 04:41:43 +02:00
|
|
|
export const selectClaimErrorsByType = (state) => selectState(state).claimErrorsByType;
|
2020-06-15 22:33:03 +02:00
|
|
|
|
|
|
|
const selectClaimRewardError = (state, props) => selectClaimErrorsByType(state, props)[props.reward_type];
|
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
export const makeSelectClaimRewardError = () => createSelector(selectClaimRewardError, (errorMessage) => errorMessage);
|
2020-06-15 22:33:03 +02:00
|
|
|
|
|
|
|
const selectRewardByType = (state, rewardType) =>
|
2021-08-18 18:34:24 +02:00
|
|
|
selectUnclaimedRewards(state).find((reward) => reward.reward_type === rewardType);
|
2020-06-15 22:33:03 +02:00
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
export const makeSelectRewardByType = () => createSelector(selectRewardByType, (reward) => reward);
|
2020-06-15 22:33:03 +02:00
|
|
|
|
|
|
|
const selectRewardByClaimCode = (state, claimCode) =>
|
2021-08-18 18:34:24 +02:00
|
|
|
selectUnclaimedRewards(state).find((reward) => reward.claim_code === claimCode);
|
2020-06-15 22:33:03 +02:00
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
export const makeSelectRewardByClaimCode = () => createSelector(selectRewardByClaimCode, (reward) => reward);
|
2020-06-15 22:33:03 +02:00
|
|
|
|
|
|
|
export const makeSelectRewardAmountByType = () =>
|
2021-08-18 18:34:24 +02:00
|
|
|
createSelector(selectRewardByType, (reward) => (reward ? reward.reward_amount : 0));
|
2020-06-15 22:33:03 +02:00
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
export const selectRewardContentClaimIds = createSelector(selectState, (state) => state.rewardedContentClaimIds);
|
2020-06-15 22:33:03 +02:00
|
|
|
|
|
|
|
export const selectReferralReward = createSelector(
|
|
|
|
selectUnclaimedRewards,
|
2021-08-18 18:34:24 +02:00
|
|
|
(unclaimedRewards) => unclaimedRewards.filter((reward) => reward.reward_type === REWARDS.TYPE_REFERRAL)[0]
|
2020-06-15 22:33:03 +02:00
|
|
|
);
|
2020-10-07 02:59:38 +02:00
|
|
|
|
2021-08-18 18:34:24 +02:00
|
|
|
export const selectHasUnclaimedRefereeReward = createSelector(selectUnclaimedRewards, (unclaimedRewards) =>
|
|
|
|
unclaimedRewards.some((reward) => reward.reward_type === REWARDS.TYPE_REFEREE)
|
2020-10-07 02:59:38 +02:00
|
|
|
);
|
2021-08-18 18:34:24 +02:00
|
|
|
|
|
|
|
export const selectIsClaimingInitialRewards = createSelector(selectClaimsPendingByType, (claimsPendingByType) => {
|
|
|
|
return !!(claimsPendingByType[REWARDS.TYPE_NEW_USER] || claimsPendingByType[REWARDS.TYPE_CONFIRM_EMAIL]);
|
|
|
|
});
|
|
|
|
|
|
|
|
export const selectHasClaimedInitialRewards = createSelector(selectClaimedRewardsById, (claimedRewardsById) => {
|
|
|
|
const claims = Object.values(claimedRewardsById);
|
|
|
|
const newUserClaimed = !!claims.find((claim) => claim && claim.reward_type === REWARDS.TYPE_NEW_USER);
|
|
|
|
const confirmEmailClaimed = !!claims.find((claim) => claim && claim.reward_type === REWARDS.TYPE_CONFIRM_EMAIL);
|
|
|
|
return newUserClaimed && confirmEmailClaimed;
|
|
|
|
});
|
2021-12-23 04:57:53 +01:00
|
|
|
|
2021-12-29 02:30:29 +01:00
|
|
|
export const selectWeeklyWatchClaimedThisWeek = createSelector(selectUnclaimedRewards, (unclaimedRewards) => {
|
|
|
|
const weeklyWatch = unclaimedRewards.find((x) => x.reward_type === REWARDS.TYPE_WEEKLY_WATCH);
|
|
|
|
if (weeklyWatch && weeklyWatch.data && weeklyWatch.data.last_claimed) {
|
|
|
|
const last = new Date(weeklyWatch.data.last_claimed);
|
|
|
|
const diff = new Date() - last;
|
2022-07-05 12:44:40 +02:00
|
|
|
const diffDays = diff / (1000 * 60 * 60 * 24);
|
|
|
|
return diffDays < 6.5;
|
2021-12-23 04:57:53 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2022-03-22 19:13:19 +01:00
|
|
|
|
|
|
|
export const selectIsRewardApproved = createSelector(selectUser, (user) => user && user.is_reward_approved);
|