import { Initialized , Success } from '@abraham/remotedata' ; import { createSelector } from '@reduxjs/toolkit' ; import { RootState , store } from '..' ; import { Day } from '../../models/day' ; import { Session } from '../../models/session' ; import { Time } from '../../models/time' ; import { Timeslot } from '../../models/timeslot' ; import { TempAny } from '../../temp-any' ; import { selectFeaturedSessions } from '../featured-sessions/selectors' ; import { FeaturedSessions } from '../featured-sessions/state' ; import { fetchSchedule } from './actions' ; const selectSchedule = ( state ) : Day [ ] => { if ( state . schedule instanceof Success ) { return state . schedule . data ; } else if ( state . schedule instanceof Initialized ) { store . dispatch ( fetchSchedule ) ; } return [ ] ; } ; export const selectFeaturedSchedule = createSelector ( selectSchedule , selectFeaturedSessions , ( schedule , featuredSessions ) : Day [ ] => { return schedule . map ( ( day ) => { return { ... day , timeslots : day . timeslots . map ( ( timeslot ) => { return { ... timeslot , sessions : timeslot . sessions . map ( ( sessionBlock ) => { const items = ( sessionBlock . items as TempAny as Session [ ] ) . filter ( ( session ) : boolean => { return featuredSessions [ session . id ] ; } ) ; return { ... sessionBlock , items , } ; } ) , } ; } ) , } ; } ) ; } ) ;