import { Observable } from 'rxjs' import { combineEpics, ActionsObservable } from 'redux-observable' import { Store } from 'redux' import * as api from './api' import * as actions from './actions' import * as AuthSelector from '../auth/selectors' import { fetchUserSuccess } from '../auth/actions' import { User } from '../auth/interfaces' import { IProfile } from './interfaces' const fetchProfile = (action$: ActionsObservable, store: Store) => { return action$.ofType(fetchUserSuccess.toString()) .pluck('payload') .mergeMap((({ id }: User) => { const accessToken = AuthSelector.accessToken(store.getState()) return api.fetchProfile(id, accessToken) .map((value: IProfile) => actions.fetchProfileSuccess(value)) .startWith(actions.fetchProfilePending()) .catch(() => { return Observable.of(actions.fetchProfileFailure()) }) })) } export const epic = combineEpics( fetchProfile, )