import format from 'date-fns/format' ; import { AugmentedSession , Session } from '../types/session' ; import { sortByDate } from './dateHelpers' ; export function hasLiveTag ( session ) { return ( session . tags || [ ] ) . map ( ( x ) => x . name ) . includes ( 'type:Live' ) ; } export function isLive ( session ) { const isLive = session . isLiveSession ; if ( isLive ) { return Date . now ( ) < session . end_date ; } return isLive ; } export function getLevels ( session ) { if ( ! Array . isArray ( session . tags ) ) { return '' ; } const levels = session . tags . map ( ( x ) => x . name ) . filter ( ( x ) => x . startsWith ( 'level:' ) ) . map ( ( x ) => x . replace ( 'level:' , '' ) ) ; return levels . join ( ',' ) ; } export function isPaid ( session ) { return ( session . tags || [ ] ) . map ( ( x ) => x . name ) . includes ( 'access:Paid (All Access)' ) ; } export function isSignalTv ( session ) { return ! ! session . signalTv ; } function augmentSession ( session ) { const live = hasLiveTag ( session ) ; const signalTv = isSignalTv ( session ) ; return { ... session , isLiveSession : live , canRegister : live , isSignalTv : signalTv , } ; } type GroupedSessions = { [ key ] : AugmentedSession [ ] ; } ; export function groupSessionsByTypeOrDate ( sessions ) { const groupedSets = { } ; for ( const baseSession of sessions ) { const session = augmentSession ( baseSession ) ; if ( ! session . isLiveSession && ! session . isSignalTv ) { if ( ! groupedSets [ 'On Demand' ] ) { groupedSets [ 'On Demand' ] = new Set ( ) ; } groupedSets [ 'On Demand' ] . add ( session ) ; continue ; } const startDate = format ( new Date ( session . start_date ) , 'yyyy-MM-dd' ) ; if ( ! groupedSets [ startDate ] ) { groupedSets [ startDate ] = new Set ( ) ; } groupedSets [ startDate ] . add ( session ) ; } const grouped = { } ; for ( const date of Object . keys ( groupedSets ) ) { if ( date === 'On Demand' ) { grouped [ date ] = [ ... groupedSets [ date ] ] . sort ( ( a , b ) => a . title . localeCompare ( b . title ) ) ; } else { grouped [ date ] = [ ... groupedSets [ date ] ] . sort ( sortByDate ) ; } } return grouped ; }