import { Initialized , Success } from '@abraham/remotedata' ; import { createSelector } from '@reduxjs/toolkit' ; import { RootState , store } from '..' ; import { PreviousSpeaker } from '../../models/previous-speaker' ; import { randomOrder } from '../../utils/functions' ; import { selectViewport } from '../ui/selectors' ; import { Viewport } from '../ui/types' ; import { fetchPreviousSpeakers } from './actions' ; const selectSpeakerId = ( _state , speakerId ) => speakerId ; const selectPreviousSpeakers = ( state ) : PreviousSpeaker [ ] => { const { previousSpeakers } = state ; if ( previousSpeakers instanceof Success ) { return previousSpeakers . data ; } else if ( previousSpeakers instanceof Initialized ) { store . dispatch ( fetchPreviousSpeakers ) ; } return [ ] ; } ; export const selectPreviousSpeaker = createSelector ( selectPreviousSpeakers , selectSpeakerId , ( speakers , speakerId ) : PreviousSpeaker | undefined => { return speakers . find ( ( speaker ) => speaker . id === speakerId ) ; } ) ; export const selectRandomPreviousSpeakers = createSelector ( selectPreviousSpeakers , selectViewport , ( previousSpeakers , viewport ) : PreviousSpeaker [ ] => { const displayCount = viewport . isPhone ? 8 : 14 ; return randomOrder ( previousSpeakers ) . slice ( 0 , displayCount ) ; } ) ;