2018-07-21 23:37:59 +00:00
|
|
|
import { interval, of } from 'rxjs'
|
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'
|
2018-07-21 23:37:59 +00:00
|
|
|
import { request } from 'universal-rxjs-ajax' // because standard AjaxObservable only works in browser
|
2018-02-04 11:56:32 +00:00
|
|
|
|
|
|
|
import * as actions from './actions'
|
|
|
|
import * as types from './actionTypes'
|
|
|
|
|
2018-07-21 23:37:59 +00:00
|
|
|
export const fetchUserEpic = (action$, state$) =>
|
2018-02-04 11:56:32 +00:00
|
|
|
action$.pipe(
|
|
|
|
ofType(types.START_FETCHING_CHARACTERS),
|
|
|
|
mergeMap(action => {
|
|
|
|
return interval(3000).pipe(
|
2018-07-21 23:37:59 +00:00
|
|
|
map(x =>
|
2018-02-16 10:01:00 +00:00
|
|
|
actions.fetchCharacter({
|
2018-07-21 23:37:59 +00:00
|
|
|
isServer: state$.value.isServer
|
2018-02-16 10:01:00 +00:00
|
|
|
})
|
2018-02-04 11:56:32 +00:00
|
|
|
),
|
|
|
|
takeUntil(action$.ofType(types.STOP_FETCHING_CHARACTERS))
|
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2018-07-21 23:37:59 +00:00
|
|
|
export const fetchCharacterEpic = (action$, state$) =>
|
2018-02-04 11:56:32 +00:00
|
|
|
action$.pipe(
|
|
|
|
ofType(types.FETCH_CHARACTER),
|
|
|
|
mergeMap(action =>
|
2018-07-21 23:37:59 +00:00
|
|
|
request({
|
|
|
|
url: `https://swapi.co/api/people/${state$.value.nextCharacterId}`
|
2018-02-04 11:56:32 +00:00
|
|
|
}).pipe(
|
2018-02-16 10:01:00 +00:00
|
|
|
map(response =>
|
|
|
|
actions.fetchCharacterSuccess(
|
2018-07-21 23:37:59 +00:00
|
|
|
response.response,
|
|
|
|
state$.value.isServer
|
2018-02-04 11:56:32 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
catchError(error =>
|
|
|
|
of(
|
|
|
|
actions.fetchCharacterFailure(
|
2018-07-21 23:37:59 +00:00
|
|
|
error.xhr.response,
|
|
|
|
state$.value.isServer
|
2018-02-04 11:56:32 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
export const rootEpic = combineEpics(
|
|
|
|
fetchUserEpic,
|
|
|
|
fetchCharacterEpic
|
|
|
|
)
|