import { getCurrentUserSpacesUrl , getCurrentUserUrl , getFeaturesUrl , getNamedSpacesUrl , } from '../../api/api-urls' ; import { fetchCollection , fetchEntity } from '../jsonapi/actions' ; import { getCollectionEntityRefs } from '../jsonapi/selectors' ; import { ThunkAction } from '../utils' ; import { getSpaceById } from './selectors' ; export function fetchCurrentUserSpaces ( ) { return async function ( dispatch , getState ) { await dispatch ( fetchCollection ( getCurrentUserSpacesUrl ( ) ) ) ; const refs = getCollectionEntityRefs ( getState ( ) , getCurrentUserSpacesUrl ( ) ) ; if ( refs ) { await Promise . all ( refs . map ( ( ref ) => dispatch ( fetchEntity ( 'spaces' , ref . id ) ) ) ) ; } } ; } export function fetchUserSpacesByUsername ( username ) { return async function ( dispatch ) { await dispatch ( fetchCollection ( getNamedSpacesUrl ( username ) ) ) ; } ; } export function fetchCurrentUser ( ) { return async function ( dispatch ) { await dispatch ( fetchCollection ( getCurrentUserUrl ( ) ) ) ; } ; } export function fetchUserById ( id ) { return async function ( dispatch ) { await dispatch ( fetchEntity ( 'identities' , id ) ) ; } ; } export function fetchFeatures ( ) { return async function ( dispatch ) { await dispatch ( fetchCollection ( getFeaturesUrl ( ) ) ) ; } ; } export function fetchSpaceById ( id ) { return async function ( dispatch ) { await dispatch ( fetchEntity ( 'spaces' , id ) ) ; } ; } export function fetchSpaceOwner ( id ) { return async function ( dispatch , getState ) { await dispatch ( fetchSpaceById ( id ) ) ; const space = getSpaceById ( getState ( ) , id ) ; if ( space ) { await dispatch ( fetchUserById ( space . relationships [ 'owned-by' ] . data . id ) ) ; } } ; }