2018-02-04 11:56:32 +00:00
|
|
|
import { interval } from 'rxjs/observable/interval'
|
|
|
|
import { of } from 'rxjs/observable/of'
|
2018-02-16 10:01:00 +00:00
|
|
|
import { takeUntil, mergeMap, catchError, map } from 'rxjs/operators'
|
2018-02-04 11:56:32 +00:00
|
|
|
import { combineEpics, ofType } from 'redux-observable'
|
|
|
|
import ajax from 'universal-rx-request' // because standard AjaxObservable only works in browser
|
|
|
|
|
|
|
|
import * as actions from './actions'
|
|
|
|
import * as types from './actionTypes'
|
|
|
|
|
|
|
|
export const fetchUserEpic = (action$, store) =>
|
|
|
|
action$.pipe(
|
|
|
|
ofType(types.START_FETCHING_CHARACTERS),
|
|
|
|
mergeMap(action => {
|
|
|
|
return interval(3000).pipe(
|
|
|
|
mergeMap(x =>
|
2018-02-16 10:01:00 +00:00
|
|
|
actions.fetchCharacter({
|
|
|
|
isServer: store.getState().isServer
|
|
|
|
})
|
2018-02-04 11:56:32 +00:00
|
|
|
),
|
|
|
|
takeUntil(action$.ofType(types.STOP_FETCHING_CHARACTERS))
|
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
export const fetchCharacterEpic = (action$, store) =>
|
|
|
|
action$.pipe(
|
|
|
|
ofType(types.FETCH_CHARACTER),
|
|
|
|
mergeMap(action =>
|
|
|
|
ajax({
|
|
|
|
url: `https://swapi.co/api/people/${store.getState().nextCharacterId}`
|
|
|
|
}).pipe(
|
2018-02-16 10:01:00 +00:00
|
|
|
map(response =>
|
|
|
|
actions.fetchCharacterSuccess(
|
|
|
|
response.body,
|
|
|
|
store.getState().isServer
|
2018-02-04 11:56:32 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
catchError(error =>
|
|
|
|
of(
|
|
|
|
actions.fetchCharacterFailure(
|
|
|
|
error.response.body,
|
|
|
|
store.getState().isServer
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
export const rootEpic = combineEpics(
|
|
|
|
fetchUserEpic,
|
|
|
|
fetchCharacterEpic
|
|
|
|
)
|