Replace weekly watch search loop with new last_claimed

This commit is contained in:
infinite-persistence 2021-12-29 09:30:29 +08:00 committed by Thomas Zarebczan
parent 283ea69a6e
commit c04023948b

View file

@ -71,23 +71,13 @@ export const selectHasClaimedInitialRewards = createSelector(selectClaimedReward
return newUserClaimed && confirmEmailClaimed;
});
export const selectWeeklyWatchClaimedThisWeek = createSelector(selectClaimedRewardsById, (claimedRewardsById) => {
const claimedRewards = Object.values(claimedRewardsById);
// The list could be huge, so:
// - don't use find() which will look from the top.
// - only search until LOOKUP_LIMIT from the back.
const LOOKUP_LIMIT = 15;
let i = claimedRewards.length;
const stopIndex = i > LOOKUP_LIMIT ? i - LOOKUP_LIMIT : 0;
while (--i >= stopIndex) {
if (claimedRewards[i].reward_type === REWARDS.TYPE_WEEKLY_WATCH) {
const last = new Date(claimedRewards[i].updated_at || claimedRewards[i].created_at);
const diff = new Date() - last;
const diffDays = Math.ceil(diff / (1000 * 60 * 60 * 24));
return diffDays < 6;
}
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;
const diffDays = Math.ceil(diff / (1000 * 60 * 60 * 24));
return diffDays < 6;
}
return false;
});