import { createSelector } from 'reselect' ; import { HitDatabaseEntry , HitDatabaseMap , RequesterMap , Requester , HitId , RequesterId , LegacyDateFormat } from '../types' ; import { todayFormatted , stringToDate } from '../utils/dates' ; import { rewardAndBonus , isPending , isApprovedButNotPaid , isNotRejected , isRejected } from '../utils/hitDatabase' ; import { Map , List } from 'immutable' ; import { hitDatabaseSelector } from './index' ; import { LEGACY_DATE_FORMAT } from 'constants/dates' ; export const pendingEarningsSelector = createSelector ( [ hitDatabaseSelector ] , hitDatabase => hitDatabase . reduce ( ( acc , hit ) => isPending ( hit ) ? acc + hit . reward : acc , 0 ) ) ; export const approvedButNotPaidEarnings = createSelector ( [ hitDatabaseSelector ] , hitDatabase => hitDatabase . reduce ( ( acc , hit ) => isApprovedButNotPaid ( hit ) ? acc + hit . reward : acc , 0 ) ) ; export const dateMoneyMap = createSelector ( [ hitDatabaseSelector ] , ( hitDatabase ) : Map < HitId , number > => hitDatabase . reduce ( ( acc , el ) => acc . update ( el . date , ( reward ) => reward ? reward + rewardAndBonus ( el ) : rewardAndBonus ( el ) ) , Map < LegacyDateFormat , number > ( ) ) ) ; export const hitsCompletedToday = createSelector ( [ hitDatabaseSelector ] , ( hitDatabase ) : HitDatabaseMap => { const todaysDateString = todayFormatted ( ) ; return hitDatabase . filter ( ( el ) => el . date === todaysDateString ) as HitDatabaseMap ; } ) ; export const unRejectedHitsCompletedToday = createSelector ( [ hitsCompletedToday ] , hits => hits . filter ( isNotRejected ) ) ; export const todaysProjectedEarnings = createSelector ( [ unRejectedHitsCompletedToday ] , hits => hits . reduce ( ( acc , cur ) => acc + rewardAndBonus ( cur ) , 0 ) ) ; export const hitDatabaseToRequesterMap = createSelector ( [ hitDatabaseSelector ] , database => database . reduce ( ( acc , cur ) : RequesterMap => acc . set ( cur . requester . id , cur . requester ) , Map < RequesterId , Requester > ( ) ) ) ; export const hitDatabaseToRequesterWorkHistoryMap = createSelector ( [ hitDatabaseSelector ] , database => database . reduce ( ( acc , cur ) => acc . update ( cur . requester . id , ( submittedHits ) => submittedHits ? submittedHits . push ( cur ) : List ( [ cur ] ) ) , Map < RequesterId , List < HitDatabaseEntry > > ( ) ) ) ; const getAllHitsSubmittedToRequester = ( requesterId ) => createSelector ( [ hitDatabaseToRequesterWorkHistoryMap ] , workHistory => workHistory . get ( requesterId , List ( [ ] ) ) ) ; export const numSubmittedHitsToRequester = ( requesterId ) => createSelector ( [ getAllHitsSubmittedToRequester ( requesterId ) ] , hits => hits . size ) ; export const numRejectedHitsToRequester = ( requesterId ) => createSelector ( [ getAllHitsSubmittedToRequester ( requesterId ) ] , hits => hits . filter ( isRejected ) . size ) ; export const allHitsSubmittedToRequesterRecentFirst = ( requesterId ) => createSelector ( [ getAllHitsSubmittedToRequester ( requesterId ) ] , hits => hits . sort ( ( a , b ) => stringToDate ( b . date ) ( LEGACY_DATE_FORMAT ) . valueOf ( ) - stringToDate ( a . date ) ( LEGACY_DATE_FORMAT ) . valueOf ( ) ) as List < HitDatabaseEntry > ) ;