import { Observable } from 'rxjs' import { combineEpics, ActionsObservable } from 'redux-observable' import { Store } from 'redux' import * as actions from './actions' import * as api from './api' import * as AuthActions from '../auth/actions' import * as AuthSelector from '../auth/selectors' export const fetchUserPropertiesOnAuth = (action$: ActionsObservable, { getState }: Store) => { return action$.ofType(AuthActions.fetchUserSuccess.toString(), actions.fetchUserProperties.toString()) .mergeMap(() => { const accessToken = AuthSelector.accessToken(getState()) const userId = AuthSelector.userId(getState()) return api.getUserProperties(userId, accessToken) .map(properties => actions.fetchUserPropertiesSuccess(properties)) .startWith(actions.fetchUserPropertiesPending()) .catch(() => { return Observable.of(actions.fetchUserPropertiesFailed()) }) }) } export const epic = combineEpics( fetchUserPropertiesOnAuth, )