import { createSelector } from 'reselect' ; import { getCurrentUserSpacesUrl , getCurrentUserUrl , getFeaturesUrl , getNamedSpacesUrl , } from '../../api/api-urls' ; import { UserResource } from '../../api/models' ; import { AppState } from '../appState' ; import { getCollectionEntityRefs , getEntityResources , first } from '../jsonapi/selectors' ; const getSpaceEntitiesMap = ( state ) => state . jsonapi . entities . spaces || { } ; const getUserEntitiesMap = ( state ) => state . jsonapi . entities . identities || { } ; const getFeatureEntitiesMap = ( state ) => state . jsonapi . entities . features || { } ; export const getCurrentUserSpaces = createSelector ( getSpaceEntitiesMap , ( state ) => getCollectionEntityRefs ( state , getCurrentUserSpacesUrl ( ) ) , ( map , refs = [ ] ) => refs . map ( ( ref ) => map [ ref . id ] && map [ ref . id ] . resource ) . filter ( ( x ) => ! ! x ) . sort ( ( a , b ) => a . attributes . name . localeCompare ( b . attributes . name ) ) ) ; export const getUserSpaces = ( state , username ) => { const refs = getCollectionEntityRefs ( state , getNamedSpacesUrl ( username ) ) ; if ( refs ) { const map = getSpaceEntitiesMap ( state ) ; return refs . map ( ( ref ) => map [ ref . id ] && map [ ref . id ] . resource ) . filter ( ( x ) => ! ! x ) . sort ( ( a , b ) => a . attributes . name . localeCompare ( b . attributes . name ) ) ; } return [ ] ; } ; export const getSpaceById = createSelector ( getSpaceEntitiesMap , ( _ , id ) => id , ( map , id ) => map [ id ] && map [ id ] . resource ) ; export const getCurrentUser = ( state ) => first ( getEntityResources < UserResource > ( state , getCollectionEntityRefs ( state , getCurrentUserUrl ( ) ) ) ) ; export const getUserById = createSelector ( getUserEntitiesMap , ( _ , id ) => id , ( map , id ) => map [ id ] && map [ id ] . resource ) ; export const getUserByUsername = createSelector ( getUserEntitiesMap , ( _ , username ) => username , ( map , username ) => Object . keys ( map ) . find ( ( id ) => map [ id ] . resource && map [ id ] . resource . attributes . username === username ) ) ; export const getFeatureById = createSelector ( getFeatureEntitiesMap , ( _ , id ) => id , ( map , id ) => map [ id ] && map [ id ] . resource ) ; export const getFeatureCollection = ( state ) => state . jsonapi . collections [ getFeaturesUrl ( ) ] ;