import { Initialized , Success } from '@abraham/remotedata' ; import { createSelector } from '@reduxjs/toolkit' ; import { RootState , store } from '..' ; import { Filter } from '../../models/filter' ; import { FilterGroup , FilterGroupKey } from '../../models/filter-group' ; import { Session } from '../../models/session' ; import { fetchSessions } from './actions' ; const buildFilter = ( group , tag ) : Filter => { return { group , tag } ; } ; const buildFilters = ( sessions , filterGroupKey ) : Filter [ ] => { const tags = new Set < string > ( ) ; sessions . forEach ( ( session ) => { const value = session [ filterGroupKey ] ; if ( value === undefined ) { return ; } else if ( typeof value === 'string' ) { tags . add ( value . trim ( ) ) ; } else { value . map ( ( tag ) => tags . add ( tag . trim ( ) ) ) ; } } ) ; return [ ... tags ] . map ( ( value ) => buildFilter ( FilterGroupKey [ filterGroupKey ] , value ) ) ; } ; const selectSessionId = ( _state , sessionId ) => sessionId ; const selectSessions = ( state ) => { if ( state . sessions instanceof Success ) { return state . sessions . data ; } else if ( state . sessions instanceof Initialized ) { store . dispatch ( fetchSessions ) ; } return [ ] ; } ; export const selectSession = createSelector ( selectSessions , selectSessionId , ( sessions , sessionId ) : Session | undefined => { return sessions . find ( ( session ) => session . id === sessionId ) ; } ) ; const selectGroups = ( _state , groups = [ FilterGroupKey . tags , FilterGroupKey . complexity ] ) => groups ; export const selectFilterGroups = createSelector ( selectSessions , selectGroups , ( sessions , groups ) : FilterGroup [ ] => { return [ { title : '{$ filters.tags $}' , key : FilterGroupKey . tags , filters : buildFilters ( sessions , FilterGroupKey . tags ) , } , { title : '{$ filters.complexity $}' , key : FilterGroupKey . complexity , filters : buildFilters ( sessions , FilterGroupKey . complexity ) , } , ] . filter ( ( filterGroup ) => groups . includes ( filterGroup . key ) ) ; } ) ;