import { Initialized , Success } from '@abraham/remotedata' ; import { createSelector } from '@reduxjs/toolkit' ; import { RootState , store } from '..' ; import { Filter } from '../../models/filter' ; import { SpeakerWithTags } from '../../models/speaker' ; import { selectFilters } from '../../store/filters/selectors' ; import { generateClassName } from '../../utils/functions' ; import { fetchSpeakers } from './actions' ; const selectSpeakerId = ( _state , speakerId ) => speakerId ; const selectSpeakers = ( state ) : SpeakerWithTags [ ] => { const { speakers } = state ; if ( speakers instanceof Success ) { return speakers . data ; } else if ( speakers instanceof Initialized ) { store . dispatch ( fetchSpeakers ) ; } return [ ] ; } ; export const selectSpeaker = createSelector ( selectSpeakers , selectSpeakerId , ( speakers , speakerId ) : SpeakerWithTags | undefined => { return speakers . find ( ( speaker ) => speaker . id === speakerId ) ; } ) ; export const selectFilteredSpeakers = createSelector ( selectSpeakers , selectFilters , ( speakers , selectedFilters ) : SpeakerWithTags [ ] => { if ( selectedFilters . length === 0 ) return speakers ; return speakers . filter ( ( speaker ) => { return ( speaker . tags || [ ] ) . some ( ( tag ) => { const className = generateClassName ( tag ) ; return selectedFilters . some ( ( filter ) => filter . tag === className ) ; } ) ; } ) ; } ) ;